Salome HOME
Changes in source code within porting on CentOS 6.3
[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_Data()
11 #include <ModelAPI_Data.h>
12 // to avoid unresolved ModelAPI_Plugin()
13 #include <ModelAPI_Plugin.h>
14 // to avoid unresolved ModelAPI_Iterator()
15 #include <ModelAPI_Iterator.h>
16 // to avoid unresolved ModelAPI_Iterator()
17 #include <ModelAPI_Iterator.h>
18 #include <ModelAPI_Attribute.h>
19 #include <ModelAPI_AttributeDocRef.h>
20 #include <ModelAPI_AttributeDouble.h>
21
22 #ifdef WIN32
23 #include <windows.h>
24 #else
25 #include <dlfcn.h>
26 #endif
27
28 using namespace std;
29
30 /// Converts library name to the operation system file name
31 string library(const string& theLibName);
32
33 /// Manager that will be initialized from Model package, one per application
34 std::shared_ptr<ModelAPI_PluginManager> MY_MANAGER;
35
36 ModelAPI_PluginManager::ModelAPI_PluginManager()
37 {
38 }
39
40 void ModelAPI_PluginManager::SetPluginManager(
41   std::shared_ptr<ModelAPI_PluginManager> theManager)
42 {
43   MY_MANAGER = theManager;
44 }
45
46 std::shared_ptr<ModelAPI_PluginManager> ModelAPI_PluginManager::get()
47 {
48   if (!MY_MANAGER) { // import Model library that implements this interface of ModelAPI
49     loadLibrary("Model");
50   }
51   return MY_MANAGER;
52 }
53
54 string library(const string& theLibName)
55 {
56   string aLibName = theLibName;
57
58 #ifndef WIN32
59   static string aLibExt( ".so" );
60   if (aLibName.size() < 3 || aLibName.substr(0, 3) !="lib")
61     aLibName = "lib" + aLibName;
62 #else
63   static string aLibExt( ".dll" );
64 #endif
65
66   string anExt = aLibName.substr(aLibName.size() - 4);
67
68   if ( anExt != aLibExt)
69     aLibName += aLibExt;
70
71   return aLibName;
72 }
73
74 // for debug purpose only (cerr), before the error management system is implemented
75 #include <iostream>
76 void ModelAPI_PluginManager::loadLibrary(const string theLibName)
77 {
78   string aFileName = library(theLibName);
79   if ( aFileName.empty() )
80   {
81     cerr<<"Library "<<theLibName.c_str()<<" can not be imported"<<endl;
82     return;
83   }
84
85 #ifdef WIN32
86   HINSTANCE aModLib = ::LoadLibrary( aFileName.c_str() ); 
87   if (!aModLib)
88     cerr<<"Failed to load "<<aFileName.c_str()<<endl;
89 #else
90   void* aModLib = dlopen( aFileName.c_str(), RTLD_LAZY );
91   if ( !aModLib )
92     cerr<<"Failed to load "<<aFileName.c_str()<<endl;
93 #endif
94 }