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-2023 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__) || defined(__APPLE__) || defined(BSD) || defined(__sun)
19#include <dlfcn.h>
20#endif
21#include <sstream>
22#include <cstring>
23#include "HepMC3/GenEvent.h"
24#include "HepMC3/ReaderPlugin.h"
25
26namespace HepMC3 {
27ReaderPlugin::ReaderPlugin(std::shared_ptr<std::istream> stream, const std::string &libname, const std::string &newreader) {
28#ifdef WIN32
29 dll_handle = LoadLibrary(libname.c_str());
30 if (!dll_handle) { printf("Error while loading library %s. Error code %i\n", libname.c_str(), GetLastError()); m_reader = nullptr; return; }
31 typedef Reader* (__stdcall *f_funci)(std::shared_ptr<std::istream> stream);
32 f_funci newReader = (f_funci)GetProcAddress((HINSTANCE)(dll_handle), newreader.c_str());
33 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; }
34 m_reader = (Reader*)(newReader(stream));
35#endif
36
37#if defined(__linux__) || defined(__darwin__) || defined(__APPLE__) || defined(BSD) || defined(__sun)
38 dll_handle = dlopen(libname.c_str(), RTLD_LAZY | RTLD_GLOBAL);
39 if (!dll_handle) { printf("Error while loading library %s: %s\n", libname.c_str(), dlerror()); m_reader = nullptr; return; }
40 using f_funci = Reader *(*)(std::shared_ptr<std::istream>);
41 auto newReader = (f_funci)dlsym(dll_handle, newreader.c_str());
42 if (!newReader) { printf("Error while loading function %s from library %s: %s\n", newreader.c_str(), libname.c_str(), dlerror()); m_reader = nullptr; return; }
43 m_reader = (Reader*)(newReader(stream));
44#endif
45}
46
47ReaderPlugin::ReaderPlugin(std::istream & stream, const std::string &libname, const std::string &newreader) {
48#ifdef WIN32
49 dll_handle = LoadLibrary(libname.c_str());
50 if (!dll_handle) { printf("Error while loading library %s. Error code %i\n", libname.c_str(), GetLastError()); m_reader = nullptr; return; }
51 typedef Reader* (__stdcall *f_funci)(std::istream & stream);
52 f_funci newReader = (f_funci)GetProcAddress((HINSTANCE)(dll_handle), newreader.c_str());
53 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; }
54 m_reader = (Reader*)(newReader(stream));
55#endif
56
57#if defined(__linux__) || defined(__darwin__) || defined(__APPLE__) || defined(BSD) || defined(__sun)
58 dll_handle = dlopen(libname.c_str(), RTLD_LAZY | RTLD_GLOBAL);
59 if (!dll_handle) { printf("Error while loading library %s: %s\n", libname.c_str(), dlerror()); m_reader = nullptr; return; }
60 using f_funci = Reader *(*)(std::istream &);
61 auto newReader = (f_funci)dlsym(dll_handle, newreader.c_str());
62 if (!newReader) { printf("Error while loading function %s from library %s: %s\n", newreader.c_str(), libname.c_str(), dlerror()); m_reader = nullptr; return; }
63 m_reader = (Reader*)(newReader(stream));
64#endif
65}
66/** @brief Constructor */
67ReaderPlugin::ReaderPlugin(const std::string& filename, const std::string &libname, const std::string &newreader) {
68#ifdef WIN32
69 dll_handle = LoadLibrary(libname.c_str());
70 if (!dll_handle) { printf("Error while loading library %s. Error code %i\n", libname.c_str(), GetLastError()); m_reader = nullptr; return; }
71 typedef Reader* (__stdcall *f_funci)(const std::string&);
72 f_funci newReader = (f_funci)GetProcAddress((HINSTANCE)(dll_handle), newreader.c_str());
73 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; }
74 m_reader = (Reader*)(newReader(filename));
75#endif
76
77#if defined(__linux__) || defined(__darwin__) || defined(__APPLE__) || defined(BSD) || defined(__sun)
78 dll_handle = dlopen(libname.c_str(), RTLD_LAZY | RTLD_GLOBAL);
79 if (!dll_handle) { printf("Error while loading library %s: %s\n", libname.c_str(), dlerror()); m_reader = nullptr; return; }
80 using f_funci = Reader *(*)(const std::string&);
81 auto newReader = (f_funci)dlsym(dll_handle, newreader.c_str());
82 if (!newReader) { printf("Error while loading function %s from library %s: %s\n", newreader.c_str(), libname.c_str(), dlerror()); m_reader = nullptr; return; }
83 m_reader = (Reader*)(newReader(filename));
84#endif
85}
87 if (m_reader) m_reader->close();
88 delete m_reader;
89#ifdef WIN32
90 if (dll_handle) {
91 FreeLibrary((HINSTANCE)(dll_handle));
92 }
93#endif
94#if defined(__linux__) || defined(__darwin__) || defined(__APPLE__) || defined(BSD) || defined(__sun)
95 if (dll_handle) {
96 dlclose(dll_handle);
97 dll_handle = nullptr;
98 }
99#endif
100}
101} // namespace HepMC3
Definition of class GenEvent.
Definition of class ReaderPlugin.
~ReaderPlugin() override
Destructor.
ReaderPlugin(std::shared_ptr< std::istream > stream, const std::string &libname, const std::string &newreader)
Constructor to read from stream.
Reader * m_reader
The actual reader.
void * dll_handle
library handler
Reader()
Constructor.
Definition Reader.h:28
HepMC3 main namespace.