Salome HOME
[bos #37406][CEA] Fixed segfault in YACS_YacsRuntimeTest on UB22,FD36 and FD37 by...
[modules/yacs.git] / src / runtime / CppContainer.cxx
index db39ca9aedafec4a222c80b9ed40b1914dd385fd..b88046f103fb842b121c2833170f336862ac624e 100644 (file)
@@ -1,9 +1,9 @@
-// Copyright (C) 2006-2012  CEA/DEN, EDF R&D
+// Copyright (C) 2006-2023  CEA, EDF
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
 // License as published by the Free Software Foundation; either
-// version 2.1 of the License.
+// version 2.1 of the License, or (at your option) any later version.
 //
 // This library is distributed in the hope that it will be useful,
 // but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -19,7 +19,7 @@
 
 #include <iostream>
 #include <sstream>
-#ifdef WNT
+#ifdef WIN32
 #include <windows.h>
 #define dlopen LoadLibrary
 #define dlclose FreeLibrary
@@ -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)
 {
   _trueCont = LocalContainer::get();
 }
 
+void CppContainer::shutdown(int level)
+{
+
+}
+
 Container *CppContainer::clone() const
 {
   if(_isAttachedOnCloning)
@@ -94,66 +106,69 @@ 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)
+    {
+  if (_trueCont)
     {
-       LocalLibrary L = _trueCont->loadComponentLibrary(componentName);
-       return L.good();
+      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 "/";
 }
 
-void CppContainer::checkCapabilityToDealWith(const ComponentInstance *inst) const throw(YACS::Exception)
+void CppContainer::checkCapabilityToDealWith(const ComponentInstance *inst) const
 {
   if(inst->getKind()!=CppComponent::KIND)
     throw Exception("CppContainer::checkCapabilityToDealWith : CppContainer is not able to deal with this type of ComponentInstance.");
@@ -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)
@@ -308,8 +323,12 @@ LocalLibrary  LocalContainer::loadComponentLibrary(const std::string & aCompName
         }
     }
   
-#ifndef WNT
+#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
@@ -318,20 +337,28 @@ LocalLibrary  LocalContainer::loadComponentLibrary(const std::string & aCompName
 #endif
   DEBTRACE("impl_name = " << impl_name);
     
-#if defined( WNT )
+#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 ) ;
 #endif
 
   const char * sError;
-#if defined( WNT )
+#if defined( WIN32 )
   sError = "Not available here !";
 #endif
   
-#if defined( WNT )
+#if defined( WIN32 )
   if (!handle) 
 #else
   sError = dlerror();
@@ -348,7 +375,7 @@ LocalLibrary  LocalContainer::loadComponentLibrary(const std::string & aCompName
   void *ihandle, *rhandle, *phandle = NULL, *thandle = NULL;
       
   ihandle = dlsym(handle, "__init");
-#if defined( WNT )
+#if defined( WIN32 )
   if (!ihandle)
 #else
   if (sError = dlerror()) 
@@ -363,7 +390,7 @@ LocalLibrary  LocalContainer::loadComponentLibrary(const std::string & aCompName
     }
   
   rhandle = dlsym(handle, "__run");
-#if defined( WNT )
+#if defined( WIN32 )
   if (!rhandle)
 #else
   if (sError = dlerror()) 
@@ -378,7 +405,7 @@ LocalLibrary  LocalContainer::loadComponentLibrary(const std::string & aCompName
     }
       
   thandle = dlsym(handle, "__terminate");
-#if defined( WNT )
+#if defined( WIN32 )
   if (!thandle)
 #else
   if (sError = dlerror())