PlasCom2  1.0
XPACC Multi-physics simluation application
DLTools.H
Go to the documentation of this file.
1 #ifndef _DL_TOOLS_H_
7 #define _DL_TOOLS_H_
8 #include <dlfcn.h>
9 #include <string>
10 #include <sstream>
11 
12 namespace ix {
13 
14  namespace sys {
15 
16  typedef void* dlHandleType;
17 
18  template<typename ObjectType>
19  class ObjectFactory {
20  protected:
21  ObjectType* (*_create)();
22  void (*_destroy)(ObjectType*);
23  public:
24  ObjectFactory() : _create(NULL), _destroy(NULL)
25  {};
26  virtual ObjectType* Create(){return((*_create)()); };
27  virtual ObjectType* (*CreatePtr())(){return(_create); };
28  virtual void (*DestroyPtr())(ObjectType*){return(_destroy); };
29  virtual void Destroy(ObjectType *objptr){(*_destroy)(objptr); };
30  virtual void SetCreate(ObjectType* (*increate)()){_create = increate;};
31  virtual void SetDestroy(void (*indestroy)(ObjectType*)){_destroy = indestroy; };
32  virtual ~ObjectFactory(){};
33  };
34 
35  template<typename ObjectType>
36  class DynamicObjectLib : public std::pair<std::string,dlHandleType>
37  {
38  protected:
39  std::string _path;
41  public:
42  virtual dlHandleType Handle() const { return(this->second); };
43  virtual std::string Name() const { return(this->first); };
44  virtual std::string File() const { return(_path); };
45  virtual std::string &Name() { return(this->first); };
46  virtual std::string &File() { return(_path); };
47  virtual std::string Error(){ return(std::string(dlerror())); };
48  virtual ObjectFactory<ObjectType> &Factory() { return(_factory); };
49  virtual ObjectFactory<ObjectType> Factory() const { return(_factory); };
50  virtual ObjectType* CreateObject(const std::string &objectname)
51  {
52  ObjectType* retval = static_cast<ObjectType*>(NULL);
53  if(this->second){
54  std::ostringstream Ostr;
55  Ostr << "create_" << objectname;
56  _factory.SetCreate((ObjectType * (*)())dlsym(this->second,Ostr.str().c_str()));
57  if(_factory.CreatePtr() != NULL)
58  retval = _factory.Create();
59  }
60  return(retval);
61  };
62  virtual void DestroyObject(const std::string &objectname,ObjectType *objptr)
63  {
64  if(this->second){
65  std::ostringstream Ostr;
66  Ostr << "destroy_" << objectname;
67  _factory.SetDestroy((void (*)(ObjectType *))dlsym(this->second,Ostr.str().c_str()));
68  if(_factory.DestroyPtr() != NULL)
69  _factory.Destroy(objptr);
70  }
71  };
72  virtual int Load(const std::string &name,const std::string &path)
73  {
74  void *handle = dlopen(path.c_str(),RTLD_LAZY);
75  if(handle){
76  if(this->second)
77  dlclose(this->second);
78  this->first = name;
79  this->second = handle;
80  }
81  else
82  return(1);
83  return(0);
84  };
85  virtual void Unload()
86  {
87  if(this->second)
88  dlclose(this->second);
89  this->second = NULL;
90  this->first.erase();
91  _path.erase();
92  _factory.SetCreate(NULL);
93  _factory.SetDestroy(NULL);
94  };
95  virtual ~DynamicObjectLib(){ if(this->second) dlclose(this->second);};
96  };
97  };
98 };
99 #endif
virtual void SetDestroy(void(*indestroy)(ObjectType *))
Definition: DLTools.H:31
virtual ObjectType * Create()
Definition: DLTools.H:26
virtual std::string & Name()
Definition: DLTools.H:45
ObjectType *(* _create)()
Definition: DLTools.H:21
void * dlHandleType
Definition: DLTools.H:16
virtual void Destroy(ObjectType *objptr)
Definition: DLTools.H:29
virtual ~DynamicObjectLib()
Definition: DLTools.H:95
Defines MPI-specific parallel global and program classes.
virtual int Load(const std::string &name, const std::string &path)
Definition: DLTools.H:72
virtual void Unload()
Definition: DLTools.H:85
virtual ~ObjectFactory()
Definition: DLTools.H:32
virtual ObjectType * CreateObject(const std::string &objectname)
Definition: DLTools.H:50
virtual void SetCreate(ObjectType *(*increate)())
Definition: DLTools.H:30
virtual ObjectType *(*)() CreatePtr()
Definition: DLTools.H:27
virtual std::string Name() const
Definition: DLTools.H:43
virtual ObjectFactory< ObjectType > & Factory()
Definition: DLTools.H:48
virtual std::string & File()
Definition: DLTools.H:46
virtual std::string Error()
Definition: DLTools.H:47
virtual void DestroyObject(const std::string &objectname, ObjectType *objptr)
Definition: DLTools.H:62
virtual dlHandleType Handle() const
Definition: DLTools.H:42
virtual std::string File() const
Definition: DLTools.H:44
virtual void(*)(ObjectType *) DestroyPtr()
Definition: DLTools.H:28
ObjectFactory< ObjectType > _factory
Definition: DLTools.H:40
virtual ObjectFactory< ObjectType > Factory() const
Definition: DLTools.H:49
void(* _destroy)(ObjectType *)
Definition: DLTools.H:22