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