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