0026371: Implementation of new entities for GD&T
[occt.git] / src / Plugin / Plugin.cxx
CommitLineData
b311480e 1// Created on: 1997-03-06
2// Created by: Mister rmi
3// Copyright (c) 1997-1999 Matra Datavision
973c2be1 4// Copyright (c) 1999-2014 OPEN CASCADE SAS
b311480e 5//
973c2be1 6// This file is part of Open CASCADE Technology software library.
b311480e 7//
d5f74e42 8// This library is free software; you can redistribute it and/or modify it under
9// the terms of the GNU Lesser General Public License version 2.1 as published
973c2be1 10// by the Free Software Foundation, with special exception defined in the file
11// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12// distribution for complete text of the license and disclaimer of any warranty.
b311480e 13//
973c2be1 14// Alternatively, this file may be used under the terms of Open CASCADE
15// commercial license or contractual agreement.
7fd59977 16
42cf5bc1 17
7fd59977 18#include <OSD_SharedLibrary.hxx>
42cf5bc1 19#include <Plugin.hxx>
7fd59977 20#include <Plugin_Failure.hxx>
42cf5bc1 21#include <Plugin_MapOfFunctions.hxx>
22#include <Resource_Manager.hxx>
23#include <Standard_GUID.hxx>
24#include <Standard_Transient.hxx>
7fd59977 25#include <TCollection_AsciiString.hxx>
26
27static Standard_Character tc[1000];
28static Standard_PCharacter thePluginId = tc;
29
30
31//=======================================================================
32//function : Load
33//purpose :
34//=======================================================================
b6c0b841
RL
35Handle(Standard_Transient) Plugin::Load (const Standard_GUID& aGUID,
36 const Standard_Boolean theVerbose)
7fd59977 37{
38
39 aGUID.ToCString(thePluginId);
40 TCollection_AsciiString pid(thePluginId);
41 static Plugin_MapOfFunctions theMapOfFunctions;
42 OSD_Function f;
43
44 if(!theMapOfFunctions.IsBound(pid)) {
45
46 Handle(Resource_Manager) PluginResource = new Resource_Manager("Plugin");
47 TCollection_AsciiString theResource(thePluginId);
48 theResource += ".Location";
49
50 if(!PluginResource->Find(theResource.ToCString())) {
b6c0b841
RL
51 PluginResource = AdditionalPluginMap();
52 if (!PluginResource->Find(theResource.ToCString())) {
53 Standard_SStream aMsg; aMsg << "could not find the resource:";
54 aMsg << theResource.ToCString()<< endl;
55 if (theVerbose)
56 cout << "could not find the resource:"<<theResource.ToCString()<< endl;
57 Plugin_Failure::Raise(aMsg);
58 }
7fd59977 59 }
60
61 TCollection_AsciiString thePluginLibrary("");
62#ifndef WNT
63 thePluginLibrary += "lib";
64#endif
65 thePluginLibrary += PluginResource->Value(theResource.ToCString());
66#ifdef WNT
67 thePluginLibrary += ".dll";
68#elif defined(__APPLE__)
69 thePluginLibrary += ".dylib";
70#elif defined (HPUX) || defined(_hpux)
71 thePluginLibrary += ".sl";
72#else
73 thePluginLibrary += ".so";
74#endif
75 OSD_SharedLibrary theSharedLibrary(thePluginLibrary.ToCString());
76 if(!theSharedLibrary.DlOpen(OSD_RTLD_LAZY)) {
77 TCollection_AsciiString error(theSharedLibrary.DlError());
78 Standard_SStream aMsg; aMsg << "could not open:";
79 aMsg << PluginResource->Value(theResource.ToCString());
80 aMsg << "; reason:";
81 aMsg << error.ToCString();
b6c0b841
RL
82 if (theVerbose)
83 cout << "could not open: " << PluginResource->Value(theResource.ToCString())<< " ; reason: "<< error.ToCString() << endl;
7fd59977 84 Plugin_Failure::Raise(aMsg);
85 }
86 f = theSharedLibrary.DlSymb("PLUGINFACTORY");
87 if( f == NULL ) {
88 TCollection_AsciiString error(theSharedLibrary.DlError());
89 Standard_SStream aMsg; aMsg << "could not find the factory in:";
90 aMsg << PluginResource->Value(theResource.ToCString());
91 aMsg << error.ToCString();
92 Plugin_Failure::Raise(aMsg);
93 }
94 theMapOfFunctions.Bind(pid,f);
95 }
96 else
97 f = theMapOfFunctions(pid);
98
ce8b059a 99 Standard_Transient* (*fp) (const Standard_GUID&) = NULL;
100 fp = (Standard_Transient* (*)(const Standard_GUID&)) f;
7fd59977 101 Handle(Standard_Transient) theServiceFactory = (*fp) (aGUID);
102 return theServiceFactory;
103
104}
105
b6c0b841
RL
106const Handle(Resource_Manager)& Plugin::AdditionalPluginMap()
107{
108 static Handle(Resource_Manager) aMap;
109 if (aMap.IsNull())
110 aMap = new Resource_Manager ("" /*theName*/, Standard_False /*theVerbose*/);
111 return aMap;
112}