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-2021 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 <cstring>
22 #include <sstream>
23 #include "HepMC3/WriterPlugin.h"
24 #include "HepMC3/GenEvent.h"
25 
26 
27 namespace HepMC3 {
28 
29 WriterPlugin::WriterPlugin(std::ostream & stream, const std::string &libname, const std::string &newwriter, std::shared_ptr<GenRunInfo> run) {
30 #ifdef WIN32
31  dll_handle = nullptr;
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, 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 = nullptr;
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  Writer* (*newWriter)(std::ostream & stream, std::shared_ptr<GenRunInfo>);
45  newWriter = (Writer* (*)(std::ostream & stream, std::shared_ptr<GenRunInfo>))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(const std::string& filename, const std::string &libname, const std::string &newwriter, std::shared_ptr<GenRunInfo> run) {
52 #ifdef WIN32
53  dll_handle = nullptr;
54  dll_handle = LoadLibrary(libname.c_str());
55  if (!dll_handle) { printf("Error while loading library %s. Error code %i\n", libname.c_str(), GetLastError()); m_writer = nullptr; return; }
56  typedef Writer* (__stdcall *f_funci)(const std::string&, shared_ptr<GenRunInfo>);
57  f_funci newWriter = (f_funci)GetProcAddress((HINSTANCE)(dll_handle), newwriter.c_str());
58  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; }
59  m_writer = (Writer*)(newWriter(filename, run));
60 #endif
61 
62 #if defined(__linux__) || defined(__darwin__) || defined(__APPLE__) || defined(BSD) || defined(__sun)
63  dll_handle = nullptr;
64  dll_handle = dlopen(libname.c_str(), RTLD_LAZY | RTLD_GLOBAL);
65  if (!dll_handle) { printf("Error while loading library %s: %s\n", libname.c_str(), dlerror()); m_writer = nullptr; return; }
66  Writer* (*newWriter)(const std::string&, std::shared_ptr<GenRunInfo>);
67  newWriter = (Writer* (*)(const std::string&, std::shared_ptr<GenRunInfo>))dlsym(dll_handle, newwriter.c_str());
68  if (!newWriter) { printf("Error while loading function %s from library %s: %s\n", newwriter.c_str(), libname.c_str(), dlerror()); m_writer = nullptr; return; }
69  m_writer = (Writer*)(newWriter(filename, run));
70 #endif
71 }
72 
74  if (m_writer) m_writer->close();
75  if (m_writer) delete m_writer;
76 #ifdef WIN32
77  if (dll_handle) {
78  FreeLibrary((HINSTANCE)dll_handle);
79  }
80 #endif
81 #if defined(__linux__) || defined(__darwin__) || defined(__APPLE__) || defined(BSD) || defined(__sun)
82  if (dll_handle) {
83  dlclose(dll_handle);
84  dll_handle = nullptr;
85  }
86 #endif
87 }
88 } // namespace HepMC3
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.
virtual void close()=0
Close file and/or stream.
Writer * m_writer
The actual writer.
Definition: WriterPlugin.h:42
Definition of class WriterPlugin.
void * dll_handle
library handler
Definition: WriterPlugin.h:43
Definition of class GenEvent.
~WriterPlugin() override
Destructor.
Definition: WriterPlugin.cc:73
Writer()
Constructor.
Definition: Writer.h:29