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-2020 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 
26 namespace HepMC3 {
27 
28 ReaderPlugin::ReaderPlugin(std::istream & stream, const std::string &libname, const std::string &newreader) {
29 #ifdef WIN32
30  dll_handle = nullptr;
31  dll_handle = LoadLibrary(libname.c_str());
32  if (!dll_handle) { printf("Error while loading library %s. Error code %i\n", libname.c_str(), GetLastError()); m_reader = nullptr; return; }
33  typedef Reader* (__stdcall *f_funci)(std::istream & stream);
34  f_funci newReader = (f_funci)GetProcAddress((HINSTANCE)(dll_handle), newreader.c_str());
35  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; }
36  m_reader = (Reader*)(newReader(stream));
37 #endif
38 
39 #if defined(__linux__) || defined(__darwin__)
40  dll_handle = nullptr;
41  dll_handle = dlopen(libname.c_str(), RTLD_LAZY | RTLD_GLOBAL);
42  if (!dll_handle) { printf("Error while loading library %s: %s\n", libname.c_str(), dlerror()); m_reader = nullptr; return; }
43  Reader* (*newReader)(std::istream & stream);
44  newReader = (Reader* (*)(std::istream & stream))dlsym(dll_handle, newreader.c_str());
45  if (!newReader) { printf("Error while loading function %s from library %s: %s\n", newreader.c_str(), libname.c_str(), dlerror()); m_reader = nullptr; return; }
46  m_reader = (Reader*)(newReader(stream));
47 #endif
48 }
49 /** @brief Constructor */
50 ReaderPlugin::ReaderPlugin(const std::string& filename, const std::string &libname, const std::string &newreader) {
51 #ifdef WIN32
52  dll_handle = nullptr;
53  dll_handle = LoadLibrary(libname.c_str());
54  if (!dll_handle) { printf("Error while loading library %s. Error code %i\n", libname.c_str(), GetLastError()); m_reader = nullptr; return; }
55  typedef Reader* (__stdcall *f_funci)(const std::string&);
56  f_funci newReader = (f_funci)GetProcAddress((HINSTANCE)(dll_handle), newreader.c_str());
57  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; }
58  m_reader = (Reader*)(newReader(filename));
59 #endif
60 
61 #if defined(__linux__) || defined(__darwin__)
62  dll_handle = nullptr;
63  dll_handle = dlopen(libname.c_str(), RTLD_LAZY | RTLD_GLOBAL);
64  if (!dll_handle) { printf("Error while loading library %s: %s\n", libname.c_str(), dlerror()); m_reader = nullptr; return; }
65  Reader* (*newReader)(const std::string&);
66  newReader = (Reader* (*)(const std::string&))dlsym(dll_handle, newreader.c_str());
67  if (!newReader) { printf("Error while loading function %s from library %s: %s\n", newreader.c_str(), libname.c_str(), dlerror()); m_reader = nullptr; return; }
68  m_reader = (Reader*)(newReader(filename));
69 #endif
70 }
72  if (m_reader) m_reader->close();
73  if (m_reader) delete m_reader;
74 #ifdef WIN32
75  if (dll_handle) {
76  FreeLibrary((HINSTANCE)(dll_handle));
77  }
78 #endif
79 #if defined(__linux__) || defined(__darwin__)
80  if (dll_handle) {
81  dlclose(dll_handle);
82  dll_handle = nullptr;
83  }
84 #endif
85 }
86 } // namespace HepMC3
HepMC3 main namespace.
virtual void close()=0
Close file and/or stream.
~ReaderPlugin() override
Destructor.
Definition: ReaderPlugin.cc:71
Reader * m_reader
The actual reader.
Definition: ReaderPlugin.h:39
void * dll_handle
library handler
Definition: ReaderPlugin.h:40
Definition of class ReaderPlugin.
Definition of class GenEvent.
ReaderPlugin(std::istream &stream, const std::string &libname, const std::string &newreader)
Constructor to read from stream.
Definition: ReaderPlugin.cc:28
Base class for all I/O readers.
Definition: Reader.h:25