Code Search for Developers
 
 
  

dynamic_library.cpp from osgDesigner at Krugle


Show dynamic_library.cpp syntax highlighted

#include <gen_prog/plugin/dynamic_library.h>


#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>


namespace gen_prog
{
    namespace plugin
    {
        dynamic_library::dynamic_library(const boost::filesystem::path & path, HANDLE handle)
        :   _path(path),
            _handle(handle)
        {
            osg::notify(osg::INFO) << "Opened dynamic_library "<< _path.native_file_string() << std::endl;
        }
    
        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    
            }
        }
        
        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;
        }
    
        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;
        }
    
        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;
        }
    }
}




See more files for this project here

osgDesigner

osgDesigner is a graphical tool used to modify an OpenSceneGraph (OSG) scene using the osgIntrospection framework. OpenSceneGraph developpers will be able to extend osgDesigner at need using (editor | render | osgIntrospection wrapper) plugin system.

Project homepage: http://sourceforge.net/projects/osgdesigner
Programming language(s): C++,Shell Script
License: other

  dynamic_library.cpp