Show dynamic_library.h syntax highlighted
#ifndef __GEN_PROG_PLUGIN_DYNAMIC_LIBRARY_H__
#define __GEN_PROG_PLUGIN_DYNAMIC_LIBRARY_H__ 1
// ** standart include
#include <string>
// ** 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/Referenced>
#include <gen_prog/gen_prog_config.h>
#include <osgIntrospectionToolKit/Export.h>
#ifdef __APPLE__
#if !defined(MAC_OS_X_VERSION_10_3) || (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_3)
#define APPLE_PRE_10_3
#endif
#elif defined(WIN32) && !defined(__CYGWIN__)
#elif defined(__APPLE__) && defined(APPLE_PRE_10_3)
#else // all other unix
#include <unistd.h>
#ifdef __hpux__
// Although HP-UX has dlopen() it is broken! We therefore need to stick
// to shl_load()/shl_unload()/shl_findsym()
#include <dl.h>
#include <errno.h>
#else
#include <dlfcn.h>
#endif
#endif
namespace gen_prog
{
namespace plugin
{
/** dynamic_library - encapsulates the loading and unloading of dynamic libraries,
*/
class OSGINTROSPECTIONTOOLKIT_EXPORT dynamic_library : public osg::Referenced
{
public:
typedef std::string file_name_type;
typedef boost::filesystem::path path_type;
typedef void* HANDLE;
typedef void* PROC_ADDRESS;
enum ResolutionFlag
{
#if defined(WIN32) && !defined(__CYGWIN__)
DEFERRED = 0,
IMMEDIATE = 0
#elif defined(__APPLE__) && defined(APPLE_PRE_10_3)
DEFERRED = 0,
IMMEDIATE = 0
#elif defined(__hpux__)
DEFERRED = BIND_DEFERRED,
IMMEDIATE = BIND_IMMEDIATE
#else
DEFERRED = RTLD_LAZY,
IMMEDIATE = RTLD_NOW
#endif
};
enum ScopeFlag
{
#if defined(WIN32) && !defined(__CYGWIN__)
GLOBAL = 0,
LOCAL = 0
#elif defined(__APPLE__) && defined(APPLE_PRE_10_3)
GLOBAL = 0,
LOCAL = 0
#elif defined(__hpux__)
GLOBAL = 0,
LOCAL = 0
#else
GLOBAL = RTLD_GLOBAL,
LOCAL = RTLD_LOCAL
#endif
};
~dynamic_library();
/** returns a pointer to a DynamicLibrary object on successfully
* opening of library returns NULL on failure.
*/
static dynamic_library * loadDynamicLibrary(const path_type & dlPath, ScopeFlag sf = GLOBAL, ResolutionFlag rf = DEFERRED);
/** return file name of a path. */
static file_name_type pathToFileName(const path_type & dlPath) { return (dlPath.leaf()); }
/** return file name of library.*/
file_name_type getFileName() const { return (dynamic_library::pathToFileName(_path)); }
/** return path of library.*/
const path_type & getPath() const { return (_path); }
/** return handle to .so/.dll dynamic library itself.*/
HANDLE getHandle() const { return _handle; }
/** return address of function located in library.*/
PROC_ADDRESS getProcAddress(const std::string & procName);
protected:
/** get handle to library file */
static HANDLE getLibraryHandle(const path_type & dlPath, ScopeFlag sf, ResolutionFlag rf);
/** disallow default constructor.*/
dynamic_library();
/** disallow copy constructor.*/
dynamic_library(const dynamic_library &);
/** disallow copy operator.*/
dynamic_library & operator = (const dynamic_library &) { return *this; }
/** Disallow public construction so that users have to go
* through loadLibrary() above which returns NULL on
* failure, a valid DynamicLibrary object on success.
*/
dynamic_library(const path_type & dlPath, HANDLE handle);
path_type _path;
HANDLE _handle;
};
//
//
//#if defined(WIN32) && !defined(__CYGWIN__)
// #include <Io.h>
// #include <Windows.h>
// #include <Winbase.h>
//#elif defined(__APPLE__) && defined(APPLE_PRE_10_3)
// #include <mach-o/dyld.h>
////#else // all other unix
////#include <unistd.h>
////#ifdef __hpux__
//// Although HP-UX has dlopen() it is broken! We therefore need to stick
//// to shl_load()/shl_unload()/shl_findsym()
////#include <dl.h>
////#include <errno.h>
////#else
////#include <dlfcn.h>
////#endif
//#endif
//
//
//// ** logger include
//#include <osg/Notify>
//
//
//
// inline dynamic_library::dynamic_library(const boost::filesystem::path & path, HANDLE handle)
// : osg::Referenced(),
// _path(path),
// _handle(handle)
// {
// osg::notify(osg::INFO) << "Opened dynamic_library "<< _path.native_file_string() << std::endl;
// }
//
// inline dynamic_library::~dynamic_library()
// {
// if (_handle)
// {
// osg::notify(osg::INFO) << "Closing dynamic library "<< _path.native_file_string() << std::endl;
// #if defined(WIN32) && !defined(__CYGWIN__)
// FreeLibrary((HMODULE)_handle);
// #elif defined(__APPLE__) && defined(APPLE_PRE_10_3)
// NSUnLinkModule(static_cast<NSModule>(_handle), FALSE);
// #elif defined(__hpux__)
// // fortunately, shl_t is a pointer
// shl_unload (static_cast<shl_t>(_handle));
// #else // other unix
// dlclose(_handle);
// #endif
// }
// }
//
// inline dynamic_library * dynamic_library::loadDynamicLibrary(const boost::filesystem::path & dlPath, ScopeFlag sf, ResolutionFlag rf)
// {
// HANDLE handle = NULL;
//
// handle = getLibraryHandle(dlPath, sf, rf);
//
// if (handle) return (new dynamic_library(dlPath, handle));
//
// osg::notify(osg::INFO) << "dynamic library failed loading \""<< dlPath.native_file_string() << "\"" << std::endl;
//
// return NULL;
// }
//
// inline dynamic_library::HANDLE dynamic_library::getLibraryHandle(const boost::filesystem::path & dlPath, ScopeFlag sf, ResolutionFlag rf)
// {
// HANDLE handle = NULL;
//
// #if defined(WIN32) && !defined(__CYGWIN__)
// handle = LoadLibrary(dlPath.native_file_string().data());
//
// #elif defined(__APPLE__) && defined(APPLE_PRE_10_3)
// NSObjectFileImage image;
// // NSModule os_handle = NULL;
// if (NSCreateObjectFileImageFromFile(dlPath.native_file_string().data(), &image) == NSObjectFileImageSuccess)
// {
// // os_handle = NSLinkModule(image, libraryName.c_str(), TRUE);
// handle = NSLinkModule(image, dlPath.native_file_string().data(), TRUE);
// NSDestroyObjectFileImage(image);
// }
//
// #elif defined(__hpux__)
// // BIND_FIRST is neccessary for some reason
// handle = shl_load(dlPath.native_file_string().data(), rf|BIND_FIRST|BIND_VERBOSE, 0);
// return handle;
//
// #else // other unix
// handle = dlopen(dlPath.native_file_string().data(), rf | sf);
// if( handle == NULL )
// osg::notify(osg::INFO) << "dynamic_library::getLibraryHandle( "<< dlPath.native_file_string() << ") - dlopen(): " << dlerror() << std::endl;
// #endif
//
// return handle;
// }
//
// inline dynamic_library::PROC_ADDRESS dynamic_library::getProcAddress(const std::string& procName)
// {
// if (_handle==NULL) return NULL;
//
// #if defined(WIN32) && !defined(__CYGWIN__)
// return (dynamic_library::PROC_ADDRESS)GetProcAddress( (HMODULE)_handle,
// procName.c_str() );
// #elif defined(__APPLE__) && defined(APPLE_PRE_10_3)
// std::string temp("_");
// NSSymbol symbol;
// temp += procName; // Mac OS X prepends an underscore on function names
// symbol = NSLookupSymbolInModule(static_cast<NSModule>(_handle), temp.c_str());
// return NSAddressOfSymbol(symbol);
//
// #elif defined(__hpux__)
// void* result = NULL;
// if (shl_findsym (reinterpret_cast<shl_t*>(&_handle), procName.c_str(), TYPE_PROCEDURE, result) == 0)
// {
// return result;
// }
// else
// {
// osg::notify(osg::WARN) << "dynamic_library::failed looking up " << procName << std::endl;
// osg::notify(osg::WARN) << "dynamic_library::error " << strerror(errno) << std::endl;
// return NULL;
// }
//
// #else // other unix
// void* sym = dlsym( _handle, procName.c_str() );
// if (!sym) {
// osg::notify(osg::WARN) << "dynamic_library::failed looking up " << procName << std::endl;
// osg::notify(osg::WARN) << "dynamic_library::error " << dlerror() << std::endl;
// }
// return sym;
// #endif
//
// return NULL;
// }
}
}
#endif // ** __GEN_PROG_PLUGIN_DYNAMIC_LIBRARY_H__ ** //
See more files for this project here