cisst-saw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
osaDynamicLoaderAndFactory.h
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ex: set filetype=cpp softtabstop=4 shiftwidth=4 tabstop=4 cindent expandtab: */
3 
4 /*
5 
6  Author(s): Peter Kazanzides
7  Created on: 2007-01-16
8 
9  (C) Copyright 2007-2007 Johns Hopkins University (JHU), All Rights
10  Reserved.
11 
12 --- begin cisst license - do not edit ---
13 
14 This software is provided "as is" under an open source license, with
15 no warranty. The complete license can be found in license.txt and
16 http://www.cisst.org/cisst/license.txt.
17 
18 --- end cisst license ---
19 */
20 
21 
27 #ifndef _osaDynamicLoaderAndFactory_h
28 #define _osaDynamicLoaderAndFactory_h
29 
30 #include <typeinfo>
33 
34 // This class serves as the base class for osaDynamicLoaderAndFactory and is not
35 // intended to be accessed otherwise. Note that the constructor is protected.
37 {
38 protected:
39  void* create;
40  void* destroy;
41 
42  osaDynamicLoaderAndFactoryBase() : create(0), destroy(0) {}
43  bool Init(const char* name, const char* file, const char* path,
44  const std::type_info& tinfo, int version);
45 
46 public:
48 
49  void Reset();
50 };
51 
74 template <class BaseClass>
76 {
77  typedef BaseClass* CreateFunc();
78  typedef void DestroyFunc(BaseClass*);
79 
80 public:
86 
96  bool Init(const char *name, const char *file, const char *path = 0)
97  { return osaDynamicLoaderAndFactoryBase::Init(name, file, path, typeid(BaseClass), BaseClass::VERSION); }
98 
104  bool Init(const char* name)
105  { return osaDynamicLoaderAndFactoryBase::Init(name, name, 0, typeid(BaseClass), BaseClass::VERSION); }
106 
110  void Reset()
112 
115  BaseClass* CreateObject() const
116  { return create ? (*(reinterpret_cast<CreateFunc*>(create)))() : 0; }
117 
121  void DestroyObject(BaseClass* obj) const
122  { if (destroy) (*reinterpret_cast<DestroyFunc*>(destroy))(obj); }
123 };
124 
125 
126 #endif // _osaDynamicLoaderAndFactory_h
#define CISST_EXPORT
Definition: cmnExportMacros.h:50
void Reset()
Definition: osaDynamicLoaderAndFactory.h:110
osaDynamicLoaderAndFactory()
Definition: osaDynamicLoaderAndFactory.h:82
Dynamically load a shared library file and find the factory methods in the file.
Definition: osaDynamicLoaderAndFactory.h:75
Definition: osaDynamicLoaderAndFactory.h:36
virtual ~osaDynamicLoaderAndFactoryBase()
Definition: osaDynamicLoaderAndFactory.h:47
Macros to export the symbols of cisstOSAbstraction (in a Dll).
Declaration of osaDynamicLoader.
void * create
Definition: osaDynamicLoaderAndFactory.h:39
osaDynamicLoaderAndFactoryBase()
Definition: osaDynamicLoaderAndFactory.h:42
void * destroy
Definition: osaDynamicLoaderAndFactory.h:40
bool Init(const char *name, const char *file, const char *path, const std::type_info &tinfo, int version)
void DestroyObject(BaseClass *obj) const
Definition: osaDynamicLoaderAndFactory.h:121
virtual ~osaDynamicLoaderAndFactory()
Definition: osaDynamicLoaderAndFactory.h:85
BaseClass * CreateObject() const
Definition: osaDynamicLoaderAndFactory.h:115
bool Init(const char *name)
Definition: osaDynamicLoaderAndFactory.h:104
bool Init(const char *name, const char *file, const char *path=0)
Definition: osaDynamicLoaderAndFactory.h:96
Dynamically load a shared library file.
Definition: osaDynamicLoader.h:55