source: trunk/nv/library.hh @ 256

Last change on this file since 256 was 256, checked in by epyon, 11 years ago
  • various minor fixes
File size: 2.6 KB
RevLine 
[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>
[256]17#include <nv/string.hh>
[4]18
19namespace 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                 */
[256]39                void open( const string& name );
[4]40
41                /**
[166]42                 * Tries to open the library
43                 *
44                 * returns true if succeeded, false otherwise
45                 */
[256]46                bool try_open( const string& name );
[166]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                 */
[256]56                const string& get_name() const;
[4]57
58                /**
59                 * Get symbol from library
[166]60                 *
61                 * Throws on symbol not found
[4]62                 */
[256]63                void* get( const string& symbol );
[4]64
65                /**
[166]66                 * Get symbol from library
67                 *
68                 * Returns null if symbol not found
69                 */
[256]70                void* try_get( const string& symbol );
[166]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                 */
[256]84                static string get_error();
[4]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
[256]109                string m_name;
[4]110
111        };  // class Library
112
113        class library_error : public runtime_error
114        {
115                /// Library name
[256]116                string m_name;
[4]117        public:
118                /**
119                 * Constructor
120                 */
[256]121                library_error( const string& message, const string& name )
[4]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                 */
[256]136                const string& get_name()
[4]137                {
138                        return m_name;
139                }
140        };
141
142} // namespace nv
143
144#endif // NV_LIBRARY_HH
Note: See TracBrowser for help on using the repository browser.