| 1 | #cxx . . $ `<file>/ncrystal_core/include/NCrystal/internal/NCDynLoader.hh`/ifndef NCrystal_DynLoader_hh |
| 2 | #define NCrystal_DynLoader_hhcxx . . $ `ncrystal_core/include/NCrystal/internal/NCDynLoader.hh:2:9`! |
| 3 | |
| 4 | //////////////////////////////////////////////////////////////////////////////// |
| 5 | // // |
| 6 | // This file is part of NCrystal (see https://mctools.github.io/ncrystal/) // |
| 7 | // // |
| 8 | // Copyright 2015-2022 NCrystal developers // |
| 9 | // // |
| 10 | // Licensed under the Apache License, Version 2.0 (the "License"); // |
| 11 | // you may not use this file except in compliance with the License. // |
| 12 | // You may obtain a copy of the License at // |
| 13 | // // |
| 14 | // http://www.apache.org/licenses/LICENSE-2.0 // |
| 15 | // // |
| 16 | // Unless required by applicable law or agreed to in writing, software // |
| 17 | // distributed under the License is distributed on an "AS IS" BASIS, // |
| 18 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // |
| 19 | // See the License for the specific language governing permissions and // |
| 20 | // limitations under the License. // |
| 21 | // // |
| 22 | //////////////////////////////////////////////////////////////////////////////// |
| 23 | |
| 24 | #include "NCrystal/NCDefs.hh"cxx . . $ `<file>/ncrystal_core/include/NCrystal/NCDefs.hh`/ |
| 25 | |
| 26 | namespace NCrystalcxx . . $ NCrystal/ { |
| 27 | |
| 28 | //Dynamic load libraries and symbols. |
| 29 | |
| 30 | class DynLoadercxx . . $ NCrystal/DynLoader# : private MoveOnlycxx . . $ NCrystal/MoveOnly# { |
| 31 | public: |
| 32 | |
| 33 | //Constructor, getFunction, and findSymbolAddr functions throw DataLoadError |
| 34 | //exceptions in case of problems. |
| 35 | |
| 36 | //Flags (same meaning as dlopen's RTLD_ flags). Might not be used on Windows: |
| 37 | enum class ScopeFlagcxx . . $ NCrystal/DynLoader#ScopeFlag# { globalcxx . . $ NCrystal/DynLoader#ScopeFlag#global., localcxx . . $ NCrystal/DynLoader#ScopeFlag#local., default_valuecxx . . $ NCrystal/DynLoader#ScopeFlag#default_value. = localcxx . . $ NCrystal/DynLoader#ScopeFlag#local. }; |
| 38 | enum class LazyFlagcxx . . $ NCrystal/DynLoader#LazyFlag# { lazycxx . . $ NCrystal/DynLoader#LazyFlag#lazy., nowcxx . . $ NCrystal/DynLoader#LazyFlag#now., default_valuecxx . . $ NCrystal/DynLoader#LazyFlag#default_value. = lazycxx . . $ NCrystal/DynLoader#LazyFlag#lazy. }; |
| 39 | |
| 40 | //Constructor, loads library: |
| 41 | DynLoadercxx . . $ NCrystal/DynLoader#DynLoader(55542d53c44106f3).( const stdcxx . . $ std/::stringcxx . . $ std/string#& filename, |
| 42 | ScopeFlagcxx . . $ NCrystal/DynLoader#ScopeFlag# = ScopeFlagcxx . . $ NCrystal/DynLoader#ScopeFlag#::default_valuecxx . . $ NCrystal/DynLoader#ScopeFlag#default_value., |
| 43 | LazyFlagcxx . . $ NCrystal/DynLoader#LazyFlag# = LazyFlagcxx . . $ NCrystal/DynLoader#LazyFlag#::default_valuecxx . . $ NCrystal/DynLoader#LazyFlag#default_value. ); |
| 44 | |
| 45 | //Destructor, unloads library unless doNotClose was called (or object was moved-from): |
| 46 | ~cxx . . $ NCrystal/DynLoader#`~DynLoader`(ced63f7c635d850d).DynLoadercxx . . $ NCrystal/DynLoader#(); |
| 47 | |
| 48 | //Lookup function by symbol and reinterpret_cast it to a given signature: |
| 49 | template<class Signature> |
| 50 | stdcxx . . $ std/::pair<bool,stdcxx . . $ std/::functioncxx . . $ std/function#<Signature>> tryGetFunctioncxx . . $ NCrystal/DynLoader#tryGetFunction(76d541a192e9a200).( const stdcxx . . $ std/::stringcxx . . $ std/string#& fctsymbol) const |
| 51 | { |
| 52 | auto a = tryFindSymbolAddrcxx . . $ NCrystal/DynLoader#tryFindSymbolAddr(17d41e5b99e3a68d).(fctsymbol); |
| 53 | return { a.first, reinterpret_cast<Signature*>(a.second) }; |
| 54 | } |
| 55 | |
| 56 | template<class Signature> |
| 57 | stdcxx . . $ std/::functioncxx . . $ std/function#<Signature> getFunctioncxx . . $ NCrystal/DynLoader#getFunction(ccd493d6b0efcbc8).( const stdcxx . . $ std/::stringcxx . . $ std/string#& fctsymbol) const |
| 58 | { |
| 59 | //The use of dummy below, in the very particular manner it is written, is |
| 60 | //a way to workaround a quirk in the C/POSIX specification. With gcc 4.8.5 |
| 61 | //and -pedantic, it would generate a misleading warning ("ISO C++ forbids |
| 62 | //casting between pointer-to-function and pointer-to-object") otherwise. |
| 63 | //The workaround is taken from the dlsym manpage |
| 64 | //(https://linux.die.net/man/3/dlsym). |
| 65 | void (*dummy)(); |
| 66 | *(void **) (&dummy) = findSymbolAddrcxx . . $ NCrystal/DynLoader#findSymbolAddr(9404366c342a279f).(fctsymbol); |
| 67 | return reinterpret_cast<Signature*>(dummy); |
| 68 | } |
| 69 | |
| 70 | //Just lookup the address of symbols: |
| 71 | void * findSymbolAddrcxx . . $ NCrystal/DynLoader#findSymbolAddr(9404366c342a279f).( const stdcxx . . $ std/::stringcxx . . $ std/string#& ) const; |
| 72 | stdcxx . . $ std/::pair<bool,void *> tryFindSymbolAddrcxx . . $ NCrystal/DynLoader#tryFindSymbolAddr(17d41e5b99e3a68d).( const stdcxx . . $ std/::stringcxx . . $ std/string#& ) const; |
| 73 | |
| 74 | DynLoadercxx . . $ NCrystal/DynLoader#DynLoader(ab7b72cbead99d96).( DynLoadercxx . . $ NCrystal/DynLoader#&& ); |
| 75 | DynLoadercxx . . $ NCrystal/DynLoader#& operatorcxx . . $ NCrystal/DynLoader#`operator=`(89fed11e24582117).=( DynLoadercxx . . $ NCrystal/DynLoader#&& ); |
| 76 | |
| 77 | //Call to avoid dlclose call in destructor: |
| 78 | void doNotClosecxx . . $ NCrystal/DynLoader#doNotClose(49f6e7a06ebc5aa8).(); |
| 79 | |
| 80 | private: |
| 81 | void* m_handlecxx . . $ NCrystal/DynLoader#m_handle.; |
| 82 | stdcxx . . $ std/::stringcxx . . $ std/string# m_filenamecxx . . $ NCrystal/DynLoader#m_filename.; |
| 83 | bool m_doClosecxx . . $ NCrystal/DynLoader#m_doClose. = true; |
| 84 | }; |
| 85 | |
| 86 | } |
| 87 | |
| 88 | #endif |