1 #ifndef _library__loadlib__hpp__included__
2 #define _library__loadlib__hpp__included__
13 threads::lock
& global_mutex();
22 * Construct a NULL library.
31 * Parameter filename: The name of file.
32 * Throws std::bad_alloc: Not enough memory.
33 * Throws std::runtime_error: Error loading shared library.
35 library(const std::string
& filename
) throw(std::bad_alloc
, std::runtime_error
)
39 lib
= new internal(filename
);
51 threads::alock
h(global_mutex());
52 if(lib
&& !--lib
->refs
)
58 * Parameter symbol: The symbol to look up.
59 * Returns: The symbol value.
60 * Throws std::bad_alloc: Not enough memory.
61 * Throws std::runtime_error: Error looking up the symbol.
63 void* operator[](const std::string
& symbol
) const throw(std::bad_alloc
, std::runtime_error
)
65 threads::alock
h(global_mutex());
66 if(!lib
) throw std::runtime_error("Symbol '" + symbol
+ "' not found");
67 return (*lib
)[symbol
];
70 * See what libraries are called on this platform.
72 * Returns: The name of library.
74 static const std::string
& name() throw();
76 * See what standard library extension is on this platform.
78 * Returns: The extension of library.
80 static const std::string
& extension() throw();
84 library(const library
& r
)
86 threads::alock
h(global_mutex());
90 library
& operator=(const library
& r
)
94 threads::alock
h(global_mutex());
95 if(lib
&& !--lib
->refs
)
101 std::string
get_libname() const { return lib
->libname
; }
102 void mark(const void* obj
) const { if(lib
) lib
->mark(obj
); }
103 bool is_marked(const void* obj
) const { return lib
? lib
->is_marked(obj
) : false; }
105 * Get currently loading library, or NULL if nothing is loading.
107 static library
* loading() throw();
109 void set_loading(library
* lib
) throw(std::bad_alloc
);
112 internal(const std::string
& filename
) throw(std::bad_alloc
, std::runtime_error
);
114 void* operator[](const std::string
& symbol
) const throw(std::bad_alloc
, std::runtime_error
);
115 internal(const internal
&);
116 internal
& operator=(const internal
&);
120 void mark(const void* obj
) { marked
.insert(obj
); }
121 bool is_marked(const void* obj
) { return marked
.count(obj
); }
122 std::set
<const void*> marked
;
124 mutable internal
* lib
;
132 template<typename T
, typename
... U
> struct fntype
{ typedef T(*t
)(U
...); };
143 * Construct a module from list of symbols.
145 module(std::initializer_list
<symbol
> symbols
, std::function
<void(const module
&)> init_fn
);
147 * Construct a module from library.
153 module(const module
& mod
);
161 void* operator[](const std::string
& symbol
) const throw(std::bad_alloc
, std::runtime_error
);
163 * Variable symbol lookup.
165 template<typename T
> T
* var(const std::string
& symbol
) const throw(std::bad_alloc
, std::runtime_error
)
167 return (T
*)(*this)[symbol
];
170 * Function symbol lookup.
172 template<typename T
, typename
... U
> typename fntype
<T
, U
...>::t
fn(const std::string
& symbol
) const
173 throw(std::bad_alloc
, std::runtime_error
)
175 return (typename fntype
<T
, U
...>::t
)(*this)[symbol
];
180 std::string
get_libname() const { return libname
; }
182 * Run all not ran initialization functions.
184 static void run_initializers();
186 * Mark object (only works for libraries).
188 void mark(const void* obj
) const { if(dynamic
) lib
.mark(obj
); }
190 * Is object marked (only works for libraries)?
192 bool is_marked(const void* obj
) const { return dynamic
? lib
.is_marked(obj
) : false; }
196 std::map
<std::string
, void*> symbols
;
197 std::function
<void(const module
&)> init
;