Skip to content
Snippets Groups Projects
Commit c4a6034d authored by Jan Trávníček's avatar Jan Trávníček
Browse files

introduce load cli command to load library

parent 71806540
No related branches found
No related tags found
No related merge requests found
LIBRARY:=alib2cli
TESTBIN:=alib2test
LINK_PATHS=../alib2xml/ ../alib2common/ ../alib2abstraction/ ../alib2measure/ ../alib2std/
LINK_LIBRARIES=alib2xml alib2common alib2abstraction alib2measure alib2std xml2
LINK_LIBRARIES=alib2xml alib2common alib2abstraction alib2measure alib2std xml2 stdc++fs
INCLUDE_PATHS=\$$(SOURCES_BASE_DIR)/../../alib2xml/src/ \$$(SOURCES_BASE_DIR)/../../alib2common/src/ \$$(SOURCES_BASE_DIR)/../../alib2abstraction/src/ \$$(SOURCES_BASE_DIR)/../../alib2measure/src/ \$$(SOURCES_BASE_DIR)/../../alib2std/src/ /usr/include/libxml2/
#ifndef _CLI_LOAD_COMMAND_H_
#define _CLI_LOAD_COMMAND_H_
#include <command/Command.h>
#include <environment/Environment.h>
#include <common/LibraryLoader.h>
namespace cli {
class LoadCommand : public Command {
std::string m_libraryName;
public:
LoadCommand ( std::string libraryName ) : m_libraryName ( std::move ( libraryName ) ) {
}
virtual Command::Result run ( Environment & ) const override {
cli::LibraryLoader::load ( m_libraryName );
return Command::Result::OK;
}
};
} /* namespace cli */
#endif /* _CLI_LOAD_COMMAND_H_ */
#include "LibraryLoader.h"
namespace cli {
std::list < LibraryLoader::Library >::iterator LibraryLoader::find ( const std::string name ) {
for ( std::list < LibraryLoader::Library >::iterator iter = libraries.begin ( ); iter != libraries.end ( ); ++ iter ) {
if ( iter->path ( ) == name )
return iter;
}
return libraries.end ( );
}
std::list < LibraryLoader::Library > LibraryLoader::libraries;
void LibraryLoader::load ( const std::string & name ) {
std::list < LibraryLoader::Library >::iterator iter = find ( name );
if ( iter == libraries.end ( ) )
iter = libraries.emplace ( libraries.end ( ), std::move ( name ) );
iter->load ( );
}
} /* namespace cli */
#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 ( const std::string & path ) : m_path ( path ), m_handle ( NULL ) {
}
Library ( const Library & ) = delete;
Library ( Library && other ) : m_path ( std::move ( other.m_path ) ), m_handle ( other.m_handle ) {
other.m_handle = NULL;
}
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 );
m_handle = NULL;
}
}
const std::string & path ( ) const {
return m_path;
}
bool loaded ( ) const {
return m_handle != NULL;
}
};
static std::list < Library >::iterator find ( const std::string name );
static std::list < Library > libraries;
public:
static void load ( const std::string & name );
};
} /* namespace cli */
#endif /* _CLI_LIBRARY_LOADER_H_ */
......@@ -30,6 +30,7 @@
#include <command/DataTypesIntrospectionCommand.h>
#include <command/CastsIntrospectionCommand.h>
#include <command/SetCommand.h>
#include <command/LoadCommand.h>
 
#include <primitive/Integer.h>
#include <primitive/String.h>
......@@ -358,6 +359,12 @@ std::unique_ptr < Command > Parser::parse ( ) {
match ( cli::Lexer::TokenType::INTEGER, cli::Lexer::TokenType::IDENTIFIER, cli::Lexer::TokenType::STRING );
match ( cli::Lexer::TokenType::END );
return std::make_unique < SetCommand > ( std::move ( param ), std::move ( value ) );
} else if ( check_nonreserved_kw ( "load" ) ) {
match_nonreserved_kw ( "load" );
std::string libraryName = getTokenValue ( );
match ( cli::Lexer::TokenType::INTEGER, cli::Lexer::TokenType::IDENTIFIER, cli::Lexer::TokenType::STRING );
match ( cli::Lexer::TokenType::END );
return std::make_unique < LoadCommand > ( std::move ( libraryName ) );
} else {
throw exception::CommonException ( "Mismatched set while expanding parse rule." );
}
......
......@@ -95,7 +95,7 @@ public:
std::unique_ptr < Param > in_redirect_param ( );
 
std::unique_ptr < Param > param ( );
std::unique_ptr < Arg > template_param ( );
 
std::unique_ptr < Param > move_param ( );
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment