10 #ifndef __PION_PLUGIN_HEADER__ 11 #define __PION_PLUGIN_HEADER__ 17 #include <boost/noncopyable.hpp> 18 #include <boost/thread/once.hpp> 19 #include <boost/thread/mutex.hpp> 20 #include <boost/filesystem/path.hpp> 21 #include <pion/config.hpp> 22 #include <pion/error.hpp> 41 const std::string& name)
43 return find_file(path_to_file, name, PION_PLUGIN_EXTENSION);
54 const std::string& name)
56 return find_file(path_to_file, name, PION_CONFIG_EXTENSION);
67 static void add_static_entry_point(
const std::string& plugin_name,
79 static void check_cygwin_path(boost::filesystem::path& final_path,
80 const std::string& path_string);
83 static void add_plugin_directory(
const std::string& dir);
86 static void reset_plugin_directories(
void);
90 virtual ~
plugin() { release_data(); }
93 inline bool is_open(
void)
const {
return (m_plugin_data != NULL); }
97 return (is_open() ? m_plugin_data->m_plugin_name : std::string());
101 static void get_all_plugin_names(std::vector<std::string>& plugin_names);
115 void open(
const std::string& plugin_name);
129 void open_file(
const std::string& plugin_file);
132 inline void close(
void) { release_data(); }
143 : m_lib_handle(NULL), m_create_func(NULL), m_destroy_func(NULL),
146 data_type(
const std::string& plugin_name)
147 : m_lib_handle(NULL), m_create_func(NULL), m_destroy_func(NULL),
148 m_plugin_name(plugin_name), m_references(0)
184 return (is_open() ? m_plugin_data->m_create_func : NULL);
189 return (is_open() ? m_plugin_data->m_destroy_func : NULL);
193 void release_data(
void);
196 void grab_data(
const plugin& p);
202 typedef std::map<std::string, data_type*> map_type;
207 std::vector<std::string> m_plugin_dirs;
210 map_type m_plugin_map;
213 boost::mutex m_plugin_mutex;
218 static inline config_type& get_plugin_config(
void) {
219 boost::call_once(plugin::create_plugin_config, m_instance_flag);
220 return *m_config_ptr;
224 static void create_plugin_config(
void);
235 static bool find_file(std::string& path_to_file,
const std::string& name,
236 const std::string& extension);
248 static bool check_for_file(std::string& final_path,
const std::string& start_path,
249 const std::string& name,
const std::string& extension);
257 static void open_plugin(
const std::string& plugin_file,
261 static std::string get_plugin_name(
const std::string& plugin_file);
264 static void *load_dynamic_library(
const std::string& plugin_file);
267 static void close_dynamic_library(
void *lib_handle);
270 static void *get_library_symbol(
void *lib_handle,
const std::string& symbol);
274 static const std::string PION_PLUGIN_CREATE;
277 static const std::string PION_PLUGIN_DESTROY;
280 static const std::string PION_PLUGIN_EXTENSION;
283 static const std::string PION_CONFIG_EXTENSION;
286 static boost::once_flag m_instance_flag;
289 static config_type * m_config_ptr;
300 template <
typename InterfaceClassType>
326 inline InterfaceClassType *
create(
void) {
327 CreateObjectFunction *create_func =
328 (CreateObjectFunction*)(get_create_function());
329 if (create_func == NULL)
331 return create_func();
335 inline void destroy(InterfaceClassType *object_ptr) {
340 DestroyObjectFunction* f_;
342 Cast.v_ = get_destroy_function();
343 DestroyObjectFunction *destroy_func = Cast.f_;
344 if (destroy_func == NULL)
346 destroy_func(object_ptr);
354 template <
typename InterfaceClassType>
356 private boost::noncopyable
368 if (m_instance_ptr) {
369 m_plugin_ptr.destroy(m_instance_ptr);
374 inline void create(
const std::string& plugin_type) {
376 m_plugin_ptr.open(plugin_type);
377 m_instance_ptr = m_plugin_ptr.create();
381 inline bool empty(
void)
const {
return m_instance_ptr==NULL; }
384 inline InterfaceClassType *
get(void) {
return m_instance_ptr; }
387 inline InterfaceClassType&
operator*(
void) {
return *m_instance_ptr; }
390 inline const InterfaceClassType&
operator*(
void)
const {
return *m_instance_ptr; }
393 inline InterfaceClassType*
operator->(
void) {
return m_instance_ptr; }
396 inline const InterfaceClassType*
operator->(
void)
const {
return m_instance_ptr; }
422 #ifdef PION_STATIC_LINKING 424 #define PION_DECLARE_PLUGIN(plugin_name) \ 426 extern "C" plugin_name *pion_create_##plugin_name(void); \ 427 extern "C" void pion_destroy_##plugin_name(plugin_name *plugin_ptr); \ 428 static pion::static_entry_point_helper helper_##plugin_name(#plugin_name, (void*) pion_create_##plugin_name, (void*) pion_destroy_##plugin_name); 431 class static_entry_point_helper {
433 static_entry_point_helper(
const std::string& name,
void *create,
void *destroy)
441 #define PION_DECLARE_PLUGIN(plugin_name) static bool find_config_file(std::string &path_to_file, const std::string &name)
void * m_lib_handle
symbol library loaded from a shared object file
void destroy(InterfaceClassType *object_ptr)
destroys an instance of the plug-in object
bool empty(void) const
returns true if pointer is empty
void DestroyObjectFunction(InterfaceClassType *)
data type for a function that is used to destroy object instances
const InterfaceClassType * operator->(void) const
return a const reference to the instance
void * m_create_func
function used to create instances of the plug-in object
static bool find_plugin_file(std::string &path_to_file, const std::string &name)
plugin_ptr< InterfaceClassType > m_plugin_ptr
smart pointer that manages the plugin's dynamic object code
std::string m_plugin_name
the name of the plugin (must be unique per process)
InterfaceClassType * m_instance_ptr
raw pointer to the plugin instance
void * m_destroy_func
function used to destroy instances of the plug-in object
void reset(void)
reset the instance pointer
plugin(const plugin &p)
copy constructor
void * get_destroy_function(void)
returns a pointer to the plug-in's "destroy object" function
void close(void)
closes plug-in library
InterfaceClassType * operator->(void)
return a reference to the instance
exception thrown if a plugin has an undefined state
std::string get_plugin_name(void) const
returns the name of the plugin that is currently open
InterfaceClassType * create(void)
creates a new instance of the plug-in object
plugin_ptr(void)
default constructor & destructor
InterfaceClassType & operator*(void)
return a reference to the instance
void * get_create_function(void)
returns a pointer to the plug-in's "create object" function
data_type(void)
default constructors for convenience
unsigned long m_references
number of references to this class
virtual ~plugin_instance_ptr()
virtual destructor / may be extended
const InterfaceClassType & operator*(void) const
return a const reference to the instance
bool is_open(void) const
returns true if a shared library is loaded/open
plugin(void)
default constructor is private (use plugin_ptr class to create objects)
InterfaceClassType * CreateObjectFunction(void)
data type for a function that is used to create object instances
plugin_instance_ptr(void)
default constructor & destructor
void create(const std::string &plugin_type)
create a new instance of the given plugin_type
static void add_static_entry_point(const std::string &plugin_name, void *create_func, void *destroy_func)
plugin & operator=(const plugin &p)
assignment operator
plugin_ptr(const plugin_ptr &p)
copy constructor
plugin_ptr & operator=(const plugin_ptr &p)
assignment operator