Leosac  0.7.0
OpenSourceAccessControl
dynamiclibrary.cpp
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 #include "dynamiclibrary.hpp"
28 
29 DynamicLibrary::DynamicLibrary(const std::string &file)
30  : _file(file)
31  , _handle(nullptr)
32 {
33 }
34 
36 {
37  char *err;
38 
39  if (!(_handle = dlopen(_file.c_str(), static_cast<int>(mode) | RTLD_NODELETE)))
40  {
41  if ((err = dlerror()))
42  throw(DynLibException(std::string("dlopen(): ") + err));
43  else
44  throw(DynLibException("dlopen(): Unknown error"));
45  }
46 }
47 
49 {
50  if (dlclose(_handle))
51  throw(DynLibException(std::string("dlclose(): ") + dlerror()));
52 }
53 
54 void *DynamicLibrary::getSymbol(const std::string &symbol)
55 {
56  void *sym;
57  char *err;
58 
59  sym = dlsym(_handle, symbol.c_str());
60  if ((err = dlerror()))
61  throw(DynLibException(std::string("dlsym(): ") + err));
62  return (sym);
63 }
64 
65 const std::string &DynamicLibrary::getFilePath() const
66 {
67  return _file;
68 }
DynamicLibrary(const std::string &file)
Construct a dynamic library wrapper for the shared object referenced by name.
void * getSymbol(const std::string &symbol)
Lookup a symbol by name and return a pointer to it.
Exception class for DynLib related errors.
const std::string & getFilePath() const
Returns the full path from which the library was loaded.
std::string _file
void open(RelocationMode mode=RelocationMode::Lazy)
Attempts to open the shared library file so that we can access its symbols.
void close()
Close the already opened library handler.
DynamicLibrary class.