1 #ifndef _library__loadlib__hpp__included__
2 #define _library__loadlib__hpp__included__
9 #if defined(_WIN32) || defined(_WIN64)
15 threads::lock
& global_mutex();
24 * Construct a NULL library.
33 * Parameter filename: The name of file.
34 * Throws std::bad_alloc: Not enough memory.
35 * Throws std::runtime_error: Error loading shared library.
37 library(const std::string
& filename
) throw(std::bad_alloc
, std::runtime_error
)
39 lib
= new internal(filename
);
46 threads::alock
h(global_mutex());
47 if(lib
&& !--lib
->refs
)
53 * Parameter symbol: The symbol to look up.
54 * Returns: The symbol value.
55 * Throws std::bad_alloc: Not enough memory.
56 * Throws std::runtime_error: Error looking up the symbol.
58 void* operator[](const std::string
& symbol
) const throw(std::bad_alloc
, std::runtime_error
)
60 threads::alock
h(global_mutex());
61 if(!lib
) throw std::runtime_error("Symbol '" + symbol
+ "' not found");
62 return (*lib
)[symbol
];
65 * See what libraries are called on this platform.
67 * Returns: The name of library.
69 static const std::string
& name() throw();
71 * See what standard library extension is on this platform.
73 * Returns: The extension of library.
75 static const std::string
& extension() throw();
79 library(const library
& r
)
81 threads::alock
h(global_mutex());
85 library
& operator=(const library
& r
)
89 threads::alock
h(global_mutex());
90 if(lib
&& !--lib
->refs
)
96 std::string
get_libname() const { return lib
->libname
; }
100 internal(const std::string
& filename
) throw(std::bad_alloc
, std::runtime_error
);
102 void* operator[](const std::string
& symbol
) const throw(std::bad_alloc
, std::runtime_error
);
103 internal(const internal
&);
104 internal
& operator=(const internal
&);
105 #if defined(_WIN32) || defined(_WIN64)
107 #elif !defined(NO_DLFCN)
121 template<typename T
, typename
... U
> struct fntype
{ typedef T(*t
)(U
...); };
132 * Construct a module from list of symbols.
134 module(std::initializer_list
<symbol
> symbols
, std::function
<void(const module
&)> init_fn
);
136 * Construct a module from library.
142 module(const module
& mod
);
150 void* operator[](const std::string
& symbol
) const throw(std::bad_alloc
, std::runtime_error
);
152 * Variable symbol lookup.
154 template<typename T
> T
* var(const std::string
& symbol
) const throw(std::bad_alloc
, std::runtime_error
)
156 return (T
*)(*this)[symbol
];
159 * Function symbol lookup.
161 template<typename T
, typename
... U
> typename fntype
<T
, U
...>::t
fn(const std::string
& symbol
) const
162 throw(std::bad_alloc
, std::runtime_error
)
164 return (typename fntype
<T
, U
...>::t
)(*this)[symbol
];
169 std::string
get_libname() const { return libname
; }
171 * Run all not ran initialization functions.
173 static void run_initializers();
177 std::map
<std::string
, void*> symbols
;
178 std::function
<void(const module
&)> init
;