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