[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 | */
|
---|
[109] | 32 | library();
|
---|
[4] | 33 |
|
---|
| 34 | /**
|
---|
| 35 | * Opens the library
|
---|
[166] | 36 | *
|
---|
| 37 | * Throws library_error on failure
|
---|
[4] | 38 | */
|
---|
| 39 | void open( const std::string& name );
|
---|
| 40 |
|
---|
| 41 | /**
|
---|
[166] | 42 | * Tries to open the library
|
---|
| 43 | *
|
---|
| 44 | * returns true if succeeded, false otherwise
|
---|
| 45 | */
|
---|
| 46 | bool try_open( const std::string& name );
|
---|
| 47 |
|
---|
| 48 | /**
|
---|
[109] | 49 | * Returns true if the library is open, false otherwise
|
---|
| 50 | */
|
---|
| 51 | bool is_open() const;
|
---|
| 52 |
|
---|
| 53 | /**
|
---|
[4] | 54 | * Returns library name
|
---|
| 55 | */
|
---|
| 56 | const std::string& get_name() const;
|
---|
| 57 |
|
---|
| 58 | /**
|
---|
| 59 | * Get symbol from library
|
---|
[166] | 60 | *
|
---|
| 61 | * Throws on symbol not found
|
---|
[4] | 62 | */
|
---|
| 63 | void* get( const std::string& symbol );
|
---|
| 64 |
|
---|
| 65 | /**
|
---|
[166] | 66 | * Get symbol from library
|
---|
| 67 | *
|
---|
| 68 | * Returns null if symbol not found
|
---|
| 69 | */
|
---|
| 70 | void* try_get( const std::string& symbol );
|
---|
| 71 |
|
---|
| 72 | /**
|
---|
[4] | 73 | * Destructor
|
---|
| 74 | *
|
---|
| 75 | * Closes the library if open.
|
---|
| 76 | */
|
---|
| 77 | ~library();
|
---|
| 78 |
|
---|
| 79 | /**
|
---|
| 80 | * Returns library loading error if present
|
---|
| 81 | *
|
---|
| 82 | * Exact implementation depends on platform/compiler.
|
---|
| 83 | */
|
---|
| 84 | static std::string get_error();
|
---|
| 85 |
|
---|
| 86 | protected:
|
---|
| 87 | /**
|
---|
| 88 | * Opens the library
|
---|
| 89 | *
|
---|
| 90 | * Opens the library and prepares it for getting symbols.
|
---|
| 91 | * Needs to be done before any get call.
|
---|
[166] | 92 | *
|
---|
| 93 | * returns true on success, false otherwise
|
---|
[4] | 94 | */
|
---|
[166] | 95 | bool open();
|
---|
[4] | 96 |
|
---|
| 97 | /**
|
---|
| 98 | * @brief Closes the library
|
---|
| 99 | *
|
---|
| 100 | * Closes the library. Any further get calls or operations on
|
---|
| 101 | * received symbols are invalid!
|
---|
| 102 | */
|
---|
| 103 | void close();
|
---|
| 104 |
|
---|
| 105 | /// Library handle
|
---|
| 106 | void* m_handle;
|
---|
| 107 |
|
---|
| 108 | /// Library name
|
---|
| 109 | std::string m_name;
|
---|
| 110 |
|
---|
| 111 | }; // class Library
|
---|
| 112 |
|
---|
| 113 | class library_error : public runtime_error
|
---|
| 114 | {
|
---|
| 115 | /// Library name
|
---|
| 116 | std::string m_name;
|
---|
| 117 | public:
|
---|
| 118 | /**
|
---|
| 119 | * Constructor
|
---|
| 120 | */
|
---|
| 121 | library_error( const std::string& message, const std::string& name )
|
---|
| 122 | : runtime_error( "Library (" + name + ") : " + message + " [ " + library::get_error() + " ]"), m_name( name )
|
---|
| 123 | {
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | /**
|
---|
[16] | 127 | * Destructor.
|
---|
| 128 | *
|
---|
| 129 | * It must not throw any exceptions because of the inheritance
|
---|
| 130 | */
|
---|
| 131 | ~library_error() throw() {}
|
---|
| 132 |
|
---|
| 133 | /**
|
---|
[4] | 134 | * Returns library name
|
---|
| 135 | */
|
---|
| 136 | const std::string& get_name()
|
---|
| 137 | {
|
---|
| 138 | return m_name;
|
---|
| 139 | }
|
---|
| 140 | };
|
---|
| 141 |
|
---|
| 142 | } // namespace nv
|
---|
| 143 |
|
---|
| 144 | #endif // NV_LIBRARY_HH
|
---|