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