All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
DLLoader.cpp
Go to the documentation of this file.
1 /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2  Copyright (c) 2013 The plumed team
3  (see the PEOPLE file at the root of the distribution for a list of names)
4 
5  See http://www.plumed-code.org for more information.
6 
7  This file is part of plumed, version 2.0.
8 
9  plumed is free software: you can redistribute it and/or modify
10  it under the terms of the GNU Lesser General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  (at your option) any later version.
13 
14  plumed is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public License
20  along with plumed. If not, see <http://www.gnu.org/licenses/>.
21 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
22 #include "DLLoader.h"
23 
24 #ifdef __PLUMED_HAS_DLOPEN
25 #include <dlfcn.h>
26 #endif
27 
28 namespace PLMD{
29 
31 #ifdef __PLUMED_HAS_DLOPEN
32  return true;
33 #else
34  return false;
35 #endif
36 }
37 
38 
39 void* DLLoader::load(const std::string&s){
40 #ifdef __PLUMED_HAS_DLOPEN
41  void* p=dlopen(s.c_str(),RTLD_NOW|RTLD_LOCAL);
42  if(!p){
43  lastError=dlerror();
44  } else {
45  lastError="";
46  handles.push(p);
47  }
48  return p;
49 #else
50  return NULL;
51 #endif
52 }
53 
54 const std::string & DLLoader::error(){
55  return lastError;
56 }
57 
59 #ifdef __PLUMED_HAS_DLOPEN
60  while(!handles.empty()){
61  dlclose(handles.top());
62  handles.pop();
63  }
64 #endif
65 }
66 
68  // do nothing
69 }
70 
71 
72 }
~DLLoader()
Cleanup.
Definition: DLLoader.cpp:58
DLLoader()
Default constructor.
Definition: DLLoader.cpp:67
const std::string & error()
Returns the last error in dynamic loader.
Definition: DLLoader.cpp:54
std::stack< void * > handles
Definition: DLLoader.h:41
static bool installed()
Returns true if the dynamic loader is available (on some systems it may not).
Definition: DLLoader.cpp:30
std::string lastError
Definition: DLLoader.h:42
void * load(const std::string &)
Load a library, returning its handle.
Definition: DLLoader.cpp:39