]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModelAPI/ModelAPI_PluginManager.cxx
Salome HOME
Features and plugins loading mechanisms
[modules/shaper.git] / src / ModelAPI / ModelAPI_PluginManager.cxx
1 // File:        ModelAPI_PluginManager.hxx
2 // Created:     20 Mar 2014
3 // Author:      Mikhail PONIKAROV
4
5 #include <ModelAPI_PluginManager.h>
6 // to avoid unresolved ModelAPI_Document()
7 #include <ModelAPI_Document.h>
8 // to avoid unresolved ModelAPI_Feature()
9 #include <ModelAPI_Feature.h>
10 // to avoid unresolved ModelAPI_Plugin()
11 #include <ModelAPI_Plugin.h>
12
13 #ifdef WIN32
14 #include <windows.h>
15 #else
16 #include <dlfcn.h>
17 #endif
18
19 using namespace std;
20
21 /// Converts library name to the operation system file name
22 string library(const string& theLibName);
23
24 /// Manager that will be initialized from Model package, one per application
25 boost::shared_ptr<ModelAPI_PluginManager> MY_MANAGER;
26
27 ModelAPI_PluginManager::ModelAPI_PluginManager()
28 {
29 }
30
31 void ModelAPI_PluginManager::SetPluginManager(
32   boost::shared_ptr<ModelAPI_PluginManager> theManager)
33 {
34   MY_MANAGER = theManager;
35 }
36
37 boost::shared_ptr<ModelAPI_PluginManager> ModelAPI_PluginManager::get()
38 {
39   if (!MY_MANAGER) { // import Model library that implements this interface of ModelAPI
40     loadLibrary("Model");
41   }
42   return MY_MANAGER;
43 }
44
45 string library(const string& theLibName)
46 {
47   string aLibName = theLibName;
48
49 #ifndef WIN32
50   static string aLibExt( ".so" );
51   if (aLibName.size() < 3 || aLibName.substr(0, 3) !="lib")
52     aLibName = ".lib" + aLibName;
53 #else
54   static string aLibExt( ".dll" );
55 #endif
56
57   string anExt = aLibName.substr(aLibName.size() - 4);
58
59   if ( anExt != aLibExt)
60     aLibName += aLibExt;
61
62   return aLibName;
63 }
64
65 void ModelAPI_PluginManager::loadLibrary(const string theLibName)
66 {
67   string aFileName = library(theLibName);
68   if ( aFileName.empty() )
69   {
70     cerr<<"Library "<<theLibName.c_str()<<" can not be imported"<<endl;
71     return;
72   }
73
74 #ifdef WIN32
75   HINSTANCE aModLib = ::LoadLibrary( aFileName.c_str() ); 
76   if (!aModLib)
77     cerr<<"Failed to load "<<aFileName.c_str()<<endl;
78 #else
79   void* aModLib = dlopen( aFileName.c_str(), RTLD_LAZY );
80   if ( !aModLib )
81     cerr<<"Failed to load "<<aFileName.c_str()<<endl;
82 #endif
83 }