HepMC event record
include/HepMC/Attribute.h
1 // -*- C++ -*-
2 //
3 // This file is part of HepMC
4 // Copyright (C) 2014-2015 The HepMC collaboration (see AUTHORS for details)
5 //
6 #ifndef HEPMC_ATTRIBUTE_H
7 #define HEPMC_ATTRIBUTE_H
8 /**
9  * @file Attribute.h
10  * @brief Definition of \b class Attribute, \b class IntAttribute and \b class StringAttribute
11  *
12  * @class HepMC::Attribute
13  * @brief Base class for all attributes
14  *
15  * Contains virtual functions to_string and from_string that
16  * each attribute must implement, as well as init function that
17  * attributes should overload to initialize parsed attribute
18  *
19  * @ingroup attributes
20  *
21  */
22 #include <cstdio> // sprintf
23 #include <string>
24 #include <limits>
25 #include <sstream>
26 #include <iomanip>
27 #include "HepMC/Common.h"
28 using std::string;
29 
30 namespace HepMC {
31 
32 /** @brief Forward declaration of GenEvent. */
33 class GenEvent;
34 
35 /** @brief Forward declaration of GenRunInfo. */
36 class GenRunInfo;
37 
38 class Attribute {
39 //
40 // Constructors
41 //
42 public:
43  /** @brief Default constructor */
45 
46  /** @brief Virtual destructor */
47  virtual ~Attribute() {}
48 
49 protected:
50  /** @brief Protected constructor that allows to set string
51  *
52  * Used when parsing attributes from file. An StringAttribute class
53  * object is made, which uses this constructor to signify that
54  * it just holds string without parsing it.
55  *
56  * @note There should be no need for user class to ever use this constructor
57  */
58  Attribute(const string &st):m_is_parsed(false),m_string(st) {}
59 
60 //
61 // Virtual Functions
62 //
63 public:
64  /** @brief Fill class content from string.
65  */
66  virtual bool from_string(const string & att) = 0;
67 
68  /** @brief Optionally initialize the attribute after from_string
69  *
70  * Is passed a reference to the GenEvent object to which the
71  * Attribute belongs.
72  */
73  virtual bool init(const GenEvent & /*geneve*/) {
74  return true;
75  }
76 
77  /** @brief Optionally initialize the attribute after from_string
78  *
79  * Is passed a reference to the GenRunInfo object to which the
80  * Attribute belongs.
81  */
82  virtual bool init(const GenRunInfo & /*genrun*/) {
83  return true;
84  }
85 
86  /** @brief Fill string from class content */
87  virtual bool to_string(string &att) const = 0;
88 
89 //
90 // Accessors
91 //
92 public:
93  /** @brief Check if this attribute is parsed */
94  bool is_parsed() { return m_is_parsed; }
95 
96  /** @brief Get unparsed string */
97  const string& unparsed_string() const { return m_string; }
98 
99 protected:
100  /** @brief Set is_parsed flag */
101  void set_is_parsed(bool flag) { m_is_parsed = flag; }
102 
103  /** @brief Set unparsed string */
104  void set_unparsed_string(const string &st) { m_string = st; }
105 
106 //
107 // Fields
108 //
109 private:
110  bool m_is_parsed; //!< Is this attribute parsed?
111  string m_string; //!< Raw (unparsed) string
112 };
113 
114 /**
115  * @class HepMC::IntAttribute
116  * @brief Attribute that holds an Integer implemented as an int
117  *
118  * @ingroup attributes
119  */
120 class IntAttribute : public Attribute {
121 public:
122 
123  /** @brief Default constructor */
125 
126  /** @brief Constructor initializing attribute value */
127  IntAttribute(int val):Attribute(),m_val(val) {}
128 
129  /** @brief Implementation of Attribute::from_string */
130  bool from_string(const string &att) {
131 #ifdef HEPMC_HAS_CXX11
132  m_val = std::stoi(att);
133 #else
134  m_val = atoi( att.c_str() );
135 #endif
136  return true;
137  }
138 
139  /** @brief Implementation of Attribute::to_string */
140  bool to_string(string &att) const {
141 #ifdef HEPMC_HAS_CXX11
142  att = std::to_string(m_val);
143 #else
144  char buf[24];
145  sprintf(buf,"%23i",m_val);
146  att = buf;
147 #endif
148  return true;
149  }
150 
151  /** @brief get the value associated to this Attribute. */
152  int value() const {
153  return m_val;
154  }
155 
156  /** @brief set the value associated to this Attribute. */
157  void set_value(int i) {
158  m_val = i;
159  }
160 
161 private:
162  int m_val; ///< Attribute value
163 };
164 
165 /**
166  * @class HepMC::IntAttribute
167  * @brief Attribute that holds an Integer implemented as an int
168  *
169  * @ingroup attributes
170  */
171 class LongAttribute : public Attribute {
172 public:
173 
174  /** @brief Default constructor */
176 
177  /** @brief Constructor initializing attribute value */
178  LongAttribute(long val): Attribute(), m_val(val) {}
179 
180  /** @brief Implementation of Attribute::from_string */
181  bool from_string(const string &att) {
182 #ifdef HEPMC_HAS_CXX11
183  m_val = std::stoi(att);
184 #else
185  m_val = atoi( att.c_str() );
186 #endif
187  return true;
188  }
189 
190  /** @brief Implementation of Attribute::to_string */
191  bool to_string(string &att) const {
192 #ifdef HEPMC_HAS_CXX11
193  att = std::to_string(m_val);
194 #else
195  char buf[24];
196  sprintf(buf,"%23li",m_val);
197  att = buf;
198 #endif
199  return true;
200  }
201 
202  /** @brief get the value associated to this Attribute. */
203  long value() const {
204  return m_val;
205  }
206 
207  /** @brief set the value associated to this Attribute. */
208  void set_value(long l) {
209  m_val = l;
210  }
211 
212 private:
213 
214  long m_val; ///< Attribute value
215 
216 };
217 
218 /**
219  * @class HepMC::DoubleAttribute
220  * @brief Attribute that holds a real number as a double.
221  *
222  * @ingroup attributes
223  */
224 class DoubleAttribute : public Attribute {
225 public:
226 
227  /** @brief Default constructor */
229 
230  /** @brief Constructor initializing attribute value */
231  DoubleAttribute(double val): Attribute(), m_val(val) {}
232 
233  /** @brief Implementation of Attribute::from_string */
234  bool from_string(const string &att) {
235 #ifdef HEPMC_HAS_CXX11
236  m_val = std::stod(att);
237 #else
238  m_val = atof( att.c_str() );
239 #endif
240  return true;
241  }
242 
243  /** @brief Implementation of Attribute::to_string */
244  bool to_string(string &att) const {
245  std::ostringstream oss;
246  oss << std::setprecision(std::numeric_limits<double>::digits10)
247  << m_val;
248  att = oss.str();
249  return true;
250  }
251 
252  /** @brief get the value associated to this Attribute. */
253  double value() const {
254  return m_val;
255  }
256 
257  /** @brief set the value associated to this Attribute. */
258  void set_value(double d) {
259  m_val = d;
260  }
261 
262 private:
263 
264  double m_val; ///< Attribute value
265 };
266 
267 /**
268  * @class HepMC::FloatAttribute
269  * @brief Attribute that holds a real number as a float.
270  *
271  * @ingroup attributes
272  */
273 class FloatAttribute : public Attribute {
274 public:
275 
276  /** @brief Default constructor */
278 
279  /** @brief Constructor initializing attribute value */
280  FloatAttribute(float val): Attribute(), m_val(val) {}
281 
282  /** @brief Implementation of Attribute::from_string */
283  bool from_string(const string &att) {
284 #ifdef HEPMC_HAS_CXX11
285  m_val = std::stof(att);
286 #else
287  m_val = float(atof( att.c_str() ));
288 #endif
289  return true;
290  }
291 
292  /** @brief Implementation of Attribute::to_string */
293  bool to_string(string &att) const {
294  std::ostringstream oss;
295  oss << std::setprecision(std::numeric_limits<float>::digits10)
296  << m_val;
297  att = oss.str();
298  return true;
299  }
300 
301  /** @brief get the value associated to this Attribute. */
302  float value() const {
303  return m_val;
304  }
305 
306  /** @brief set the value associated to this Attribute. */
307  void set_value(float f) {
308  m_val = f;
309  }
310 
311 private:
312 
313  float m_val; ///< Attribute value
314 };
315 
316 /**
317  * @class HepMC::StringAttribute
318  * @brief Attribute that holds a string
319  *
320  * Default attribute constructed when reading input files.
321  * It can be then parsed by other attributes or left as a string.
322  *
323  * @ingroup attributes
324  *
325  */
326 class StringAttribute : public Attribute {
327 public:
328 
329  /** @brief Default constructor - empty string */
331 
332  /** @brief String-based constructor
333  *
334  * The Attribute constructor used here marks that this is an unparsed
335  * string that can be (but does not have to be) parsed
336  *
337  */
338  StringAttribute(const string &st):Attribute(st) {}
339 
340  /** @brief Implementation of Attribute::from_string */
341  bool from_string(const string &att) {
342  set_unparsed_string(att);
343  return true;
344  }
345 
346  /** @brief Implementation of Attribute::to_string */
347  bool to_string(string &att) const {
348  att = unparsed_string();
349  return true;
350  }
351 
352  /** @brief get the value associated to this Attribute. */
353  string value() const {
354  return unparsed_string();
355  }
356 
357  /** @brief set the value associated to this Attribute. */
358  void set_value(string s) {
360  }
361 
362 };
363 
364 } // namespace HepMC
365 
366 #endif
const string & unparsed_string() const
Get unparsed string.
bool m_is_parsed
Is this attribute parsed?
bool from_string(const string &att)
Implementation of Attribute::from_string.
virtual bool init(const GenEvent &)
Optionally initialize the attribute after from_string.
void set_value(long l)
set the value associated to this Attribute.
FloatAttribute()
Default constructor.
bool from_string(const string &att)
Implementation of Attribute::from_string.
StringAttribute(const string &st)
String-based constructor.
string m_string
Raw (unparsed) string.
Stores run-related information.
bool from_string(const string &att)
Implementation of Attribute::from_string.
LongAttribute(long val)
Constructor initializing attribute value.
long value() const
get the value associated to this Attribute.
void set_value(double d)
set the value associated to this Attribute.
virtual ~Attribute()
Virtual destructor.
void set_unparsed_string(const string &st)
Set unparsed string.
Stores event-related information.
virtual bool to_string(string &att) const =0
Fill string from class content.
bool from_string(const string &att)
Implementation of Attribute::from_string.
virtual bool init(const GenRunInfo &)
Optionally initialize the attribute after from_string.
void set_value(float f)
set the value associated to this Attribute.
float value() const
get the value associated to this Attribute.
bool to_string(string &att) const
Implementation of Attribute::to_string.
IntAttribute(int val)
Constructor initializing attribute value.
StringAttribute()
Default constructor - empty string.
Attribute(const string &st)
Protected constructor that allows to set string.
bool to_string(string &att) const
Implementation of Attribute::to_string.
void set_value(string s)
set the value associated to this Attribute.
void set_value(int i)
set the value associated to this Attribute.
bool to_string(string &att) const
Implementation of Attribute::to_string.
LongAttribute()
Default constructor.
DoubleAttribute(double val)
Constructor initializing attribute value.
Attribute()
Default constructor.
DoubleAttribute()
Default constructor.
FloatAttribute(float val)
Constructor initializing attribute value.
double value() const
get the value associated to this Attribute.
bool from_string(const string &att)
Implementation of Attribute::from_string.
IntAttribute()
Default constructor.
virtual bool from_string(const string &att)=0
Fill class content from string.
int value() const
get the value associated to this Attribute.
bool to_string(string &att) const
Implementation of Attribute::to_string.
Base class for all attributes.
Definition of template class SmartPointer.
void set_is_parsed(bool flag)
Set is_parsed flag.
bool to_string(string &att) const
Implementation of Attribute::to_string.
bool is_parsed()
Check if this attribute is parsed.
string value() const
get the value associated to this Attribute.