dynamicLinkLibs.cpp
Go to the documentation of this file.
1 /*------------------------------- phasicFlow ---------------------------------
2  O C enter of
3  O O E ngineering and
4  O O M ultiscale modeling of
5  OOOOOOO F luid flow
6 ------------------------------------------------------------------------------
7  Copyright (C): www.cemf.ir
8  email: hamid.r.norouzi AT gmail.com
9 ------------------------------------------------------------------------------
10 Licence:
11  This file is part of phasicFlow code. It is a free software for simulating
12  granular and multiphase flows. You can redistribute it and/or modify it under
13  the terms of GNU General Public License v3 or any other later versions.
14 
15  phasicFlow is distributed to help others in their research in the field of
16  granular and multiphase flows, but WITHOUT ANY WARRANTY; without even the
17  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 
19 -----------------------------------------------------------------------------*/
20 
21 #include <dlfcn.h>
22 
23 #include "dynamicLinkLibs.hpp"
24 #include "List.hpp"
25 #include "streams.hpp"
26 
27 
29  const dictionary &dict,
30  word libList)
31 {
32 
33  wordList libNames;
34  if(dict.containsDataEntry(libList))
35  {
36  libNames = dict.getVal<wordList>(libList);
37  }
38 
39 
40  REPORT(1)<< "libs are "<< greenText(libNames)<<endREPORT;
41 
42  for(auto libName:libNames)
43  {
44  auto* hndl = open(libName);
45 
46  if(hndl)
47  {
48  libs_.insertIf(libName, hndl);
49  }
50  else
51  {
52  fatalExit;
53  }
54  }
55 }
56 
57 
59 {
60  for(auto &lib:libs_)
61  {
62  if( lib.second && dlclose(lib.second) != 0)
63  {
64  warningInFunction<< "could not close lib "<<lib.first<<endl;
65  }
66  }
67 }
68 
70 {
71  REPORT(2)<<"Loading "<< greenText(libName)<<endREPORT;
72 
73  void* handle = dlopen(libName.c_str(), RTLD_LAZY|RTLD_GLOBAL);
74 
75  if(!handle)
76  {
77  warningInFunction<< "could not open lib "<< libName<<endl;
78  return nullptr;
79  }
80 
81  return handle;
82 }
pFlow::List< word >
endREPORT
#define endREPORT
Definition: streams.hpp:41
pFlow::dynamicLinkLibs::dynamicLinkLibs
dynamicLinkLibs(const dictionary &dict, word libList="libs")
Definition: dynamicLinkLibs.cpp:28
fatalExit
#define fatalExit
Definition: error.hpp:57
pFlow::dynamicLinkLibs::libs_
wordHashMap< void * > libs_
Definition: dynamicLinkLibs.hpp:36
REPORT
#define REPORT(n)
Definition: streams.hpp:40
warningInFunction
#define warningInFunction
Definition: error.hpp:55
List.hpp
pFlow::word
std::string word
Definition: builtinTypes.hpp:63
pFlow::endl
iOstream & endl(iOstream &os)
Definition: iOstream.hpp:312
greenText
#define greenText(text)
Definition: streams.hpp:32
pFlow::dictionary::containsDataEntry
bool containsDataEntry(const word &name) const
Definition: dictionary.cpp:748
pFlow::dynamicLinkLibs::open
void * open(word libName)
Definition: dynamicLinkLibs.cpp:69
streams.hpp
pFlow::dictionary::getVal
T getVal(const word &keyword) const
Definition: dictionary.hpp:309
pFlow::dynamicLinkLibs::~dynamicLinkLibs
~dynamicLinkLibs()
Definition: dynamicLinkLibs.cpp:58
dynamicLinkLibs.hpp
pFlow::dictionary
Definition: dictionary.hpp:38