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 <nv/string.hh>
|
---|
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 | library();
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Opens the library
|
---|
36 | *
|
---|
37 | * Throws library_error on failure
|
---|
38 | */
|
---|
39 | void open( const string& name );
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Tries to open the library
|
---|
43 | *
|
---|
44 | * returns true if succeeded, false otherwise
|
---|
45 | */
|
---|
46 | bool try_open( const string& name );
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Returns true if the library is open, false otherwise
|
---|
50 | */
|
---|
51 | bool is_open() const;
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * Returns library name
|
---|
55 | */
|
---|
56 | const string& get_name() const;
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * Get symbol from library
|
---|
60 | *
|
---|
61 | * Throws on symbol not found
|
---|
62 | */
|
---|
63 | void* get( const string& symbol );
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * Get symbol from library
|
---|
67 | *
|
---|
68 | * Returns null if symbol not found
|
---|
69 | */
|
---|
70 | void* try_get( const string& symbol );
|
---|
71 |
|
---|
72 | /**
|
---|
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 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.
|
---|
92 | *
|
---|
93 | * returns true on success, false otherwise
|
---|
94 | */
|
---|
95 | bool open();
|
---|
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 | string m_name;
|
---|
110 |
|
---|
111 | }; // class Library
|
---|
112 |
|
---|
113 | class library_error : public runtime_error
|
---|
114 | {
|
---|
115 | /// Library name
|
---|
116 | string m_name;
|
---|
117 | public:
|
---|
118 | /**
|
---|
119 | * Constructor
|
---|
120 | */
|
---|
121 | library_error( const string& message, const string& name )
|
---|
122 | : runtime_error( "Library (" + name + ") : " + message + " [ " + library::get_error() + " ]"), m_name( name )
|
---|
123 | {
|
---|
124 | }
|
---|
125 |
|
---|
126 | /**
|
---|
127 | * Destructor.
|
---|
128 | *
|
---|
129 | * It must not throw any exceptions because of the inheritance
|
---|
130 | */
|
---|
131 | ~library_error() throw() {}
|
---|
132 |
|
---|
133 | /**
|
---|
134 | * Returns library name
|
---|
135 | */
|
---|
136 | const string& get_name()
|
---|
137 | {
|
---|
138 | return m_name;
|
---|
139 | }
|
---|
140 | };
|
---|
141 |
|
---|
142 | } // namespace nv
|
---|
143 |
|
---|
144 | #endif // NV_LIBRARY_HH
|
---|