GRSISort "v4.0.0.5"
An extension of the ROOT analysis Framework
Loading...
Searching...
No Matches
TParserLibrary.cxx
Go to the documentation of this file.
1#include "TParserLibrary.h"
2
3#define dlsym __bull__
4#include <dlfcn.h>
5#undef dlsym
6
7#include "TGRSIOptions.h"
8#include "TGRSIUtilities.h"
9#include "TRunInfo.h"
10
11// redeclare dlsym to be a function returning a function pointer instead of void *
12extern "C" void* (*dlsym(void* handle, const char* symbol))();
13
15{
16 if(fHandle != nullptr) {
17 dlclose(fHandle);
18 }
19}
20
22{
23 if(fHandle != nullptr) {
24 std::cout << "Already loaded handle " << fHandle << std::endl;
25 return;
26 }
27
28 if(TGRSIOptions::Get()->ParserLibrary().empty()) {
29 std::ostringstream str;
30 str << DRED << "No data parser library provided! Please provided the location of the parser library via .grsirc file or on the command line." << RESET_COLOR;
31 throw std::runtime_error(str.str());
32 }
33
34 if(!FileExists(TGRSIOptions::Get()->ParserLibrary().c_str())) {
35 std::ostringstream str;
36 str << DRED << "Library '" << TGRSIOptions::Get()->ParserLibrary() << "' does not exist or we do not have permissions to access it!" << RESET_COLOR;
37 throw std::runtime_error(str.str());
38 }
39
40 fHandle = dlopen(TGRSIOptions::Get()->ParserLibrary().c_str(), RTLD_LAZY);
41 if(fHandle == nullptr) {
42 std::ostringstream str;
43 str << DRED << "Failed to open raw file library '" << TGRSIOptions::Get()->ParserLibrary() << "': " << dlerror() << "!" << RESET_COLOR;
44 std::cout << "dlerror: '" << dlerror() << "'" << std::endl;
45 throw std::runtime_error(str.str());
46 }
47 // try and get constructor and destructor functions from opened library
48#pragma GCC diagnostic push
49#pragma GCC diagnostic ignored "-Wcast-function-type"
50 fInitLibrary = reinterpret_cast<void (*)()>(dlsym(fHandle, "InitLibrary"));
51 fLibraryVersion = reinterpret_cast<std::string (*)()>(dlsym(fHandle, "LibraryVersion"));
52
53 fCreateRawFile = reinterpret_cast<TRawFile* (*)(const std::string&)>(dlsym(fHandle, "CreateFile"));
54 fDestroyRawFile = reinterpret_cast<void (*)(TRawFile*)>(dlsym(fHandle, "DestroyFile"));
55
56 fCreateDataParser = reinterpret_cast<TDataParser* (*)()>(dlsym(fHandle, "CreateParser"));
57 fDestroyDataParser = reinterpret_cast<void (*)(TDataParser*)>(dlsym(fHandle, "DestroyParser"));
58#pragma GCC diagnostic pop
59
60 if(fInitLibrary == nullptr || fLibraryVersion == nullptr || fCreateRawFile == nullptr || fDestroyRawFile == nullptr || fCreateDataParser == nullptr || fDestroyDataParser == nullptr) {
61 std::ostringstream str;
62 str << DRED << "Failed to find CreateFile, DestroyFile, CreateParser, DestroyParser, LibraryVersion, and/or InitLibrary functions in library '" << TGRSIOptions::Get()->ParserLibrary() << "'!" << RESET_COLOR;
63 throw std::runtime_error(str.str());
64 }
66 std::cout << "\tUsing library " << TGRSIOptions::Get()->ParserLibrary() << " version " << fLibraryVersion() << std::endl;
69}
#define DRED
Definition Globals.h:18
#define RESET_COLOR
Definition Globals.h:5
#define dlsym
bool FileExists(const char *filename)
#define dlsym
static TGRSIOptions * Get(int argc=0, char **argv=nullptr)
Do not use!
void ParserLibrary(std::string &library)
TDataParser *(* fCreateDataParser)()
std::string(* fLibraryVersion)()
void(* fInitLibrary)()
void(* fDestroyRawFile)(TRawFile *)
void Load()
if necessary loads shared object library and sets/initializes all other functions
void(* fDestroyDataParser)(TDataParser *)
TRawFile *(* fCreateRawFile)(const std::string &)
void * fHandle
handle for shared object library
Reader for raw files.
Definition TRawFile.h:31
static void SetLibraryVersion(const char *ver)
Definition TRunInfo.h:173
static void SetLibraryPath(const char *ver)
Definition TRunInfo.h:185