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