Leosac  0.8.0
Open Source Access Control
dynamiclibrary.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014-2016 Leosac
3 
4  This file is part of Leosac.
5 
6  Leosac is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Affero General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Leosac is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Affero General Public License for more details.
15 
16  You should have received a copy of the GNU Affero General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
26 #ifndef DYNAMICLIBRARY_HPP
27 #define DYNAMICLIBRARY_HPP
28 
29 extern "C" {
30 #include <dlfcn.h>
31 }
32 
33 #include <string>
34 
39 {
40  public:
41  enum class RelocationMode : int
42  {
43  Lazy = RTLD_LAZY,
44  Now = RTLD_NOW
45  };
46 
47  public:
52  explicit DynamicLibrary(const std::string &file);
53  ~DynamicLibrary() = default;
54 
55  DynamicLibrary(const DynamicLibrary &other) = delete;
56  DynamicLibrary &operator=(const DynamicLibrary &other) = delete;
57 
58  public:
65 
70  void close();
71 
77  void *getSymbol(const std::string &symbol);
78 
82  const std::string &getFilePath() const;
83 
84  private:
85  std::string _file;
86  void *_handle;
87 };
88 
89 #endif // DYNAMICLIBRARY_HPP
DynamicLibrary::RelocationMode::Lazy
@ Lazy
DynamicLibrary::getSymbol
void * getSymbol(const std::string &symbol)
Lookup a symbol by name and return a pointer to it.
Definition: dynamiclibrary.cpp:54
DynamicLibrary::getFilePath
const std::string & getFilePath() const
Returns the full path from which the library was loaded.
Definition: dynamiclibrary.cpp:65
DynamicLibrary::DynamicLibrary
DynamicLibrary(const std::string &file)
Construct a dynamic library wrapper for the shared object referenced by name.
Definition: dynamiclibrary.cpp:29
DynamicLibrary::operator=
DynamicLibrary & operator=(const DynamicLibrary &other)=delete
DynamicLibrary::RelocationMode
RelocationMode
Definition: dynamiclibrary.hpp:41
DynamicLibrary
Wraps a dynamic library handler and provide methods to interact with it.
Definition: dynamiclibrary.hpp:38
DynamicLibrary::_file
std::string _file
Definition: dynamiclibrary.hpp:85
DynamicLibrary::open
void open(RelocationMode mode=RelocationMode::Lazy)
Attempts to open the shared library file so that we can access its symbols.
Definition: dynamiclibrary.cpp:35
DynamicLibrary::_handle
void * _handle
Definition: dynamiclibrary.hpp:86
DynamicLibrary::~DynamicLibrary
~DynamicLibrary()=default
DynamicLibrary::close
void close()
Close the already opened library handler.
Definition: dynamiclibrary.cpp:48
DynamicLibrary::RelocationMode::Now
@ Now