Cat
DLL.cpp
Go to the documentation of this file.
1 // $Id: $
2 // Include files
3 
4 #include "ProcDataBase.h"
5 #include "Element.h"
6 #include "Processus.h"
7 
8 // local
9 #include "DLL.h"
10 
11 //-----------------------------------------------------------------------------
12 // Implementation file for class : DLL
13 //
14 // 2006-10-24 :
15 //-----------------------------------------------------------------------------
16 
17 //=============================================================================
18 // Standard constructor, initializes variables
19 //=============================================================================
21 m_handle (0)
22 {
23 }
24 //=============================================================================
25 // Destructor
26 //=============================================================================
28 
29 //=========================================================================
30 //
31 //=========================================================================
32 bool DLL::containsObject ( std::string name ) {
33  std::vector<identification>::const_iterator iter;
34  for ( iter=m_listElements.begin() ;
35  iter!=m_listElements.end() ;
36  ++iter ) {
37  if ( 0==name.compare( iter->first ) ){
38  return true;
39  }
40  }
41  for ( iter=m_listProcessus.begin() ;
42  iter!=m_listProcessus.end() ;
43  ++iter ) {
44  if ( 0==name.compare( iter->first ) ){
45  return true;
46  }
47  }
48  return false;
49 }
50 
51 
52 //=========================================================================
53 //
54 //=========================================================================
55 std::vector<std::string> DLL::objectList ( ) {
56  std::vector<std::string> list;
57  std::vector<identification>::const_iterator iter;
58  for ( iter=m_listElements.begin() ;
59  iter!=m_listElements.end() ;
60  ++iter ) {
61  list.push_back ( iter->first );
62  }
63  for ( iter=m_listProcessus.begin() ;
64  iter!=m_listProcessus.end() ;
65  ++iter ) {
66  list.push_back ( iter->first );
67  }
68  return list;
69 }
70 
71 //=========================================================================
72 //
73 //=========================================================================
74 void DLL::print ( ) {
75  char line [100];
76  info("Symbols found in dll "+name()+": "+
77  itos(m_listElements.size())+" Element(s) - "+
78  itos(m_listProcessus.size())+" Processus.");
79  std::vector<identification>::const_iterator iter;
80  if ( m_listElements.size()>0 ) {
81  sprintf(line,"============================================================================================");
82  info(line,"DLL::print");
83  sprintf(line ,"Element Description");
84  info(line,"DLL::print");
85  for ( iter=m_listElements.begin() ;
86  iter!=m_listElements.end() ;
87  ++iter ) {
88  sprintf(line," %25s %65s ",
89  iter->first.c_str(),
90  iter->second.c_str());
91  info(std::string(line),"DLL::print");
92  }
93  }
94  if ( m_listProcessus.size() > 0 ) {
95  sprintf(line,"============================================================================================");
96  info(line,"DLL::print");
97  sprintf (line ,"Processus Target Type");
98  info(line,"DLL::print");
99  for ( iter=m_listProcessus.begin() ;
100  iter!=m_listProcessus.end() ;
101  ++iter ) {
102  sprintf(line," %25s %25s ",
103  iter->first.c_str(),
104  application()->procDb()->processus(iter->first)->type().c_str()
105  // application()->procDb()->processus(iter->first)->title().c_str()
106  );
107  info(std::string(line),"DLL::print");
108  }
109  }
110  sprintf(line,"============================================================================================");
111  info(line,"DLL::print");
112 }
113 
114 
115 //=========================================================================
116 //
117 //=========================================================================
118 StatusCode DLL::load ( std::string library ) {
119  typedef Element* (*func) ();
120 #ifdef WIN32
121  m_handle = LoadLibrary ((library+std::string(".dll")).c_str());
122 #else
123  m_handle = dlopen( (std::string("lib")+library+std::string(".so")).c_str() , RTLD_LAZY );
124 #endif
125  if ( NULL != m_handle ) {
126 
127  // get entry point
128  setName(library);
129  DLL::EntryPoint entryPoint;
130 #ifdef WIN32
131  entryPoint = (DLL::EntryPoint)GetProcAddress(m_handle,
132  (library+std::string("_dll")).c_str());
133 #else
134  entryPoint = (DLL::EntryPoint)dlsym(m_handle,
135  (library+std::string("_dll")).c_str());
136 #endif
137  if ( !entryPoint ) {
138  warning("No entry point found in library "+library+".","DLL::load");
139  // could not find entry point
140 #ifdef WIN32
141  FreeLibrary( m_handle );
142 #else
143  dlclose( m_handle );
144 #endif
145  warning("Could not load dll entry point "+name(),"DLL::load");
146  return StatusCode::FAILURE;
147  }
148  else {
149  debug("Entry point found in library "+library+".","DLL::load");
150  // entry point found : load list of objects
151  entryPoint ( this );
152  // check if we can find constructors/destructors
153  if ( init().isFailure() ) {
154  info("Could not find Constructors/Destructors in library "+
155  library+".","DLL::load");
156  return StatusCode::FAILURE;
157  }
158  print();
159  return StatusCode::SUCCESS;
160  }
161  }
162  else {
163  warning("No handle on library "+library+".","DLL::load");
164  }
165  return StatusCode::FAILURE;
166 }
167 
168 
169 //=========================================================================
170 //
171 //=========================================================================
173  bool initOk = true;
174  std::vector<identification>::const_iterator iter;
175  for ( iter=m_listElements.begin() ;
176  iter!=m_listElements.end() ;
177  ++iter ) {
178  ConstructorElement constructor;
179  DestructorElement destructor;
180 #ifdef WIN32
181  constructor = (ConstructorElement)GetProcAddress(m_handle,
182  (iter->first+
183  std::string("_create")).c_str());
184  destructor = (DestructorElement)GetProcAddress(m_handle,
185  (iter->first+
186  std::string("_destroy")).c_str());
187 #else
188  constructor = (ConstructorElement)dlsym(m_handle,
189  (iter->first+
190  std::string("_create")).c_str());
191  destructor = (DestructorElement)dlsym(m_handle,
192  (iter->first+
193  std::string("_destroy")).c_str());
194 #endif
195  if ( 0 == constructor) {
196  warning("Could not find constructor of "+iter->first+std::string(" (_create)"),"DLL::init");
197  initOk=false;
198  }
199  if ( 0 == destructor) {
200  warning("Could not find destructor of "+iter->first+std::string(" (_destroy)"),"DLL::init");
201  initOk=false;
202  }
203  if (initOk) {
204  m_elementConstructors.push_back( constructor );
205  m_elementDestructors.push_back( destructor );
206  }
207  }
208  if (!initOk){
209  return StatusCode::FAILURE;
210  }
211 
212  initOk=true;
213  for ( iter=m_listProcessus.begin() ;
214  iter!=m_listProcessus.end() ;
215  ++iter ) {
216  ConstructorProcessus constructor;
217  DestructorProcessus destructor;
218 #ifdef WIN32
219  constructor = (ConstructorProcessus)GetProcAddress(m_handle,
220  (iter->first+
221  std::string("_create")).c_str());
222  destructor = (DestructorProcessus)GetProcAddress(m_handle,
223  (iter->first+
224  std::string("_destroy")).c_str());
225 #else
226  constructor = (ConstructorProcessus)dlsym(m_handle,
227  (iter->first+
228  std::string("_create")).c_str());
229  destructor = (DestructorProcessus)dlsym(m_handle,
230  (iter->first+
231  std::string("_destroy")).c_str());
232 #endif
233  if ( 0 == constructor) {
234  warning("Could not find constructor of "+iter->first,"DLL::init");
235  initOk=false;
236  }
237  if ( 0 == destructor) {
238  warning("Could not find destructor of "+iter->first,"DLL::init");
239  initOk=false;
240  }
241  if (initOk){
242  m_processusConstructors.push_back( constructor );
243  m_processusDestructors.push_back( destructor );
244  // create the corresponding processus in the database
245  Processus* proc = createProcessus ( iter->first );
246  if ( 0==proc ){
247  initOk=false;
248  }
249  else {
250  application()->procDb()->add( proc);
251  }
252  }
253  }
254  if (!initOk){
255  return StatusCode::FAILURE;
256  }
257  return StatusCode::SUCCESS;
258 }
259 
260 //=========================================================================
261 //
262 //=========================================================================
264  if ( NULL != m_handle ){
265 #ifdef WIN32
266  FreeLibrary( m_handle );
267 #else
268  dlclose( m_handle );
269 #endif
270  return StatusCode::SUCCESS;
271  }
272  return StatusCode::FAILURE;
273 }
274 
275 //=========================================================================
276 //
277 //=========================================================================
278 Element* DLL::createElement ( std::string name ) {
279  std::vector<identification>::const_iterator iter;
280  std::vector<ConstructorElement>::const_iterator constructor=m_elementConstructors.begin();
281  for ( iter=m_listElements.begin() ;
282  iter!=m_listElements.end() ;
283  ++iter ) {
284  if ( 0==name.compare(iter->first) ){
285  Element *element = (*constructor)() ;
286  element->setDllName(this->name());
287  return ( element );
288  }
289  constructor++;
290  }
291  return NULL;
292 }
293 
294 //=========================================================================
295 //
296 //=========================================================================
298  std::vector<identification>::const_iterator iter;
299  std::vector<ConstructorProcessus>::const_iterator constructor=m_processusConstructors.begin();
300  for ( iter=m_listProcessus.begin() ;
301  iter!=m_listProcessus.end() ;
302  ++iter ) {
303  if ( 0==name.compare(iter->first) ){
304  Processus *processus=(*constructor)();
305  processus->setDllName(this->name());
306  return ( processus );
307  }
308  constructor++;
309  }
310  return NULL;
311 }
312 
313 //=========================================================================
314 //
315 //=========================================================================
317  std::vector<identification>::const_iterator iter;
318  std::vector<DestructorElement>::const_iterator destructor=m_elementDestructors.begin();
319  for ( iter=m_listElements.begin() ;
320  iter!=m_listElements.end() ;
321  ++iter ) {
322  if ( 0==(ptr->type()).compare(iter->first) ){
323  ( (*destructor)( ptr ) );
324  return StatusCode::SUCCESS;
325  }
326  destructor++;
327  }
328  return StatusCode::FAILURE;
329 }
330 
331 //=========================================================================
332 //
333 //=========================================================================
335  std::vector<identification>::const_iterator iter;
336  std::vector<DestructorProcessus>::const_iterator destructor=m_processusDestructors.begin();
337  for ( iter=m_listProcessus.begin() ;
338  iter!=m_listProcessus.end() ;
339  ++iter ) {
340  if ( 0==(ptr->type()).compare(iter->first) ){
341  ( (*destructor)( ptr ) );
342  return StatusCode::SUCCESS;
343  }
344  destructor++;
345  }
346  return StatusCode::FAILURE;
347 }
348 
349 //=============================================================================
std::string itos(int)
Definition: Tools.cpp:46
void setDllName(std::string dllName)
Definition: Object.h:66
std::vector< std::string > objectList()
Definition: DLL.cpp:55
listOfElements m_listElements
Definition: DLL.h:222
listOfProcessus m_listProcessus
Definition: DLL.h:223
StatusCode init()
Definition: DLL.cpp:172
std::vector< ConstructorElement > m_elementConstructors
Definition: DLL.h:225
std::vector< DestructorProcessus > m_processusDestructors
Definition: DLL.h:228
std::vector< DestructorElement > m_elementDestructors
Definition: DLL.h:226
std::string name()
Definition: DLL.h:108
std::vector< ConstructorProcessus > m_processusConstructors
Definition: DLL.h:227
void info(std::string mymsg)
Definition: DLL.h:235
Processus *(* ConstructorProcessus)()
Definition: DLL.h:71
void(* EntryPoint)(DLL *)
Definition: DLL.h:67
StatusCode add(Processus *)
Application * application()
Definition: Tools.cpp:42
void print()
Definition: DLL.cpp:74
virtual ~DLL()
Destructor.
Definition: DLL.cpp:27
Element * createElement(std::string)
Definition: DLL.cpp:278
bool containsObject(std::string)
Definition: DLL.cpp:32
Processus * createProcessus(std::string)
Definition: DLL.cpp:297
Element *(* ConstructorElement)()
Definition: DLL.h:69
DLL()
Standard constructor.
Definition: DLL.cpp:20
void warning(std::string mymsg)
Definition: DLL.h:236
ProcDataBase * procDb()
Definition: Application.h:111
Processus * processus(std::string)
StatusCode destroy(Element *)
Definition: DLL.cpp:316
void debug(std::string mymsg)
Definition: DLL.h:234
std::string type()
Definition: Object.h:29
void setName(std::string name)
Definition: DLL.h:100
void(* DestructorProcessus)(Processus *)
Definition: DLL.h:72
Definition: proc.py:1
void(* DestructorElement)(Element *)
Definition: DLL.h:70
StatusCode load(std::string)
Definition: DLL.cpp:118
ImageHandle m_handle
Definition: DLL.h:221
StatusCode unload()
Definition: DLL.cpp:263