Version: 9.15.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::LocalContainerget ()
 

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::LocalContainer ( )
protected

Definition at line 182 of file CppContainer.cxx.

183 {
184 }

Referenced by get().

◆ ~LocalContainer()

LocalContainer::~LocalContainer ( )
protectedvirtual

Definition at line 186 of file CppContainer.cxx.

187 {
188  destroy();
189 }

References destroy().

Member Function Documentation

◆ createComponentInstance()

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.

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 }
YACS::BASES::Mutex _instance_mapMutex
static std::multimap< std::string, CppComponent * > _instance_map
void createInternalInstance(const char *componentName, void *&obj, RunFunction &r, TerminateFunction &t)
void(* RunFunction)(void *, const char *, int, int, Any **, Any **, returnInfo *)
void(* TerminateFunction)(void **)

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

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

◆ createInternalInstance()

void LocalContainer::createInternalInstance ( const char *  componentName,
void *&  obj,
RunFunction r,
TerminateFunction t 
)

Definition at line 259 of file CppContainer.cxx.

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 }
LocalLibrary loadComponentLibrary(const std::string &, const char *prefix=NULL, bool forcedLoad=false)
static std::map< std::string, LocalLibrary > _library_map
Proc * p
Definition: driver.cxx:216
void *(* InitFunction)()
void(* PingFunction)()
TerminateFunction terminateHandle

References _library_map, yacsorb.CORBAEngineTest::i, YACS::ENGINE::LocalLibrary::initHandle, loadComponentLibrary(), p, YACS::ENGINE::LocalLibrary::pingHandle, testCppPluginInvokation::r, YACS::ENGINE::LocalLibrary::runHandle, gui.Appli::t, and YACS::ENGINE::LocalLibrary::terminateHandle.

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

◆ destroy()

void LocalContainer::destroy ( )

Definition at line 200 of file CppContainer.cxx.

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 }
static LocalContainer * _singleton
YACS::BASES::Mutex _library_mapMutex

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

Referenced by ~LocalContainer().

◆ get()

LocalContainer * LocalContainer::get ( )
static

Definition at line 191 of file CppContainer.cxx.

192 {
193  if (NULL == _singleton)
194  {
196  }
197  return _singleton;
198 }

References _singleton, and LocalContainer().

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

◆ loadComponentLibrary()

LocalLibrary LocalContainer::loadComponentLibrary ( const std::string &  aCompName,
const char *  prefix = NULL,
bool  forcedLoad = false 
)

Definition at line 293 of file CppContainer.cxx.

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 #if defined(UNICODE)
343  size_t length = strlen(impl_name.c_str()) + sizeof(char);
344  wchar_t* aPath = new wchar_t[length + 1];
345  memset(aPath, '\0', length);
346  mbstowcs(aPath, impl_name.c_str(), length);
347 #else
348  const char* aPath = fullLibName.c_str();
349 #endif
350  handle = dlopen( aPath ) ;
351 #else
352  void* handle;
353  handle = dlopen( impl_name.c_str() , RTLD_LAZY ) ;
354 #endif
355 
356  const char * sError;
357 #if defined( WIN32 )
358  sError = "Not available here !";
359 #endif
360 
361 #if defined( WIN32 )
362  if (!handle)
363 #else
364  sError = dlerror();
365  if ((sError = dlerror()) || !handle)
366 #endif
367  {
368  std::stringstream msg;
369  msg << "Can't load shared library : " << impl_name
370  << " (dlopen error : " << sError << ") at "
371  << __FILE__ << ":" << __LINE__;
372  throw YACS::Exception(msg.str());
373  }
374 
375  void *ihandle, *rhandle, *phandle = NULL, *thandle = NULL;
376 
377  ihandle = dlsym(handle, "__init");
378 #if defined( WIN32 )
379  if (!ihandle)
380 #else
381  if (sError = dlerror())
382 #endif
383  {
384  dlclose(handle);
385  std::stringstream msg;
386  msg << "Library " << impl_name
387  << " doesn't contains initialization function (" << sError << ") at "
388  << __FILE__ << ":" << __LINE__;
389  throw YACS::Exception(msg.str());
390  }
391 
392  rhandle = dlsym(handle, "__run");
393 #if defined( WIN32 )
394  if (!rhandle)
395 #else
396  if (sError = dlerror())
397 #endif
398  {
399  dlclose(handle);
400  std::stringstream msg;
401  msg << "Library " << impl_name
402  << " doesn't contains main switch function (" << sError << ") at "
403  << __FILE__ << ":" << __LINE__;
404  throw YACS::Exception(msg.str());
405  }
406 
407  thandle = dlsym(handle, "__terminate");
408 #if defined( WIN32 )
409  if (!thandle)
410 #else
411  if (sError = dlerror())
412 #endif
413  {
414  dlclose(handle);
415  std::stringstream msg;
416  msg << "Library " << impl_name
417  << " doesn't contains terminate function (" << sError << ") at "
418  << __FILE__ << ":" << __LINE__;
419  throw YACS::Exception(msg.str());
420  }
421  phandle = dlsym(handle, "__ping");
422 
423  _library_map[aCompName] = LocalLibrary(handle, (InitFunction) ihandle,
424  (RunFunction) rhandle,
425  (PingFunction) phandle,
426  (TerminateFunction) thandle);
427  return _library_map[aCompName];
428 }
void toupper(std::string &s)
#define DEBTRACE(msg)
Definition: YacsTrace.hxx:31
void unLoadComponentLibrary(const std::string &aCompName)

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

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

◆ unLoadComponentLibrary()

void LocalContainer::unLoadComponentLibrary ( const std::string &  aCompName)

Definition at line 430 of file CppContainer.cxx.

431 {
432  std::map<std::string, LocalLibrary >::iterator itLib
433  = _library_map.find(aCompName);
434 
435  if (itLib == _library_map.end()) return;
436 
437  dlclose(itLib->second.handle);
438  _library_map.erase(itLib);
439 
440 }

References _library_map.

Referenced by loadComponentLibrary().

◆ unregisterComponentInstance()

void LocalContainer::unregisterComponentInstance ( CppComponent C)

Definition at line 281 of file CppContainer.cxx.

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

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

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

Friends And Related Function Documentation

◆ CppComponent

friend class CppComponent
friend

Definition at line 73 of file CppContainer.hxx.

Referenced by createComponentInstance().

Member Data Documentation

◆ _instance_map

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

Definition at line 94 of file CppContainer.hxx.

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

◆ _instance_mapMutex

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

Definition at line 92 of file CppContainer.hxx.

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

◆ _library_map

std::map< std::string, LocalLibrary > LocalContainer::_library_map
staticprotected

◆ _library_mapMutex

YACS::BASES::Mutex YACS::ENGINE::LocalContainer::_library_mapMutex
protected

Definition at line 92 of file CppContainer.hxx.

Referenced by destroy().

◆ _singleton

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: