Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
LibraryLoader.h
Go to the documentation of this file.
1
6#pragma once
7
8#include <dlfcn.h>
9
10#include <experimental/filesystem>
11#include <list>
12
14
15namespace cli {
16
18 class Library {
19 std::string m_path;
20 void * m_handle;
21
22 public:
23 explicit Library ( std::string path ) : m_path ( std::move ( path ) ), m_handle ( nullptr ) {
24 load ( );
25 }
26
27 Library ( const Library & ) = delete;
28
29 Library ( Library && other ) noexcept : m_path ( std::move ( other.m_path ) ), m_handle ( other.m_handle ) {
30 other.m_handle = nullptr;
31 }
32
33 Library & operator = ( const Library & ) = delete;
34 Library & operator = ( Library && other ) = delete;
35
36 ~Library ( ) {
37 unload ( );
38 }
39
40 void load ( ) {
41 if ( ! loaded ( ) )
42 m_handle = dlopen ( m_path.c_str ( ), RTLD_NOW );
43 if ( ! loaded ( ) )
44 throw exception::CommonException ( std::string ( dlerror ( ) ) );
45 }
46
47 void unload ( ) {
48 if ( loaded ( ) ) {
49 dlclose ( m_handle );
50 m_handle = nullptr;
51 }
52 }
53
54 const std::string & path ( ) const {
55 return m_path;
56 }
57
58 bool loaded ( ) const {
59 return m_handle != nullptr;
60 }
61
62 };
63
64 static std::list < Library >::iterator find ( const std::string & name );
65
66 static std::list < Library > libraries;
67
68public:
69 static void load ( std::string name );
70 static void unload ( const std::string & name );
71};
72
73} /* namespace cli */
74
Definition: LibraryLoader.h:17
static void load(std::string name)
Definition: LibraryLoader.cpp:21
static void unload(const std::string &name)
Definition: LibraryLoader.cpp:27
Basic exception from which all other exceptions are derived.
Definition: CommonException.h:21
Definition: Arg.h:11