Debian Codegraph @

include/HepMC3/WriterAscii.h

raw
1 // -*- C++ -*-
2 //
3 // This file is part of HepMC
4 // Copyright (C) 2014-2019 The HepMC collaboration (see AUTHORS for details)
5 //
6 #ifndef HEPMC3_WRITERASCII_H
7 #define HEPMC3_WRITERASCII_H
8 ///
9 /// @file WriterAscii.h
10 /// @brief Definition of class \b WriterAscii
11 ///
12 /// @class HepMC3::WriterAscii
13 /// @brief GenEvent I/O serialization for structured text files
14 ///
15 /// @ingroup IO
16 ///
17 #include "HepMC3/Writer.h"
18 #include "HepMC3/GenEvent.h"
19 #include "HepMC3/GenRunInfo.h"
20 #include <string>
21 #include <fstream>
22
23 namespace HepMC3 {
24
25 class WriterAscii : public Writer {
26 public:
27
28 /// @brief Constructor
29 /// @warning If file already exists, it will be cleared before writing
30 WriterAscii(const std::string& filename,
31 shared_ptr<GenRunInfo> run = shared_ptr<GenRunInfo>());
32
33 /// @brief Constructor from ostream
34 WriterAscii(std::ostream& stream,
35 shared_ptr<GenRunInfo> run = shared_ptr<GenRunInfo>());
36
37 /// @brief Destructor
38 ~WriterAscii();
39
40 /// @brief Write event to file
41 ///
42 /// @param[in] evt Event to be serialized
43 void write_event(const GenEvent& evt);
44
45 /// @brief Write the GenRunInfo object to file.
46 void write_run_info();
47
48 /// @brief Return status of the stream
49 bool failed();
50
51 /// @brief Close file stream
52 void close();
53
54 /// @brief Set output precision
55 ///
56 /// So far available range is [2,24]. Default is 16.
57 void set_precision(const int& prec );
58 /// @brief Return output precision
59 int precision() const;
60 private:
61
62 /// @name Buffer management
63 //@{
64
65 /// @brief Attempts to allocate buffer of the chosen size
66 ///
67 /// This function can be called manually by the user or will be called
68 /// before first read/write operation
69 ///
70 /// @note If buffer size is too large it will be divided by 2 until it is
71 /// small enough for system to allocate
72 void allocate_buffer();
73
74 /// @brief Set buffer size (in bytes)
75 ///
76 /// Default is 256kb. Minimum is 256b.
77 /// Size can only be changed before first read/write operation.
78 void set_buffer_size(const size_t& size );
79
80 /// @brief Escape '\' and '\n' characters in string
81 std::string escape(const std::string& s) const;
82
83 /// Inline function flushing buffer to output stream when close to buffer capacity
84 void flush();
85
86 /// Inline function forcing flush to the output stream
87 void forced_flush();
88
89 //@}
90
91
92 /// @name Write helpers
93 //@{
94
95 /// @brief Inline function for writing strings
96 ///
97 /// Since strings can be long (maybe even longer than buffer) they have to be dealt
98 /// with separately.
99 void write_string( const std::string &str );
100
101 /// @brief Write vertex
102 ///
103 /// Helper routine for writing single vertex to file
104 void write_vertex(ConstGenVertexPtr v);
105
106 /// @brief Write particle
107 ///
108 /// Helper routine for writing single particle to file
109 void write_particle(ConstGenParticlePtr p, int second_field);
110
111 //@}
112
113 private:
114
115 std::ofstream m_file; //!< Output file
116 std::ostream* m_stream; //!< Output stream
117
118 int m_precision; //!< Output precision
119 char* m_buffer; //!< Stream buffer
120 char* m_cursor; //!< Cursor inside stream buffer
121 unsigned long m_buffer_size; //!< Buffer size
122
123 };
124
125
126 } // namespace HepMC3
127
128 #endif