// Copyright (C) 2012-2013 ChaosForge / Kornel Kisielewicz // http://chaosforge.org/ // // This file is part of NV Libraries. // For conditions of distribution and use, see copyright notice in nv.hh /** * @file library.hh * @author Kornel Kisielewicz * @brief Implements a dynamic library class */ #ifndef NV_LIBRARY_HH #define NV_LIBRARY_HH #include #include namespace nv { /** * library * @brief Class representing a dynamically loaded library */ class library { public: /** * Library constructor */ library(); /** * Opens the library * * Throws library_error on failure */ void open( const string& name ); /** * Tries to open the library * * returns true if succeeded, false otherwise */ bool try_open( const string& name ); /** * Returns true if the library is open, false otherwise */ bool is_open() const; /** * Returns library name */ const string& get_name() const; /** * Get symbol from library * * Throws on symbol not found */ void* get( const string& symbol ); /** * Get symbol from library * * Returns null if symbol not found */ void* try_get( const string& symbol ); /** * Destructor * * Closes the library if open. */ ~library(); /** * Returns library loading error if present * * Exact implementation depends on platform/compiler. */ static string get_error(); protected: /** * Opens the library * * Opens the library and prepares it for getting symbols. * Needs to be done before any get call. * * returns true on success, false otherwise */ bool open(); /** * @brief Closes the library * * Closes the library. Any further get calls or operations on * received symbols are invalid! */ void close(); /// Library handle void* m_handle; /// Library name string m_name; }; // class Library class library_error : public runtime_error { /// Library name string m_name; public: /** * Constructor */ library_error( const string& message, const string& name ) : runtime_error( "Library (" + name + ") : " + message + " [ " + library::get_error() + " ]"), m_name( name ) { } /** * Destructor. * * It must not throw any exceptions because of the inheritance */ ~library_error() throw() {} /** * Returns library name */ const string& get_name() { return m_name; } }; } // namespace nv #endif // NV_LIBRARY_HH