10 #ifndef __PION_PIONPLUGIN_HEADER__ 11 #define __PION_PIONPLUGIN_HEADER__ 17 #include <boost/thread/mutex.hpp> 18 #include <boost/filesystem/path.hpp> 19 #include <pion/PionConfig.hpp> 20 #include <pion/PionException.hpp> 34 virtual const char* what()
const throw() {
35 return "Plug-in was not loaded properly";
64 :
PionException(
"Plug-in library does not include create() symbol: ", file) {}
71 :
PionException(
"Plug-in library does not include destroy() symbol: ", file) {}
82 const std::string& name)
84 return findFile(path_to_file, name, PION_PLUGIN_EXTENSION);
95 const std::string& name)
97 return findFile(path_to_file, name, PION_CONFIG_EXTENSION);
107 static bool findStaticEntryPoint(
const std::string& plugin_name,
109 void **destroy_func);
118 static void addStaticEntryPoint(
const std::string& plugin_name,
130 static void checkCygwinPath(boost::filesystem::path& final_path,
131 const std::string& path_string);
134 static void addPluginDirectory(
const std::string& dir);
137 static void resetPluginDirectories(
void);
144 inline bool is_open(
void)
const {
return (m_plugin_data != NULL); }
148 return (is_open() ? m_plugin_data->m_plugin_name : std::string());
152 static void getAllPluginNames(std::vector<std::string>& plugin_names);
166 void open(
const std::string& plugin_name);
180 void openFile(
const std::string& plugin_file);
190 void openStaticLinked(
const std::string& plugin_name,
195 inline void close(
void) { releaseData(); }
206 : m_lib_handle(NULL), m_create_func(NULL), m_destroy_func(NULL),
210 : m_lib_handle(NULL), m_create_func(NULL), m_destroy_func(NULL),
211 m_plugin_name(plugin_name), m_references(0)
247 return (is_open() ? m_plugin_data->m_create_func : NULL);
252 return (is_open() ? m_plugin_data->m_destroy_func : NULL);
256 void releaseData(
void);
265 class StaticEntryPoint
268 StaticEntryPoint(
const std::string& name,
void *create,
void *destroy)
269 : m_plugin_name(name), m_create_func(create), m_destroy_func(destroy)
271 std::string m_plugin_name;
272 void * m_create_func;
273 void * m_destroy_func;
277 typedef std::list<StaticEntryPoint> StaticEntryPointList;
280 typedef std::map<std::string, PionPluginData*> PluginMap;
292 static bool findFile(std::string& path_to_file,
const std::string& name,
293 const std::string& extension);
305 static bool checkForFile(std::string& final_path,
const std::string& start_path,
306 const std::string& name,
const std::string& extension);
314 static void openPlugin(
const std::string& plugin_file,
318 static std::string getPluginName(
const std::string& plugin_file);
321 static void *loadDynamicLibrary(
const std::string& plugin_file);
324 static void closeDynamicLibrary(
void *lib_handle);
327 static void *getLibrarySymbol(
void *lib_handle,
const std::string& symbol);
331 static const std::string PION_PLUGIN_CREATE;
334 static const std::string PION_PLUGIN_DESTROY;
337 static const std::string PION_PLUGIN_EXTENSION;
340 static const std::string PION_CONFIG_EXTENSION;
343 static std::vector<std::string> m_plugin_dirs;
346 static PluginMap m_plugin_map;
349 static boost::mutex m_plugin_mutex;
352 static StaticEntryPointList *m_entry_points_ptr;
363 template <
typename InterfaceClassType>
389 inline InterfaceClassType *
create(
void) {
390 CreateObjectFunction *create_func =
391 (CreateObjectFunction*)(getCreateFunction());
392 if (create_func == NULL)
394 return create_func();
398 inline void destroy(InterfaceClassType *object_ptr) {
399 DestroyObjectFunction *destroy_func =
400 (DestroyObjectFunction*)(getDestroyFunction());
401 if (destroy_func == NULL)
403 destroy_func(object_ptr);
421 #ifdef PION_STATIC_LINKING 423 #define PION_DECLARE_PLUGIN(plugin_name) \ 425 extern "C" plugin_name *pion_create_##plugin_name(void); \ 426 extern "C" void pion_destroy_##plugin_name(plugin_name *plugin_ptr); \ 427 static pion::StaticEntryPointHelper helper_##plugin_name(#plugin_name, pion_create_##plugin_name, pion_destroy_##plugin_name); 430 class StaticEntryPointHelper {
432 StaticEntryPointHelper(
const std::string& name,
void *create,
void *destroy)
440 #define PION_DECLARE_PLUGIN(plugin_name) void * getCreateFunction(void)
returns a pointer to the plug-in's "create object" function
PionPluginPtr & operator=(const PionPluginPtr &p)
assignment operator
PionPlugin(void)
default constructor is private (use PionPluginPtr class to create objects)
exception thrown if the plug-in directory does not exist
void * m_lib_handle
symbol library loaded from a shared object file
exception thrown if the plug-in file cannot be opened
InterfaceClassType * CreateObjectFunction(void)
data type for a function that is used to create object instances
void * m_create_func
function used to create instances of the plug-in object
std::string getPluginName(void) const
returns the name of the plugin that is currently open
PionPluginData(void)
default constructors for convenience
void DestroyObjectFunction(InterfaceClassType *)
data type for a function that is used to destroy object instances
void close(void)
closes plug-in library
PionPluginPtr(const PionPluginPtr &p)
copy constructor
PionPlugin(const PionPlugin &p)
copy constructor
bool is_open(void) const
returns true if a shared library is loaded/open
static bool findPluginFile(std::string &path_to_file, const std::string &name)
PionPlugin & operator=(const PionPlugin &p)
assignment operator
static void addStaticEntryPoint(const std::string &plugin_name, void *create_func, void *destroy_func)
exception thrown if the plug-in file cannot be found
exception thrown if the plug-in file cannot be opened
PionPluginPtr(void)
default constructor & destructor
void destroy(InterfaceClassType *object_ptr)
destroys an instance of the plug-in object
InterfaceClassType * create(void)
creates a new instance of the plug-in object
the following enables use of the lock-free cache
void * m_destroy_func
function used to destroy instances of the plug-in object
exception thrown if a plug-in library is missing the destroy() function
void * getDestroyFunction(void)
returns a pointer to the plug-in's "destroy object" function
exception thrown if a plug-in library is missing the create() function
unsigned long m_references
number of references to this class
static bool findConfigFile(std::string &path_to_file, const std::string &name)
std::string m_plugin_name
the name of the plugin (must be unique per process)