]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModelAPI/ModelAPI_PluginManager.cxx
Salome HOME
Remove Boost shared_ptr, use std instead
[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 std::shared_ptr<ModelAPI_PluginManager> MY_MANAGER;
26
27 ModelAPI_PluginManager::ModelAPI_PluginManager()
28 {
29 }
30
31 void ModelAPI_PluginManager::SetPluginManager(
32   std::shared_ptr<ModelAPI_PluginManager> theManager)
33 {
34   MY_MANAGER = theManager;
35 }
36
37 std::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 #include <iostream>
66 void ModelAPI_PluginManager::loadLibrary(const string theLibName)
67 {
68   string aFileName = library(theLibName);
69   if ( aFileName.empty() )
70   {
71     cerr<<"Library "<<theLibName.c_str()<<" can not be imported"<<endl;
72     return;
73   }
74
75 #ifdef WIN32
76   HINSTANCE aModLib = ::LoadLibrary( aFileName.c_str() ); 
77   if (!aModLib)
78     cerr<<"Failed to load "<<aFileName.c_str()<<endl;
79 #else
80   void* aModLib = dlopen( aFileName.c_str(), RTLD_LAZY );
81   if ( !aModLib )
82     cerr<<"Failed to load "<<aFileName.c_str()<<endl;
83 #endif
84 }