[4] | 1 | // Copyright (C) 2012-2013 ChaosForge / Kornel Kisielewicz
|
---|
| 2 | // http://chaosforge.org/
|
---|
| 3 | //
|
---|
| 4 | // This file is part of NV Libraries.
|
---|
| 5 | // For conditions of distribution and use, see copyright notice in nv.hh
|
---|
| 6 |
|
---|
| 7 | /**
|
---|
| 8 | * @file library.hh
|
---|
| 9 | * @author Kornel Kisielewicz
|
---|
| 10 | * @brief Implements a dynamic library class
|
---|
| 11 | */
|
---|
| 12 |
|
---|
| 13 | #ifndef NV_LIBRARY_HH
|
---|
| 14 | #define NV_LIBRARY_HH
|
---|
| 15 |
|
---|
| 16 | #include <nv/exception.hh>
|
---|
| 17 | #include <string>
|
---|
| 18 |
|
---|
| 19 | namespace nv
|
---|
| 20 | {
|
---|
| 21 | /**
|
---|
| 22 | * library
|
---|
| 23 | * @brief Class representing a dynamically loaded library
|
---|
| 24 | */
|
---|
| 25 | class library
|
---|
| 26 | {
|
---|
| 27 | public:
|
---|
| 28 |
|
---|
| 29 | /**
|
---|
| 30 | * Library constructor
|
---|
| 31 | *
|
---|
| 32 | * Opens the library
|
---|
| 33 | */
|
---|
| 34 | library( const std::string& name );
|
---|
| 35 |
|
---|
| 36 | /**
|
---|
| 37 | * Opens the library
|
---|
| 38 | */
|
---|
| 39 | void open( const std::string& name );
|
---|
| 40 |
|
---|
| 41 | /**
|
---|
| 42 | * Returns library name
|
---|
| 43 | */
|
---|
| 44 | const std::string& get_name() const;
|
---|
| 45 |
|
---|
| 46 | /**
|
---|
| 47 | * Get symbol from library
|
---|
| 48 | */
|
---|
| 49 | void* get( const std::string& symbol );
|
---|
| 50 |
|
---|
| 51 | /**
|
---|
| 52 | * Destructor
|
---|
| 53 | *
|
---|
| 54 | * Closes the library if open.
|
---|
| 55 | */
|
---|
| 56 | ~library();
|
---|
| 57 |
|
---|
| 58 | /**
|
---|
| 59 | * Returns library loading error if present
|
---|
| 60 | *
|
---|
| 61 | * Exact implementation depends on platform/compiler.
|
---|
| 62 | */
|
---|
| 63 | static std::string get_error();
|
---|
| 64 |
|
---|
| 65 | protected:
|
---|
| 66 | /**
|
---|
| 67 | * Opens the library
|
---|
| 68 | *
|
---|
| 69 | * Opens the library and prepares it for getting symbols.
|
---|
| 70 | * Needs to be done before any get call.
|
---|
| 71 | */
|
---|
| 72 | void open();
|
---|
| 73 |
|
---|
| 74 | /**
|
---|
| 75 | * @brief Closes the library
|
---|
| 76 | *
|
---|
| 77 | * Closes the library. Any further get calls or operations on
|
---|
| 78 | * received symbols are invalid!
|
---|
| 79 | */
|
---|
| 80 | void close();
|
---|
| 81 |
|
---|
| 82 | /// Library handle
|
---|
| 83 | void* m_handle;
|
---|
| 84 |
|
---|
| 85 | /// Library name
|
---|
| 86 | std::string m_name;
|
---|
| 87 |
|
---|
| 88 | }; // class Library
|
---|
| 89 |
|
---|
| 90 | class library_error : public runtime_error
|
---|
| 91 | {
|
---|
| 92 | /// Library name
|
---|
| 93 | std::string m_name;
|
---|
| 94 | public:
|
---|
| 95 | /**
|
---|
| 96 | * Constructor
|
---|
| 97 | */
|
---|
| 98 | library_error( const std::string& message, const std::string& name )
|
---|
| 99 | : runtime_error( "Library (" + name + ") : " + message + " [ " + library::get_error() + " ]"), m_name( name )
|
---|
| 100 | {
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | /**
|
---|
| 104 | * Returns library name
|
---|
| 105 | */
|
---|
| 106 | const std::string& get_name()
|
---|
| 107 | {
|
---|
| 108 | return m_name;
|
---|
| 109 | }
|
---|
| 110 | };
|
---|
| 111 |
|
---|
| 112 | } // namespace nv
|
---|
| 113 |
|
---|
| 114 | #endif // NV_LIBRARY_HH
|
---|