Salome HOME
4b5e23b5921a62becc5f50ad46c9a18bd4fe857e
[modules/kernel.git] / src / SALOMEDSClient / SALOMEDSClient_ClientFactory.cxx
1 #include "SALOMEDSClient_ClientFactory.hxx"
2
3 #ifdef WIN32
4 #include <windows.h>
5 static HMODULE _libHandle = 0;
6 #define SALOMEDS_LIB_NAME "SalomeDS.dll"
7 #else
8 #include <dlfcn.h>
9 static void* _libHandle = NULL;
10 #define SALOMEDS_LIB_NAME "libSalomeDS.so"
11 #endif
12
13 #define SOBJECT_FACTORY      "SObjectFactory"
14 #define SCOMPONENT_FACTORY   "SComponentFactory"
15 #define STUDY_FACTORY        "StudyFactory"
16 #define STUDYMANAGER_FACTORY "StudyManagerFactory"
17 #define STUDYMANAGER_CREATE  "CreateStudyManager"
18
19 typedef SALOMEDSClient_SObject* (*SOBJECT_FACTORY_FUNCTION) (SALOMEDS::SObject_ptr);
20 typedef SALOMEDSClient_SComponent* (*SCOMPONENT_FACTORY_FUNCTION) (SALOMEDS::SComponent_ptr);
21 typedef SALOMEDSClient_Study* (*STUDY_FACTORY_FUNCTION) (SALOMEDS::Study_ptr);
22 typedef SALOMEDSClient_StudyManager* (*STUDYMANAGER_FACTORY_FUNCTION) ();
23 typedef SALOMEDSClient_StudyManager* (*STUDYMANAGER_CREATE_FUNCTION) (CORBA::ORB_ptr, PortableServer::POA_ptr);
24
25 static SOBJECT_FACTORY_FUNCTION aSObjectFactory = NULL;
26 static SCOMPONENT_FACTORY_FUNCTION aSComponentFactory = NULL;
27 static STUDY_FACTORY_FUNCTION aStudyFactory = NULL;
28 static STUDYMANAGER_FACTORY_FUNCTION aManagerFactory = NULL;
29 static STUDYMANAGER_CREATE_FUNCTION aCreateFactory = NULL;
30
31 using namespace std;
32
33 _PTR(SObject) ClientFactory::SObject(SALOMEDS::SObject_ptr theSObject)
34 {
35   SALOMEDSClient_SObject* so = NULL;
36
37 #ifdef WIN32
38   if(!_libHandle) _libHandle = ::LoadLibrary(SALOMEDS_LIB_NAME);
39   if(!aSObjectFactory) aSObjectFactory = (SOBJECT_FACTORY_FUNCTION)::GetProcAddress(_libHandle, SOBJECT_FACTORY);
40 #else
41   if(!_libHandle) _libHandle = dlopen(SALOMEDS_LIB_NAME, RTLD_LAZY | RTLD_GLOBAL);
42   if(!aSObjectFactory) aSObjectFactory = (SOBJECT_FACTORY_FUNCTION) dlsym(_libHandle, SOBJECT_FACTORY);
43 #endif
44
45   if(aSObjectFactory) so = aSObjectFactory(theSObject); 
46   return _PTR(SObject)(so);
47 }
48
49 _PTR(SComponent) ClientFactory::SComponent(SALOMEDS::SComponent_ptr theSComponent)
50 {
51   SALOMEDSClient_SComponent* sco = NULL; 
52
53 #ifdef WIN32
54   if(!_libHandle) _libHandle = ::LoadLibrary(SALOMEDS_LIB_NAME);
55   if(!aSComponentFactory) aSComponentFactory = (SCOMPONENT_FACTORY_FUNCTION)::GetProcAddress(_libHandle, SCOMPONENT_FACTORY);
56 #else
57   if(!_libHandle) _libHandle = dlopen(SALOMEDS_LIB_NAME, RTLD_LAZY | RTLD_GLOBAL);
58   if(!aSComponentFactory) aSComponentFactory = (SCOMPONENT_FACTORY_FUNCTION) dlsym(_libHandle, SCOMPONENT_FACTORY);
59 #endif
60
61   if(aSComponentFactory) sco = aSComponentFactory(theSComponent); 
62   return _PTR(SComponent)(sco);
63 }
64
65 _PTR(Study) ClientFactory::Study(SALOMEDS::Study_ptr theStudy)
66 {
67   SALOMEDSClient_Study* study = NULL;
68
69 #ifdef WIN32
70   if(!_libHandle) _libHandle = ::LoadLibrary(SALOMEDS_LIB_NAME);
71   if(!aStudyFactory) aStudyFactory = (STUDY_FACTORY_FUNCTION)::GetProcAddress(_libHandle, STUDY_FACTORY);
72 #else
73   if(!_libHandle) _libHandle = dlopen(SALOMEDS_LIB_NAME, RTLD_LAZY | RTLD_GLOBAL);
74   if(!aStudyFactory) aStudyFactory = (STUDY_FACTORY_FUNCTION) dlsym(_libHandle, STUDY_FACTORY);
75 #endif
76
77   if(aStudyFactory) study = aStudyFactory(theStudy); 
78   return _PTR(Study)(study);
79 }
80
81 _PTR(StudyManager) ClientFactory::StudyManager()
82 {
83   SALOMEDSClient_StudyManager* manager = NULL;
84
85 #ifdef WIN32
86   if(!_libHandle) _libHandle = ::LoadLibrary(SALOMEDS_LIB_NAME);
87   if(!aManagerFactory) aManagerFactory = (STUDYMANAGER_FACTORY_FUNCTION)::GetProcAddress(_libHandle, STUDYMANAGER_FACTORY);
88 #else
89   if(!_libHandle) _libHandle = dlopen(SALOMEDS_LIB_NAME, RTLD_LAZY | RTLD_GLOBAL);
90   if(!aManagerFactory) aManagerFactory = (STUDYMANAGER_FACTORY_FUNCTION) dlsym(_libHandle, STUDYMANAGER_FACTORY);
91 #endif
92
93   if(aManagerFactory) manager = aManagerFactory(); 
94   return _PTR(StudyManager)(manager);
95 }
96
97 _PTR(StudyManager) ClientFactory::createStudyManager(CORBA::ORB_ptr orb, PortableServer::POA_ptr poa)
98 {
99   SALOMEDSClient_StudyManager* manager = NULL;
100
101 #ifdef WIN32
102   if(!_libHandle) _libHandle = ::LoadLibrary(SALOMEDS_LIB_NAME);
103   if(!aCreateFactory) aCreateFactory = (STUDYMANAGER_CREATE_FUNCTION)::GetProcAddress(_libHandle, STUDYMANAGER_CREATE);
104 #else
105   if(!_libHandle) _libHandle = dlopen(SALOMEDS_LIB_NAME, RTLD_LAZY | RTLD_GLOBAL);
106   if(!aCreateFactory) aCreateFactory = (STUDYMANAGER_CREATE_FUNCTION) dlsym(_libHandle, STUDYMANAGER_CREATE);
107 #endif
108
109   if(aCreateFactory)  manager = aCreateFactory(orb, poa);
110   return _PTR(StudyManager)(manager);
111 }
112