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