Version: 8.3.0
YACS::ENGINE::LocalContainer Class Reference

#include <CppContainer.hxx>

Collaboration diagram for YACS::ENGINE::LocalContainer:

Public Member Functions

void destroy ()
 
LocalLibrary loadComponentLibrary (const std::string &, const char *prefix=NULL, bool forcedLoad=false)
 
CppComponentcreateComponentInstance (const char *componentName)
 Find or create a new C++ component instance. More...
 
void createInternalInstance (const char *componentName, void *&obj, RunFunction &r, TerminateFunction &t)
 
void unLoadComponentLibrary (const std::string &aCompName)
 
void unregisterComponentInstance (CppComponent *C)
 

Static Public Member Functions

static
YACS::ENGINE::LocalContainer
get ()
 

Protected Member Functions

 LocalContainer ()
 
virtual ~LocalContainer ()
 

Protected Attributes

YACS::BASES::Mutex _instance_mapMutex
 
YACS::BASES::Mutex _library_mapMutex
 

Static Protected Attributes

static std::map< std::string,
LocalLibrary
_library_map
 
static std::multimap
< std::string, CppComponent * > 
_instance_map
 

Static Private Attributes

static LocalContainer_singleton = NULL
 

Friends

class CppComponent
 

Detailed Description

Definition at line 71 of file CppContainer.hxx.

Constructor & Destructor Documentation

LocalContainer::LocalContainer ( )
protected

Definition at line 182 of file CppContainer.cxx.

Referenced by get().

183 {
184 }
LocalContainer::~LocalContainer ( )
protectedvirtual

Definition at line 186 of file CppContainer.cxx.

References destroy().

187 {
188  destroy();
189 }

Member Function Documentation

CppComponent * LocalContainer::createComponentInstance ( const char *  name)

Find or create a new C++ component instance.

Create a new component class (C++ implementation)

Parameters
name: component name

Try to return a handle to an new instance of a C++ component If the associated library is not loaded, try to load it

Returns
a handle to the component instance or throw an exception if it's not possible

Definition at line 243 of file CppContainer.cxx.

References _instance_map, _instance_mapMutex, CppComponent, createInternalInstance(), and gui.Appli::t.

Referenced by YACS::ENGINE::CppContainer::createComponentInstance().

244 {
245  void *o;
246  RunFunction r;
248 
249  createInternalInstance(name, o, r, t);
250 
251  CppComponent * C;
252  C = new CppComponent(o, r, t, name);
253  _instance_mapMutex.lock(); // lock to be alone
254  _instance_map.insert(std::pair<std::string, CppComponent *>(name, C));
255  _instance_mapMutex.unLock(); // unlock
256  return C;
257 }
void LocalContainer::createInternalInstance ( const char *  componentName,
void *&  obj,
RunFunction r,
TerminateFunction t 
)

Definition at line 259 of file CppContainer.cxx.

References _library_map, CORBAEngineTest::i, YACS::ENGINE::LocalLibrary::initHandle, loadComponentLibrary(), p, YACS::ENGINE::LocalLibrary::pingHandle, YACS::ENGINE::LocalLibrary::runHandle, and YACS::ENGINE::LocalLibrary::terminateHandle.

Referenced by createComponentInstance(), and YACS::ENGINE::CppContainer::createInternalInstance().

261 {
262  LocalLibrary L;
263 
264  std::map<std::string, LocalLibrary>::iterator foundL = _library_map.find(name);
265  if (foundL != _library_map.end())
266  L = foundL->second;
267  else
268  L = loadComponentLibrary(name, NULL, false);
269 
270  r = L.runHandle;
272  t = L.terminateHandle;
273 
275  if (p) p();
276 
277  obj = i();
278 
279 }
void LocalContainer::destroy ( )

Definition at line 200 of file CppContainer.cxx.

References _instance_map, _instance_mapMutex, _library_map, _library_mapMutex, and _singleton.

Referenced by ~LocalContainer().

201 {
202  if (NULL == _singleton)
203  return;
204 
205  // destroy all component instances
206  _instance_mapMutex.lock(); // lock
207  std::multimap<std::string, CppComponent *>::iterator iI, iJ;
208  for (iI=_instance_map.begin(); iI != _instance_map.end(); iI = iJ)
209  {
210  iJ = iI++;
211  iI->second->setContainer(NULL);
212  delete iI->second;
213  }
214  _instance_map.clear();
215  _instance_mapMutex.unLock(); // unlock
216 
217  // unload all dynamic libraries
218  _library_mapMutex.lock();
219  std::map<std::string, LocalLibrary>::iterator iL;
220  for (iL=_library_map.begin(); iL != _library_map.end(); iL++)
221  dlclose(iL->second.handle);
222  _library_map.clear();
223  _library_mapMutex.unLock();
224 
225  delete _singleton;
226  _singleton = NULL;
227 }
LocalContainer * LocalContainer::get ( )
static

Definition at line 191 of file CppContainer.cxx.

References _singleton, and LocalContainer().

Referenced by YACS::ENGINE::CppContainer::start().

192 {
193  if (NULL == _singleton)
194  {
196  }
197  return _singleton;
198 }
LocalLibrary LocalContainer::loadComponentLibrary ( const std::string &  aCompName,
const char *  prefix = NULL,
bool  forcedLoad = false 
)

Definition at line 293 of file CppContainer.cxx.

References _library_map, DEBTRACE, gui.Appli::t, toupper(), and unLoadComponentLibrary().

Referenced by createInternalInstance().

294 {
295 
296  // if forcedLoad is true, unload library if it exists
297  // if forcedLoad is false, return the existing library or load it
298 
299  if (forcedLoad)
300  unLoadComponentLibrary(aCompName);
301  else
302  {
303  std::map<std::string, LocalLibrary >::iterator itLib
304  = _library_map.find(aCompName);
305  if (itLib != _library_map.end()) return itLib->second;
306  }
307 
308  // --- try dlopen C++ component
309 
310  std::string sprefix;
311  if (prefix)
312  sprefix = prefix;
313  else
314  {
315  std::string s = aCompName + "_ROOT_DIR";
316  toupper(s);
317  const char * t = getenv(s.c_str());
318  sprefix="";
319  if (t)
320  {
321  sprefix = t;
322  sprefix += "/lib/salome";
323  }
324  }
325 
326 #ifndef WIN32
327 #ifdef __APPLE__
328  std::string impl_name = std::string ("lib") + aCompName + std::string("Local.dylib");
329 #else
330  std::string impl_name = std::string ("lib") + aCompName + std::string("Local.so");
331 #endif
332  if(sprefix != "")
333  impl_name = sprefix + std::string("/") + impl_name;
334 #else
335  std::string impl_name = aCompName + std::string("Local.dll");
336  impl_name = sprefix + std::string("\\") + impl_name;
337 #endif
338  DEBTRACE("impl_name = " << impl_name);
339 
340 #if defined( WIN32 )
341  HMODULE handle;
342  handle = dlopen( impl_name.c_str() ) ;
343 #else
344  void* handle;
345  handle = dlopen( impl_name.c_str() , RTLD_LAZY ) ;
346 #endif
347 
348  const char * sError;
349 #if defined( WIN32 )
350  sError = "Not available here !";
351 #endif
352 
353 #if defined( WIN32 )
354  if (!handle)
355 #else
356  sError = dlerror();
357  if ((sError = dlerror()) || !handle)
358 #endif
359  {
360  std::stringstream msg;
361  msg << "Can't load shared library : " << impl_name
362  << " (dlopen error : " << sError << ") at "
363  << __FILE__ << ":" << __LINE__;
364  throw YACS::Exception(msg.str());
365  }
366 
367  void *ihandle, *rhandle, *phandle = NULL, *thandle = NULL;
368 
369  ihandle = dlsym(handle, "__init");
370 #if defined( WIN32 )
371  if (!ihandle)
372 #else
373  if (sError = dlerror())
374 #endif
375  {
376  dlclose(handle);
377  std::stringstream msg;
378  msg << "Library " << impl_name
379  << " doesn't contains initialization function (" << sError << ") at "
380  << __FILE__ << ":" << __LINE__;
381  throw YACS::Exception(msg.str());
382  }
383 
384  rhandle = dlsym(handle, "__run");
385 #if defined( WIN32 )
386  if (!rhandle)
387 #else
388  if (sError = dlerror())
389 #endif
390  {
391  dlclose(handle);
392  std::stringstream msg;
393  msg << "Library " << impl_name
394  << " doesn't contains main switch function (" << sError << ") at "
395  << __FILE__ << ":" << __LINE__;
396  throw YACS::Exception(msg.str());
397  }
398 
399  thandle = dlsym(handle, "__terminate");
400 #if defined( WIN32 )
401  if (!thandle)
402 #else
403  if (sError = dlerror())
404 #endif
405  {
406  dlclose(handle);
407  std::stringstream msg;
408  msg << "Library " << impl_name
409  << " doesn't contains terminate function (" << sError << ") at "
410  << __FILE__ << ":" << __LINE__;
411  throw YACS::Exception(msg.str());
412  }
413  phandle = dlsym(handle, "__ping");
414 
415  _library_map[aCompName] = LocalLibrary(handle, (InitFunction) ihandle,
416  (RunFunction) rhandle,
417  (PingFunction) phandle,
418  (TerminateFunction) thandle);
419  return _library_map[aCompName];
420 }
void LocalContainer::unLoadComponentLibrary ( const std::string &  aCompName)

Definition at line 422 of file CppContainer.cxx.

References _library_map.

Referenced by loadComponentLibrary().

423 {
424  std::map<std::string, LocalLibrary >::iterator itLib
425  = _library_map.find(aCompName);
426 
427  if (itLib == _library_map.end()) return;
428 
429  dlclose(itLib->second.handle);
430  _library_map.erase(itLib);
431 
432 }
void LocalContainer::unregisterComponentInstance ( CppComponent C)

Definition at line 281 of file CppContainer.cxx.

References _instance_map, _instance_mapMutex, and YACS::ENGINE::ComponentInstance::getCompoName().

Referenced by YACS::ENGINE::CppContainer::unregisterComponentInstance().

282 {
283  _instance_mapMutex.lock(); // lock to be alone
284  _instance_map.erase(C->getCompoName());
285  _instance_mapMutex.unLock(); // unlock
286 }

Friends And Related Function Documentation

friend class CppComponent
friend

Definition at line 73 of file CppContainer.hxx.

Referenced by createComponentInstance().

Member Data Documentation

std::multimap< std::string, CppComponent * > LocalContainer::_instance_map
staticprotected

Definition at line 94 of file CppContainer.hxx.

Referenced by createComponentInstance(), destroy(), and unregisterComponentInstance().

YACS::BASES::Mutex YACS::ENGINE::LocalContainer::_instance_mapMutex
protected

Definition at line 92 of file CppContainer.hxx.

Referenced by createComponentInstance(), destroy(), and unregisterComponentInstance().

std::map< std::string, LocalLibrary > LocalContainer::_library_map
staticprotected
YACS::BASES::Mutex YACS::ENGINE::LocalContainer::_library_mapMutex
protected

Definition at line 92 of file CppContainer.hxx.

Referenced by destroy().

LocalContainer * LocalContainer::_singleton = NULL
staticprivate

Definition at line 98 of file CppContainer.hxx.

Referenced by destroy(), and get().


The documentation for this class was generated from the following files: