Salome HOME
Avoid copy of buffer at unpickling time by using PyMemoryView_FromMemory instead...
[modules/yacs.git] / src / runtime / CppContainer.cxx
index c2d151e85cbbc2273bcad1d4d332b82fd56773ad..4b32ba08c915fd98918574a38b69cf94433b4a2e 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2006-2014  CEA/DEN, EDF R&D
+// Copyright (C) 2006-2019  CEA/DEN, EDF R&D
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
@@ -37,6 +37,8 @@
 
 using namespace YACS::ENGINE;
 
+char CppContainer::KIND[]="Cpp";
+
 //#define _DEVDEBUG_
 #include "YacsTrace.hxx"
 
@@ -70,19 +72,29 @@ void CppContainer::lock()
 
 void CppContainer::unLock()
 {
-  _mutex.unlock();
+  _mutex.unLock();
+}
+
+std::string CppContainer::getKind() const
+{
+  return KIND;
 }
 
-bool CppContainer::isAlreadyStarted(const ComponentInstance *inst) const
+bool CppContainer::isAlreadyStarted(const Task *askingNode) const
 {
   return NULL != _trueCont;
 }
 
-void CppContainer::start(const ComponentInstance *inst) throw (YACS::Exception)
+void CppContainer::start(const Task *askingNode) throw (YACS::Exception)
 {
   _trueCont = LocalContainer::get();
 }
 
+void CppContainer::shutdown(int level)
+{
+
+}
+
 Container *CppContainer::clone() const
 {
   if(_isAttachedOnCloning)
@@ -94,61 +106,64 @@ Container *CppContainer::clone() const
     return new CppContainer(*this);
 }
 
-bool CppContainer::loadComponentLibrary(const std::string & componentName) throw (YACS::Exception)
+Container *CppContainer::cloneAlways() const
 {
-    if (_trueCont) 
+  return new CppContainer(*this);
+}
+
+bool CppContainer::loadComponentLibrary(const std::string & componentName) throw (YACS::Exception)
     {
-       LocalLibrary L = _trueCont->loadComponentLibrary(componentName);
-       return L.good();
+  if (_trueCont)
+    {
+      LocalLibrary L = _trueCont->loadComponentLibrary(componentName);
+      return L.good();
     }
-    else 
+  else
     {
-       std::string mesg = "CppContainer not started";
-       throw YACS::Exception(mesg);
+      std::string mesg = "CppContainer not started";
+      throw YACS::Exception(mesg);
+    }
+  return false;
     }
-    return false;
-}
 
-CppComponent * CppContainer::createComponentInstance(const std::string & componentName, int /* studyID */)
+CppComponent * CppContainer::createComponentInstance(const std::string & componentName)
 {
-    DEBTRACE("CppContainer::createComponentInstance");
-    if (_trueCont)
-       return _trueCont->createComponentInstance(componentName.c_str());
-    else 
+  DEBTRACE("CppContainer::createComponentInstance");
+  if (_trueCont)
+    return _trueCont->createComponentInstance(componentName.c_str());
+  else
     {
-       std::string mesg = "CppContainer not started";
-       throw YACS::Exception(mesg);
+      std::string mesg = "CppContainer not started";
+      throw YACS::Exception(mesg);
     }
 }
 
 void CppContainer::createInternalInstance(const std::string & name, void *&obj, 
                                           RunFunction &r, TerminateFunction &t)
 {
-   DEBTRACE("CppContainer::createInternalInstance");
-   if (_trueCont)
-      _trueCont->createInternalInstance(name.c_str(), obj, r, t);
-   else 
-   {
-     std::string mesg = "CppContainer not started";
-     throw YACS::Exception(mesg);
-   }
+  DEBTRACE("CppContainer::createInternalInstance");
+  if (_trueCont)
+    _trueCont->createInternalInstance(name.c_str(), obj, r, t);
+  else
+    {
+      std::string mesg = "CppContainer not started";
+      throw YACS::Exception(mesg);
+    }
 }
 
-void CppContainer::unregisterComponentInstance(CppComponent * C)
+void CppContainer::unregisterComponentInstance(CppComponent *compo)
 {
-    if (_trueCont) 
-    {
-       _trueCont->unregisterComponentInstance(C);
-    }
+  if (_trueCont)
+    _trueCont->unregisterComponentInstance(compo);
 }
 
 
-std::string CppContainer::getPlacementId(const ComponentInstance *inst) const
+std::string CppContainer::getPlacementId(const Task *askingNode) const
 {
   return "/";
 }
 
-std::string CppContainer::getFullPlacementId(const ComponentInstance *inst) const
+std::string CppContainer::getFullPlacementId(const Task *askingNode) const
 {
   return "/";
 }
@@ -184,31 +199,31 @@ LocalContainer * LocalContainer::get()
 
 void LocalContainer::destroy()
 {
-    if (NULL == _singleton)
-      return;
-    
-    // destroy all component instances
-    _instance_mapMutex.lock(); // lock
-    std::multimap<std::string, CppComponent *>::iterator iI, iJ;
-    for (iI=_instance_map.begin(); iI != _instance_map.end(); iI = iJ)
+  if (NULL == _singleton)
+    return;
+
+  // destroy all component instances
+  _instance_mapMutex.lock(); // lock
+  std::multimap<std::string, CppComponent *>::iterator iI, iJ;
+  for (iI=_instance_map.begin(); iI != _instance_map.end(); iI = iJ)
     {
       iJ = iI++;
       iI->second->setContainer(NULL);
       delete iI->second;
     }
-    _instance_map.clear();
-    _instance_mapMutex.unlock(); // unlock
-    
-    // unload all dynamic libraries
-    _library_mapMutex.lock();
-    std::map<std::string, LocalLibrary>::iterator iL;
-    for (iL=_library_map.begin(); iL != _library_map.end(); iL++)
-        dlclose(iL->second.handle);
-    _library_map.clear();
-    _library_mapMutex.unlock();
-    
-    delete _singleton;
-    _singleton = NULL;
+  _instance_map.clear();
+  _instance_mapMutex.unLock(); // unlock
+
+  // unload all dynamic libraries
+  _library_mapMutex.lock();
+  std::map<std::string, LocalLibrary>::iterator iL;
+  for (iL=_library_map.begin(); iL != _library_map.end(); iL++)
+    dlclose(iL->second.handle);
+  _library_map.clear();
+  _library_mapMutex.unLock();
+
+  delete _singleton;
+  _singleton = NULL;
 }
 
 
@@ -237,7 +252,7 @@ CppComponent * LocalContainer::createComponentInstance(const char * name)
   C = new CppComponent(o, r, t, name);
   _instance_mapMutex.lock(); // lock to be alone 
   _instance_map.insert(std::pair<std::string, CppComponent *>(name, C));
-  _instance_mapMutex.unlock(); // unlock
+  _instance_mapMutex.unLock(); // unlock
   return C;
 }
 
@@ -265,14 +280,14 @@ void LocalContainer::createInternalInstance(const char *name, void *&obj,
 
 void LocalContainer::unregisterComponentInstance(CppComponent * C)
 {
-    _instance_mapMutex.lock(); // lock to be alone 
-    _instance_map.erase(C->getCompoName());
-    _instance_mapMutex.unlock(); // unlock
+  _instance_mapMutex.lock(); // lock to be alone
+  _instance_map.erase(C->getCompoName());
+  _instance_mapMutex.unLock(); // unlock
 }
 
 inline void toupper (std::string & s)
 {
-   transform (s.begin (), s.end (), s.begin (), (int(*)(int)) toupper);
+  transform (s.begin (), s.end (), s.begin (), (int(*)(int)) toupper);
 }
 
 LocalLibrary  LocalContainer::loadComponentLibrary(const std::string & aCompName, const char * prefix, bool forcedLoad)
@@ -309,7 +324,11 @@ LocalLibrary  LocalContainer::loadComponentLibrary(const std::string & aCompName
     }
   
 #ifndef WIN32
+#ifdef __APPLE__
+  std::string impl_name = std::string ("lib") + aCompName + std::string("Local.dylib");
+#else
   std::string impl_name = std::string ("lib") + aCompName + std::string("Local.so");
+#endif
   if(sprefix != "")
     impl_name = sprefix + std::string("/") + impl_name;
 #else
@@ -320,7 +339,15 @@ LocalLibrary  LocalContainer::loadComponentLibrary(const std::string & aCompName
     
 #if defined( WIN32 )
   HMODULE handle;
-  handle = dlopen( impl_name.c_str() ) ;
+#if defined(UNICODE)
+  size_t length = strlen(impl_name.c_str()) + sizeof(char);
+  wchar_t* aPath = new wchar_t[length + 1];
+  memset(aPath, '\0', length);
+  mbstowcs(aPath, impl_name.c_str(), length);
+#else
+  const char* aPath = fullLibName.c_str();
+#endif
+  handle = dlopen( aPath ) ;
 #else
   void* handle;
   handle = dlopen( impl_name.c_str() , RTLD_LAZY ) ;