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