HepMC3 event record library
ReaderPlugin.cc
Go to the documentation of this file.
1// -*- C++ -*-
2//
3// This file is part of HepMC
4// Copyright (C) 2014-2019 The HepMC collaboration (see AUTHORS for details)
5//
6///
7/// @file ReaderPlugin.cc
8/// @brief Implementation of \b class ReaderPlugin
9///
10#ifdef WIN32
11#define WIN32_LEAN_AND_MEAN
12#define NOWINBASEINTERLOCK
13#define NOMINMAX
14#undef UNICODE
15#include <intrin.h>
16#include <windows.h>
17#endif
18#if defined(__linux__) || defined(__darwin__)
19#include <dlfcn.h>
20#endif
21#include <cstring>
22#include <sstream>
23#include "HepMC3/ReaderPlugin.h"
24#include "HepMC3/GenEvent.h"
25
26namespace HepMC3 {
27
28ReaderPlugin::ReaderPlugin(std::istream & stream,const std::string &libname, const std::string &newreader) {
29
30#ifdef WIN32
31 dll_handle=nullptr;
32 dll_handle = LoadLibrary(libname.c_str());
33 if (!dll_handle) { printf("Error while loading library %s. Error code %i\n",libname.c_str(),GetLastError()); m_reader=nullptr; return; }
34 typedef Reader* (__stdcall *f_funci)(std::istream & stream);
35 f_funci newReader = (f_funci)GetProcAddress((HINSTANCE)(dll_handle), newreader.c_str());
36 if (!newReader) { printf("Error while loading function %s from library %s. Error code %i\n",newreader.c_str(),libname.c_str(),GetLastError()); m_reader=nullptr; return; }
37 m_reader=(Reader*)(newReader(stream));
38#endif
39
40#if defined(__linux__) || defined(__darwin__)
41 dll_handle=nullptr;
42 dll_handle = dlopen(libname.c_str(), RTLD_LAZY | RTLD_GLOBAL);
43 if (!dll_handle) { printf("Error while loading library %s: %s\n",libname.c_str(),dlerror()); m_reader=nullptr; return; }
44 Reader* (*newReader)(std::istream & stream);
45 newReader=(Reader* (*)(std::istream & stream))dlsym(dll_handle, newreader.c_str());
46 if (!newReader) { printf("Error while loading function %s from library %s: %s\n",newreader.c_str(),libname.c_str(),dlerror()); m_reader=nullptr; return; }
47 m_reader=(Reader*)(newReader(stream));
48#endif
49
50}
51/** @brief Constructor */
52ReaderPlugin::ReaderPlugin(const std::string& filename,const std::string &libname, const std::string &newreader) {
53
54#ifdef WIN32
55 dll_handle=nullptr;
56 dll_handle = LoadLibrary(libname.c_str());
57 if (!dll_handle) { printf("Error while loading library %s. Error code %i\n",libname.c_str(),GetLastError()); m_reader=nullptr; return; }
58 typedef Reader* (__stdcall *f_funci)(const std::string&);
59 f_funci newReader = (f_funci)GetProcAddress((HINSTANCE)(dll_handle), newreader.c_str());
60 if (!newReader) { printf("Error while loading function %s from library %s. Error code %i\n",newreader.c_str(),libname.c_str(),GetLastError()); m_reader=nullptr; return; }
61 m_reader=(Reader*)(newReader(filename));
62#endif
63
64#if defined(__linux__) || defined(__darwin__)
65 dll_handle=nullptr;
66 dll_handle = dlopen(libname.c_str(), RTLD_LAZY | RTLD_GLOBAL);
67 if (!dll_handle) { printf("Error while loading library %s: %s\n",libname.c_str(),dlerror()); m_reader=nullptr; return; }
68 Reader* (*newReader)(const std::string&);
69 newReader=(Reader* (*)(const std::string&))dlsym(dll_handle, newreader.c_str());
70 if (!newReader) { printf("Error while loading function %s from library %s: %s\n",newreader.c_str(),libname.c_str(),dlerror()); m_reader=nullptr; return; }
71 m_reader=(Reader*)(newReader(filename));
72#endif
73}
75 if (m_reader) m_reader->close();
76 if (m_reader) delete m_reader;
77#ifdef WIN32
78 if(dll_handle) {
79 FreeLibrary((HINSTANCE)(dll_handle));
80 }
81#endif
82#if defined(__linux__) || defined(__darwin__)
83 if(dll_handle) {
84 dlclose(dll_handle);
85 dll_handle = nullptr;
86 }
87#endif
88}
89} // namespace HepMC3
Definition of class GenEvent.
Definition of class ReaderPlugin.
ReaderPlugin(std::istream &stream, const std::string &libname, const std::string &newreader)
Constructor to read from stream.
Definition: ReaderPlugin.cc:28
~ReaderPlugin() override
Destructor.
Definition: ReaderPlugin.cc:74
Reader * m_reader
The actual reader.
Definition: ReaderPlugin.h:41
void * dll_handle
library handler
Definition: ReaderPlugin.h:42
Base class for all I/O readers.
Definition: Reader.h:25
virtual void close()=0
Close file and/or stream.
HepMC3 main namespace.