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
21void TParserLibrary::Load(bool quiet)
22{
23 if(fHandle != nullptr) {
24 if(!quiet) {
25 std::cout << "Already loaded handle " << fHandle << std::endl;
26 }
27 return;
28 }
29
30 if(TGRSIOptions::Get()->ParserLibrary().empty()) {
31 std::ostringstream str;
32 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;
33 throw std::runtime_error(str.str());
34 }
35
36 if(!FileExists(TGRSIOptions::Get()->ParserLibrary().c_str())) {
37 std::ostringstream str;
38 str << DRED << "Library '" << TGRSIOptions::Get()->ParserLibrary() << "' does not exist or we do not have permissions to access it!" << RESET_COLOR;
39 throw std::runtime_error(str.str());
40 }
41
42 fHandle = dlopen(TGRSIOptions::Get()->ParserLibrary().c_str(), RTLD_LAZY);
43 if(fHandle == nullptr) {
44 std::ostringstream str;
45 str << DRED << "Failed to open raw file library '" << TGRSIOptions::Get()->ParserLibrary() << "': " << dlerror() << "!" << RESET_COLOR;
46 std::cout << "dlerror: '" << dlerror() << "'" << std::endl;
47 throw std::runtime_error(str.str());
48 }
49 // try and get constructor and destructor functions from opened library
50#pragma GCC diagnostic push
51#pragma GCC diagnostic ignored "-Wcast-function-type"
52 fInitLibrary = reinterpret_cast<void (*)()>(dlsym(fHandle, "InitLibrary"));
53 fLibraryVersion = reinterpret_cast<std::string (*)()>(dlsym(fHandle, "LibraryVersion"));
54
55 fCreateRawFile = reinterpret_cast<TRawFile* (*)(const std::string&)>(dlsym(fHandle, "CreateFile"));
56 fDestroyRawFile = reinterpret_cast<void (*)(TRawFile*)>(dlsym(fHandle, "DestroyFile"));
57
58 fCreateDataParser = reinterpret_cast<TDataParser* (*)()>(dlsym(fHandle, "CreateParser"));
59 fDestroyDataParser = reinterpret_cast<void (*)(TDataParser*)>(dlsym(fHandle, "DestroyParser"));
60#pragma GCC diagnostic pop
61
62 if(fInitLibrary == nullptr || fLibraryVersion == nullptr || fCreateRawFile == nullptr || fDestroyRawFile == nullptr || fCreateDataParser == nullptr || fDestroyDataParser == nullptr) {
63 std::ostringstream str;
64 str << DRED << "Failed to find CreateFile, DestroyFile, CreateParser, DestroyParser, LibraryVersion, and/or InitLibrary functions in library '" << TGRSIOptions::Get()->ParserLibrary() << "'!" << RESET_COLOR;
65 throw std::runtime_error(str.str());
66 }
68 std::cout << "\tUsing library " << TGRSIOptions::Get()->ParserLibrary() << " version " << fLibraryVersion() << std::endl;
71}
#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)()
void Load(bool quiet=false)
if necessary loads shared object library and sets/initializes all other functions
std::string(* fLibraryVersion)()
void(* fInitLibrary)()
void(* fDestroyRawFile)(TRawFile *)
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:179
static void SetLibraryPath(const char *ver)
Definition TRunInfo.h:191