Merge branch 'master' of ssh://repo.or.cz/srv/git/ail
[ail.git] / source / module.cpp
blobed450b02c3ba8ee9edd5bea3c44b81d56e0c01f8
1 #include <ail/module.hpp>
3 #ifndef AIL_WINDOWS
4 #include <dlfcn.h>
5 #endif
7 namespace ail
9 dynamic_module::dynamic_module():
10 is_loaded(false)
14 dynamic_module::~dynamic_module()
16 unload();
19 bool dynamic_module::load(std::string const & new_path)
21 #ifdef AIL_WINDOWS
22 module_handle = ::LoadLibrary(new_path.c_str());
23 #else
24 module_handle = ::dlopen(new_path.c_str(), RTLD_LAZY);
25 #endif
27 is_loaded = module_handle != 0;
29 if(is_loaded)
30 path = new_path;
31 return is_loaded;
34 void dynamic_module::unload()
36 if(is_loaded)
38 #ifdef AIL_WINDOWS
39 ::FreeLibrary(module_handle);
40 #else
41 ::dlclose(module_handle);
42 #endif
43 is_loaded = false;
47 bool dynamic_module::get_function(std::string const & name, void * & output)
49 if(!is_loaded)
50 return false;
52 #ifdef AIL_WINDOWS
53 output = ::GetProcAddress(module_handle, name.c_str());
54 return output != 0;
55 #else
56 boost::mutex::scoped_lock scoped_lock(mutex)
57 output = ::dlsym(module_handle, name.c_str());
58 return dlerror() == 0;
59 #endif