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