dynamic_library_manager.h from osgDesigner at Krugle
Show dynamic_library_manager.h syntax highlighted
#ifndef __GEN_PROG_PLUGIN_DYNAMIC_LIBRARY_MANAGER_H__
#define __GEN_PROG_PLUGIN_DYNAMIC_LIBRARY_MANAGER_H__ 1
// ** standart include
#include <vector>
#include <memory>
#include <boost/ptr_container/ptr_map.hpp>
// ** logger include
#include <osg/Notify>
// ** thread include
#include <boost/thread/recursive_mutex.hpp>
// ** filesystem include
#include <boost/filesystem/path.hpp>
#include <boost/filesystem/operations.hpp>
#include <osg/ref_ptr>
#include <gen_prog/plugin/dynamic_library.h>
#include <gen_prog/plugin/detail/thread_locker.h>
#include <gen_prog/plugin/detail/dynamic_library_identifier.h>
namespace gen_prog
{
namespace plugin
{
/* the dynamic_Library_manager is load and unload dynamic_library_type
* @\param D : a dynamic library implementation, must provide the following function
* static loadDynamicLibrary(boost::filesystem::path & path);
*
* @\param I : identifier of dynamic library, must provide the following function
* key_type toKey(path_type & dlPath);
* bool validPath(path_type & dlPath);
* bool validKey(key_type & key);
*
* @\param T : the thread_locker, it must be recursive
*
*/
template < typename Identifier = detail::dynamic_library_identifier,
//typename Tracker = detail::dynamic_library_identifier,
typename DynamicLibrary = dynamic_library,
typename ThreadLocker = detail::thread_locker<boost::recursive_mutex, boost::recursive_mutex::scoped_lock> >
class dynamic_library_manager : public ThreadLocker
{
public:
typedef Identifier identifier_type;
//typedef Tracker tracker_type;
typedef DynamicLibrary dynamic_library_type;
typedef ThreadLocker thread_locker_type;
typedef typename dynamic_library_type::file_name_type file_name_type;
typedef typename std::vector<file_name_type> file_name_list;
typedef typename dynamic_library_type::path_type path_type;
typedef typename std::vector<file_name_type> path_list;
typedef typename identifier_type::key_type key_type;
typedef typename std::vector<key_type> key_list;
typedef typename std::map< key_type, osg::ref_ptr<dynamic_library_type> > dynamic_library_container_type;
typedef typename thread_locker_type::scoped_lock_type scoped_lock_type;
bool loadPath(const path_type & dlPath)
{
scoped_lock_type lock(this->toMutex());
// ** check the path validity
key_type dlKey = _identifier.pathToKey(dlPath);
if (_identifier.isValidKey(dlKey) == false) return(false);
// ** check if the dynamic library is already loaded
if (_dlContainer.find(dlKey) != _dlContainer.end()) return (true);
// ** load the dynamic library
dynamic_library_type * dl = dynamic_library_type::loadDynamicLibrary(dlPath, dynamic_library_type::LOCAL);
if (dl == NULL) return (false);
// ** insert the dynamic library
_dlContainer[dlKey] = dl;
return (true);
}
bool unloadPath(const path_type & dlPath)
{
scoped_lock_type lock(this->toMutex());
// ** check the path validity
key_type dlKey = _identifier.pathToKey(dlPath);
if (_identifier.isValidKey(dlKey) == false) return(false);
// ** search the dynamic library
typename dynamic_library_container_type::iterator it = _dlContainer.find(dlKey);
if ((it == _dlContainer.end()) || (it->second->getPath() != dlPath))
{
::osg::notify(osg::WARN) << "osgDesigner Warning : unload request for path " << dlPath.native_file_string() << std::endl
<< " with key \"" << dlKey << "\"but dynamic library not found" << std::endl;
return (false);
}
// ** remove the dynamic library and unload it (implicit in the dynamic_library_type destructor)
_dlContainer.erase(it);
return (true);
}
bool unloadKey(const key_type & dlKey)
{
// ** check the key validity
if (_identifier.isValidKey(dlKey) == false) return(false);
scoped_lock_type lock(this->toMutex());
// ** search the dynamic library
typename dynamic_library_container_type::iterator it = _dlContainer.find(dlKey);
if (it == _dlContainer.end())
{
::osg::notify(osg::WARN) << "osgDesigner Warning : unload request for key " << dlKey << std::endl
<< " but dynamic library not found" << std::endl;
return (false);
}
// ** remove the dynamic library and unload it (implicit in the dynamic_library_type destructor)
_dlContainer.erase(it);
return (true);
}
void clear()
{
scoped_lock_type lock(this->toMutex());
// ** remove all dynamic library and unload it (implicit in the dynamic_library_type destructor)
_dlContainer.clear();
}
bool isLoadedPath(const path_type & dlPath) const
{
scoped_lock_type lock(this->toMutex());
typename dynamic_library_container_type::const_iterator it = _dlContainer.find(pathToKey(dlPath));
return ((it != _dlContainer.end()) && (it->second->getPath() == dlPath));
}
bool isLoadedKey(const key_type & dlKey) const
{
scoped_lock_type lock(this->toMutex());
// ** search the dynamic library
typename dynamic_library_container_type::const_iterator it = _dlContainer.find(dlKey);
return (it != _dlContainer.end());
}
std::size_t getLoadedPathList(path_list & pathList) const
{
scoped_lock_type lock(this);
for (typename dynamic_library_container_type::const_iterator it = _dlContainer.begin(); _dlContainer.end(); ++it)
pathList.push_back(it->second->getPath());
return (pathList.size());
}
std::size_t getLoadedKeyList(key_list & keyList) const
{
scoped_lock_type lock(this->toMutex());
for (typename dynamic_library_container_type::const_iterator it = _dlContainer.begin(); it != _dlContainer.end(); ++it)
keyList.push_back(it->first);
return (keyList.size());
}
//! return the module path for the module key. the module must be loaded
path_type keyToPath(const key_type & dlKey) const
{
scoped_lock_type lock(this->toMutex());
typename dynamic_library_container_type::const_iterator it = _dlContainer.find(dlKey);
return ((it != _dlContainer.end()) ? (it->second->getPath()) : (path_type()));
}
key_type pathToKey(const path_type & dlPath) const
{
scoped_lock_type lock(this->toMutex());
return (_identifier.pathToKey(dlPath));
}
private:
dynamic_library_container_type _dlContainer;
identifier_type _identifier;
};
}
}
#endif // ** __GEN_PROG_PLUGIN_DYNAMIC_LIBRARY_MANAGER_H__ ** //
See more files for this project here