Back to index

salome-kernel  6.5.0
SALOMEDSClient_ClientFactory.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 #include "SALOMEDSClient_ClientFactory.hxx"
00024 
00025 #ifdef WIN32
00026 #include <windows.h>
00027 static HMODULE _libHandle = 0;
00028 #define SALOMEDS_LIB_NAME "SalomeDS.dll"
00029 #else
00030 #include <dlfcn.h>
00031 static void* _libHandle = NULL;
00032 #define SALOMEDS_LIB_NAME "libSalomeDS.so"
00033 #endif
00034 
00035 #define SOBJECT_FACTORY      "SObjectFactory"
00036 #define SCOMPONENT_FACTORY   "SComponentFactory"
00037 #define STUDY_FACTORY        "StudyFactory"
00038 #define BUILDER_FACTORY      "BuilderFactory"
00039 #define STUDYMANAGER_FACTORY "StudyManagerFactory"
00040 #define STUDYMANAGER_CREATE  "CreateStudyManager"
00041 #define GET_PARAMETERS       "GetIParameters"
00042 #define CONVERT_SOBJECT      "ConvertSObject"
00043 #define CONVERT_STUDY        "ConvertStudy"
00044 #define CONVERT_BUILDER      "ConvertBuilder"
00045 
00046 typedef SALOMEDSClient_SObject* (*SOBJECT_FACTORY_FUNCTION) (SALOMEDS::SObject_ptr);
00047 typedef SALOMEDSClient_SComponent* (*SCOMPONENT_FACTORY_FUNCTION) (SALOMEDS::SComponent_ptr);
00048 typedef SALOMEDSClient_Study* (*STUDY_FACTORY_FUNCTION) (SALOMEDS::Study_ptr);
00049 typedef SALOMEDSClient_StudyManager* (*STUDYMANAGER_FACTORY_FUNCTION) ();
00050 typedef SALOMEDSClient_StudyBuilder* (*BUILDER_FACTORY_FUNCTION) (SALOMEDS::StudyBuilder_ptr);
00051 typedef SALOMEDSClient_StudyManager* (*STUDYMANAGER_CREATE_FUNCTION) (CORBA::ORB_ptr, PortableServer::POA_ptr);
00052 typedef SALOMEDSClient_IParameters* (*GET_PARAMETERS_FACTORY) (const _PTR(AttributeParameter)&);
00053 typedef SALOMEDS::SObject_ptr (*CONVERT_SOBJECT_FUNCTION) (const _PTR(SObject)&);
00054 typedef SALOMEDS::Study_ptr (*CONVERT_STUDY_FUNCTION) (const _PTR(Study)&);
00055 typedef SALOMEDS::StudyBuilder_ptr (*CONVERT_BUILDER_FUNCTION) (const _PTR(StudyBuilder)&);
00056 
00057 
00058 static SOBJECT_FACTORY_FUNCTION aSObjectFactory = NULL;
00059 static SCOMPONENT_FACTORY_FUNCTION aSComponentFactory = NULL;
00060 static STUDY_FACTORY_FUNCTION aStudyFactory = NULL;
00061 static BUILDER_FACTORY_FUNCTION aBuilderFactory = NULL;
00062 static STUDYMANAGER_FACTORY_FUNCTION aManagerFactory = NULL;
00063 static STUDYMANAGER_CREATE_FUNCTION aCreateFactory = NULL;
00064 static GET_PARAMETERS_FACTORY aGetIParameters = NULL;
00065 static CONVERT_SOBJECT_FUNCTION aConvertSObject = NULL;
00066 static CONVERT_STUDY_FUNCTION aConvertStudy = NULL;
00067 static CONVERT_BUILDER_FUNCTION aConvertBuilder = NULL;
00068 
00069 _PTR(SObject) ClientFactory::SObject(SALOMEDS::SObject_ptr theSObject)
00070 {
00071   SALOMEDSClient_SObject* so = NULL;
00072 
00073 #ifdef WIN32
00074   if(!_libHandle) _libHandle = ::LoadLibrary(SALOMEDS_LIB_NAME);
00075   if(!aSObjectFactory) aSObjectFactory = (SOBJECT_FACTORY_FUNCTION)::GetProcAddress(_libHandle, SOBJECT_FACTORY);
00076 #else
00077   if(!_libHandle) _libHandle = dlopen(SALOMEDS_LIB_NAME, RTLD_LAZY | RTLD_GLOBAL);
00078   if(!aSObjectFactory) aSObjectFactory = (SOBJECT_FACTORY_FUNCTION) dlsym(_libHandle, SOBJECT_FACTORY);
00079 #endif
00080 
00081   if(aSObjectFactory) so = aSObjectFactory(theSObject); 
00082   return _PTR(SObject)(so);
00083 }
00084 
00085 _PTR(SComponent) ClientFactory::SComponent(SALOMEDS::SComponent_ptr theSComponent)
00086 {
00087   SALOMEDSClient_SComponent* sco = NULL; 
00088 
00089 #ifdef WIN32
00090   if(!_libHandle) _libHandle = ::LoadLibrary(SALOMEDS_LIB_NAME);
00091   if(!aSComponentFactory) aSComponentFactory = (SCOMPONENT_FACTORY_FUNCTION)::GetProcAddress(_libHandle, SCOMPONENT_FACTORY);
00092 #else
00093   if(!_libHandle) _libHandle = dlopen(SALOMEDS_LIB_NAME, RTLD_LAZY | RTLD_GLOBAL);
00094   if(!aSComponentFactory) aSComponentFactory = (SCOMPONENT_FACTORY_FUNCTION) dlsym(_libHandle, SCOMPONENT_FACTORY);
00095 #endif
00096 
00097   if(aSComponentFactory) sco = aSComponentFactory(theSComponent); 
00098   return _PTR(SComponent)(sco);
00099 }
00100 
00101 _PTR(Study) ClientFactory::Study(SALOMEDS::Study_ptr theStudy)
00102 {
00103   SALOMEDSClient_Study* study = NULL;
00104 
00105 #ifdef WIN32
00106   if(!_libHandle) _libHandle = ::LoadLibrary(SALOMEDS_LIB_NAME);
00107   if(!aStudyFactory) aStudyFactory = (STUDY_FACTORY_FUNCTION)::GetProcAddress(_libHandle, STUDY_FACTORY);
00108 #else
00109   if(!_libHandle) _libHandle = dlopen(SALOMEDS_LIB_NAME, RTLD_LAZY | RTLD_GLOBAL);
00110   if(!aStudyFactory) aStudyFactory = (STUDY_FACTORY_FUNCTION) dlsym(_libHandle, STUDY_FACTORY);
00111 #endif
00112 
00113   if(aStudyFactory) study = aStudyFactory(theStudy); 
00114   return _PTR(Study)(study);
00115 }
00116 
00117 _PTR(StudyBuilder) ClientFactory::StudyBuilder(SALOMEDS::StudyBuilder_ptr theStudyBuilder)
00118 {
00119   SALOMEDSClient_StudyBuilder* studyBuilder = NULL;
00120 
00121 #ifdef WIN32
00122   if(!_libHandle) _libHandle = ::LoadLibrary(SALOMEDS_LIB_NAME);
00123   if(!aBuilderFactory) aBuilderFactory = (BUILDER_FACTORY_FUNCTION)::GetProcAddress(_libHandle, BUILDER_FACTORY);
00124 #else
00125   if(!_libHandle) _libHandle = dlopen(SALOMEDS_LIB_NAME, RTLD_LAZY | RTLD_GLOBAL);
00126   if(!aBuilderFactory) aBuilderFactory = (BUILDER_FACTORY_FUNCTION) dlsym(_libHandle, BUILDER_FACTORY);
00127 #endif
00128 
00129   if(aBuilderFactory) studyBuilder = aBuilderFactory(theStudyBuilder); 
00130   return _PTR(StudyBuilder)(studyBuilder);
00131 }
00132 
00133 _PTR(StudyManager) ClientFactory::StudyManager()
00134 {
00135   SALOMEDSClient_StudyManager* manager = NULL;
00136 
00137 #ifdef WIN32
00138   if(!_libHandle) _libHandle = ::LoadLibrary(SALOMEDS_LIB_NAME);
00139   if(!aManagerFactory) aManagerFactory = (STUDYMANAGER_FACTORY_FUNCTION)::GetProcAddress(_libHandle, STUDYMANAGER_FACTORY);
00140 #else
00141   if(!_libHandle) _libHandle = dlopen(SALOMEDS_LIB_NAME, RTLD_LAZY | RTLD_GLOBAL);
00142   if(!aManagerFactory) aManagerFactory = (STUDYMANAGER_FACTORY_FUNCTION) dlsym(_libHandle, STUDYMANAGER_FACTORY);
00143 #endif
00144 
00145   if(aManagerFactory) manager = aManagerFactory(); 
00146   return _PTR(StudyManager)(manager);
00147 }
00148 
00149 _PTR(StudyManager) ClientFactory::createStudyManager(CORBA::ORB_ptr orb, PortableServer::POA_ptr poa)
00150 {
00151   SALOMEDSClient_StudyManager* manager = NULL;
00152 
00153 #ifdef WIN32
00154   if(!_libHandle) _libHandle = ::LoadLibrary(SALOMEDS_LIB_NAME);
00155   if(!aCreateFactory) aCreateFactory = (STUDYMANAGER_CREATE_FUNCTION)::GetProcAddress(_libHandle, STUDYMANAGER_CREATE);
00156 #else
00157   if(!_libHandle) _libHandle = dlopen(SALOMEDS_LIB_NAME, RTLD_LAZY | RTLD_GLOBAL);
00158   if(!aCreateFactory) aCreateFactory = (STUDYMANAGER_CREATE_FUNCTION) dlsym(_libHandle, STUDYMANAGER_CREATE);
00159 #endif
00160 
00161   if(aCreateFactory)  manager = aCreateFactory(orb, poa);
00162   return _PTR(StudyManager)(manager);
00163 }
00164 
00165 _PTR(IParameters) ClientFactory::getIParameters(const _PTR(AttributeParameter)& ap)
00166 {
00167   SALOMEDSClient_IParameters* interface = NULL;
00168 
00169 #ifdef WIN32
00170   if(!_libHandle) _libHandle = ::LoadLibrary(SALOMEDS_LIB_NAME);
00171   if(!aGetIParameters) aGetIParameters = (GET_PARAMETERS_FACTORY)::GetProcAddress(_libHandle, GET_PARAMETERS);
00172 #else
00173   if(!_libHandle) _libHandle = dlopen(SALOMEDS_LIB_NAME, RTLD_LAZY | RTLD_GLOBAL);
00174   if(!aGetIParameters) aGetIParameters = (GET_PARAMETERS_FACTORY) dlsym(_libHandle, GET_PARAMETERS);
00175 #endif
00176 
00177   if(aGetIParameters) interface = aGetIParameters(ap); 
00178   return _PTR(IParameters)(interface);
00179 }
00180 
00181 SALOMEDS::SObject_ptr ClientFactory::crbSObject(const _PTR(SObject)& theSObject)
00182 {
00183   SALOMEDS::SObject_var so;
00184 
00185 #ifdef WIN32
00186   if(!_libHandle) _libHandle = ::LoadLibrary(SALOMEDS_LIB_NAME);
00187   if(!aConvertSObject) aConvertSObject = (CONVERT_SOBJECT_FUNCTION)::GetProcAddress(_libHandle, CONVERT_SOBJECT);
00188 #else
00189   if(!_libHandle) _libHandle = dlopen(SALOMEDS_LIB_NAME, RTLD_LAZY | RTLD_GLOBAL);
00190   if(!aConvertSObject) aConvertSObject = (CONVERT_SOBJECT_FUNCTION) dlsym(_libHandle, CONVERT_SOBJECT);
00191 #endif
00192 
00193   if(aConvertSObject) so = aConvertSObject(theSObject); 
00194 
00195   if(CORBA::is_nil(so)) return SALOMEDS::SObject::_nil();
00196   return so._retn();
00197 }
00198 
00199 
00200 SALOMEDS::Study_ptr ClientFactory::crbStudy(const _PTR(Study)& theStudy)
00201 {
00202   SALOMEDS::Study_var study;
00203 
00204 #ifdef WIN32
00205   if(!_libHandle) _libHandle = ::LoadLibrary(SALOMEDS_LIB_NAME);
00206   if(!aConvertStudy) aConvertStudy = (CONVERT_STUDY_FUNCTION)::GetProcAddress(_libHandle, CONVERT_STUDY);
00207 #else
00208   if(!_libHandle) _libHandle = dlopen(SALOMEDS_LIB_NAME, RTLD_LAZY | RTLD_GLOBAL);
00209   if(!aConvertStudy) aConvertStudy = (CONVERT_STUDY_FUNCTION) dlsym(_libHandle, CONVERT_STUDY);
00210 #endif
00211 
00212   if(aConvertStudy) study = aConvertStudy(theStudy); 
00213   
00214   if(CORBA::is_nil(study)) return SALOMEDS::Study::_nil();
00215   return study._retn();
00216 }
00217 
00218 SALOMEDS::StudyBuilder_ptr ClientFactory::crbStudyBuilder(const _PTR(StudyBuilder)& theStudyBuilder)
00219 {
00220   SALOMEDS::StudyBuilder_var studyBuilder;
00221 
00222 #ifdef WIN32
00223   if(!_libHandle) _libHandle = ::LoadLibrary(SALOMEDS_LIB_NAME);
00224   if(!aConvertBuilder)aConvertBuilder  = (CONVERT_BUILDER_FUNCTION)::GetProcAddress(_libHandle, CONVERT_BUILDER);
00225 #else
00226   if(!_libHandle) _libHandle = dlopen(SALOMEDS_LIB_NAME, RTLD_LAZY | RTLD_GLOBAL);
00227   if(!aConvertBuilder) aConvertBuilder = (CONVERT_BUILDER_FUNCTION) dlsym(_libHandle, CONVERT_BUILDER);
00228 #endif
00229 
00230   if(aConvertBuilder) studyBuilder = aConvertBuilder(theStudyBuilder); 
00231   
00232   if(CORBA::is_nil(studyBuilder)) return SALOMEDS::StudyBuilder::_nil();
00233   return studyBuilder._retn();
00234 }