HepMC3 event record library: WriterPlugin.cc Source File

HepMC3 event record library

WriterPlugin.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 WriterPlugin.cc
8/// @brief Implementation of \b class WriterPlugin
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/WriterPlugin.h"
25
26
27
28namespace HepMC3 {
29
30
31WriterPlugin::WriterPlugin(std::shared_ptr<std::ostream> stream, const std::string &libname, const std::string &newwriter, std::shared_ptr<GenRunInfo> run) {
32#ifdef WIN32
33 dll_handle = LoadLibrary(libname.c_str());
34 if (!dll_handle) { printf("Error while loading library %s. Error code %i\n", libname.c_str(), GetLastError()); m_writer = nullptr; return; }
35 typedef Writer* (__stdcall *f_funci)(std::shared_ptr<std::ostream> stream, std::shared_ptr<GenRunInfo>);
36 f_funci newWriter = (f_funci)GetProcAddress((HINSTANCE)(dll_handle), newwriter.c_str());
37 if (!newWriter) { printf("Error while loading function %s from library %s. Error code %i\n", newwriter.c_str(), libname.c_str(), GetLastError()); m_writer = nullptr; return; }
38 m_writer = (Writer*)(newWriter(stream, run));
39#endif
40
41#if defined(__linux__) || defined(__darwin__) || defined(__APPLE__) || defined(BSD) || defined(__sun)
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_writer=nullptr; return; }
44 using f_funci = Writer* (*)(std::shared_ptr<std::ostream> stream, std::shared_ptr<GenRunInfo>);
45 auto newWriter = (f_funci)dlsym(dll_handle, newwriter.c_str());
46 if (!newWriter) { printf("Error while loading function %s from library %s: %s\n", newwriter.c_str(), libname.c_str(), dlerror()); m_writer = nullptr; return; }
47 m_writer = (Writer*)(newWriter(stream, run));
48#endif
49}
50
51 WriterPlugin::WriterPlugin(std::ostream & stream, const std::string &libname, const std::string &newwriter, std::shared_ptr<GenRunInfo> run) {
52#ifdef WIN32
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_writer = nullptr; return; }
55 typedef Writer* (__stdcall *f_funci)(std::ostream & stream, std::shared_ptr<GenRunInfo>);
56 f_funci newWriter = (f_funci)GetProcAddress((HINSTANCE)(dll_handle), newwriter.c_str());
57 if (!newWriter) { printf("Error while loading function %s from library %s. Error code %i\n", newwriter.c_str(), libname.c_str(), GetLastError()); m_writer = nullptr; return; }
58 m_writer = (Writer*)(newWriter(stream, run));
59#endif
60
61#if defined(__linux__) || defined(__darwin__) || defined(__APPLE__) || defined(BSD) || defined(__sun)
62 dll_handle = dlopen(libname.c_str(), RTLD_LAZY | RTLD_GLOBAL);
63 if (!dll_handle) { printf("Error while loading library %s: %s\n", libname.c_str(), dlerror()); m_writer=nullptr; return; }
64 using f_funci = Writer* (*)(std::ostream & stream, std::shared_ptr<GenRunInfo>);
65 auto newWriter = (f_funci)dlsym(dll_handle, newwriter.c_str());
66 if (!newWriter) { printf("Error while loading function %s from library %s: %s\n", newwriter.c_str(), libname.c_str(), dlerror()); m_writer = nullptr; return; }
67 m_writer = (Writer*)(newWriter(stream, run));
68#endif
69}
70
71 WriterPlugin::WriterPlugin(const std::string& filename, const std::string &libname, const std::string &newwriter, std::shared_ptr<GenRunInfo> run) {
72#ifdef WIN32
73 dll_handle = LoadLibrary(libname.c_str());
74 if (!dll_handle) { printf("Error while loading library %s. Error code %i\n", libname.c_str(), GetLastError()); m_writer = nullptr; return; }
75 typedef Writer* (__stdcall *f_funci)(const std::string&, std::shared_ptr<GenRunInfo>);
76 f_funci newWriter = (f_funci)GetProcAddress((HINSTANCE)(dll_handle), newwriter.c_str());
77 if (!newWriter) { printf("Error while loading function %s from library %s. Error code %i\n", newwriter.c_str(), libname.c_str(), GetLastError()); m_writer = nullptr; return; }
78 m_writer = (Writer*)(newWriter(filename, run));
79#endif
80
81#if defined(__linux__) || defined(__darwin__) || defined(__APPLE__) || defined(BSD) || defined(__sun)
82 dll_handle = dlopen(libname.c_str(), RTLD_LAZY | RTLD_GLOBAL);
83 if (!dll_handle) { printf("Error while loading library %s: %s\n", libname.c_str(), dlerror()); m_writer = nullptr; return; }
84 using f_funci = Writer* (*)(const std::string&, std::shared_ptr<GenRunInfo>);
85 auto newWriter = (f_funci)dlsym(dll_handle, newwriter.c_str());
86 if (!newWriter) { printf("Error while loading function %s from library %s: %s\n", newwriter.c_str(), libname.c_str(), dlerror()); m_writer = nullptr; return; }
87 m_writer = (Writer*)(newWriter(filename, run));
88#endif
89}
90
91WriterPlugin::~WriterPlugin() {
92 if (m_writer) m_writer->close();
93 delete m_writer;
94#ifdef WIN32
95 if (dll_handle) {
96 FreeLibrary((HINSTANCE)dll_handle);
97 }
98#endif
99#if defined(__linux__) || defined(__darwin__) || defined(__APPLE__) || defined(BSD) || defined(__sun)
100 if (dll_handle) {
101 dlclose(dll_handle);
102 dll_handle = nullptr;
103 }
104#endif
105}
106} // namespace HepMC3
GenEvent.h
Definition of class GenEvent.
WriterPlugin.h
Definition of class WriterPlugin.
HepMC3::WriterPlugin::~WriterPlugin
~WriterPlugin() override
Destructor.
Definition WriterPlugin.cc:91
HepMC3::WriterPlugin::m_writer
Writer * m_writer
The actual writer.
Definition WriterPlugin.h:53
HepMC3::WriterPlugin::WriterPlugin
WriterPlugin(std::shared_ptr< std::ostream > stream, const std::string &libname, const std::string &newwriter, std::shared_ptr< HepMC3::GenRunInfo > run=std::shared_ptr< GenRunInfo >())
Constructor to write to stream.
Definition WriterPlugin.cc:31
HepMC3::WriterPlugin::dll_handle
void * dll_handle
library handler
Definition WriterPlugin.h:54
HepMC3::Writer::Writer
Writer()
Constructor.
Definition Writer.h:29
HepMC3
HepMC3 main namespace.
Definition AnalysisExample.h:24