Newer
Older
#ifndef _CLI_LIBRARY_LOADER_H_
#define _CLI_LIBRARY_LOADER_H_
#include <command/Command.h>
#include <environment/Environment.h>
#include <dlfcn.h>
#include <experimental/filesystem>
#include <alib/list>
#include <exception/CommonException.h>
namespace cli {
class LibraryLoader {
class Library {
std::string m_path;
void * m_handle;
public:
Library ( std::string path ) : m_path ( std::move ( path ) ), m_handle ( nullptr ) {
}
Library ( const Library & ) = delete;
Library ( Library && other ) noexcept : m_path ( std::move ( other.m_path ) ), m_handle ( other.m_handle ) {
}
Library & operator = ( const Library & ) = delete;
Library & operator = ( Library && other ) = delete;
~Library ( ) {
unload ( );
}
void load ( ) {
if ( ! loaded ( ) )
m_handle = dlopen ( m_path.c_str ( ), RTLD_NOW );
if ( ! loaded ( ) )
throw exception::CommonException ( std::string ( dlerror ( ) ) );
}
void unload ( ) {
if ( loaded ( ) ) {
dlclose ( m_handle );
}
}
const std::string & path ( ) const {
return m_path;
}
bool loaded ( ) const {
static std::list < Library >::iterator find ( const std::string & name );
static std::list < Library > libraries;
public:
static void unload ( const std::string & name );
};
} /* namespace cli */
#endif /* _CLI_LIBRARY_LOADER_H_ */