Fork me on GitHub

source: git/external/HepMC3/Attribute.h@ 06630ef

Last change on this file since 06630ef was 95a917c, checked in by Pavel Demin <pavel.demin@…>, 3 years ago

add HepMC3 library

  • Property mode set to 100644
File size: 31.6 KB
RevLine 
[95a917c]1// -*- C++ -*-
2//
3// This file is part of HepMC
4// Copyright (C) 2014-2020 The HepMC collaboration (see AUTHORS for details)
5//
6#ifndef HEPMC3_ATTRIBUTE_H
7#define HEPMC3_ATTRIBUTE_H
8/**
9 * @file Attribute.h
10 * @brief Definition of \b class Attribute, \b class IntAttribute and \b class StringAttribute
11 *
12 * @class HepMC3::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 <map>
28
29#include "HepMC3/GenParticle_fwd.h"
30#include "HepMC3/GenVertex_fwd.h"
31
32/** Deprecated */
33using std::string;
34
35namespace HepMC3 {
36
37/** @brief Forward declaration of GenEvent. */
38class GenEvent;
39
40/** @brief Forward declaration of GenRunInfo. */
41class GenRunInfo;
42
43/** @brief Forward declaration of GenParticle. */
44// class GenParticle;
45class Attribute {
46//
47// Constructors
48//
49public:
50 /** @brief Default constructor */
51 //Note: m_event should be set to nullptr in case event is deleted!
52 Attribute():m_is_parsed(true) { m_event=nullptr; }
53
54 /** @brief Virtual destructor */
55 virtual ~Attribute() {}
56
57protected:
58 /** @brief Protected constructor that allows to set string
59 *
60 * Used when parsing attributes from file. An StringAttribute class
61 * object is made, which uses this constructor to signify that
62 * it just holds string without parsing it.
63 *
64 * @note There should be no need for user class to ever use this constructor
65 */
66 //Note: m_event should be set to nullptr n case event is deleted!
67 explicit Attribute(const std::string &st):m_is_parsed(false),m_string(st) { m_event=nullptr; }
68
69 /** @brief GenEvent is a friend */
70 friend class GenEvent;
71
72//
73// Virtual Functions
74//
75public:
76 /** @brief Fill class content from string.
77 */
78 virtual bool from_string(const std::string & att) = 0;
79
80 /** @brief Optionally initialize the attribute after from_string.
81 */
82 virtual bool init() {
83 return true;
84 }
85
86 /** @brief Optionally initialize the attribute after from_string
87 *
88 * Is passed a reference to the GenRunInfo object to which the
89 * Attribute belongs.
90 */
91 virtual bool init(const GenRunInfo & ) {
92 return true;
93 }
94
95 /** @brief Fill string from class content */
96 virtual bool to_string(std::string &att) const = 0;
97
98//
99// Accessors
100//
101public:
102 /** @brief Check if this attribute is parsed */
103 bool is_parsed() const { return m_is_parsed; }
104
105 /** @brief Get unparsed string */
106 const std::string& unparsed_string() const { return m_string; }
107
108 /** return the GenEvent to which this Attribute belongs, if at all. */
109 const GenEvent * event() const {
110 return m_event;
111 }
112
113 /** return the GenParticle to which this Attribute belongs, if at all. */
114 GenParticlePtr particle() {
115 return m_particle;
116 }
117
118 /** return the GenParticle to which this Attribute belongs, if at all. */
119 ConstGenParticlePtr particle() const {
120 return std::const_pointer_cast<GenParticle>(m_particle);
121 }
122
123 /** return the GenVertex to which this Attribute belongs, if at all. */
124 GenVertexPtr vertex() {
125 return m_vertex;
126 }
127
128 /** return the GenVertex to which this Attribute belongs, if at all. */
129 ConstGenVertexPtr vertex() const {
130 return std::const_pointer_cast<GenVertex>(m_vertex);
131 }
132
133protected:
134 /** @brief Set is_parsed flag */
135 void set_is_parsed(bool flag) { m_is_parsed = flag; }
136
137 /** @brief Set unparsed string */
138 void set_unparsed_string(const std::string &st) { m_string = st; }
139
140//
141// Fields
142//
143private:
144 bool m_is_parsed; //!< Is this attribute parsed?
145 std::string m_string; //!< Raw (unparsed) string
146 const GenEvent * m_event; //!< Possibility to be aware of the
147 //! controlling GenEvent object.
148 GenParticlePtr m_particle; //!< Particle to which assigned.
149 GenVertexPtr m_vertex; //!< Vertex to which assigned.
150};
151
152/**
153 * @class HepMC3::IntAttribute
154 * @brief Attribute that holds an Integer implemented as an int
155 *
156 * @ingroup attributes
157 */
158class IntAttribute : public Attribute {
159public:
160
161 /** @brief Default constructor */
162 IntAttribute():Attribute(),m_val(0) {}
163
164 /** @brief Constructor initializing attribute value */
165 IntAttribute(int val):Attribute(),m_val(val) {}
166
167 /** @brief Implementation of Attribute::from_string */
168 bool from_string(const std::string &att) override {
169 m_val = atoi( att.c_str() );
170 return true;
171 }
172
173 /** @brief Implementation of Attribute::to_string */
174 bool to_string(std::string &att) const override{
175 att = std::to_string(m_val);
176 return true;
177 }
178
179 /** @brief get the value associated to this Attribute. */
180 int value() const {
181 return m_val;
182 }
183
184 /** @brief set the value associated to this Attribute. */
185 void set_value(const int& i) {
186 m_val = i;
187 }
188
189private:
190 int m_val; ///< Attribute value
191};
192
193/**
194 * @class HepMC3::LongAttribute
195 * @brief Attribute that holds an Integer implemented as an int
196 *
197 * @ingroup attributes
198 */
199class LongAttribute : public Attribute {
200public:
201
202 /** @brief Default constructor */
203 LongAttribute(): Attribute(), m_val(0) {}
204
205 /** @brief Constructor initializing attribute value */
206 LongAttribute(long val): Attribute(), m_val(val) {}
207
208 /** @brief Implementation of Attribute::from_string */
209 bool from_string(const std::string &att) override{
210 m_val = atol( att.c_str() );
211 return true;
212 }
213
214 /** @brief Implementation of Attribute::to_string */
215 bool to_string(std::string &att) const override{
216 att = std::to_string(m_val);
217 return true;
218 }
219
220 /** @brief get the value associated to this Attribute. */
221 long value() const {
222 return m_val;
223 }
224
225 /** @brief set the value associated to this Attribute. */
226 void set_value(const long& l) {
227 m_val = l;
228 }
229
230private:
231
232 long m_val; ///< Attribute value
233
234};
235
236/**
237 * @class HepMC3::DoubleAttribute
238 * @brief Attribute that holds a real number as a double.
239 *
240 * @ingroup attributes
241 */
242class DoubleAttribute : public Attribute {
243public:
244
245 /** @brief Default constructor */
246 DoubleAttribute(): Attribute(), m_val(0.0) {}
247
248 /** @brief Constructor initializing attribute value */
249 DoubleAttribute(double val): Attribute(), m_val(val) {}
250
251 /** @brief Implementation of Attribute::from_string */
252 bool from_string(const std::string &att) override{
253 m_val = atof( att.c_str() );
254 return true;
255 }
256
257 /** @brief Implementation of Attribute::to_string */
258 bool to_string(std::string &att) const override{
259 std::ostringstream oss;
260 oss << std::setprecision(std::numeric_limits<double>::digits10)
261 << m_val;
262 att = oss.str();
263 return true;
264 }
265
266 /** @brief get the value associated to this Attribute. */
267 double value() const {
268 return m_val;
269 }
270
271 /** @brief set the value associated to this Attribute. */
272 void set_value(const double& d) {
273 m_val = d;
274 }
275
276private:
277
278 double m_val; ///< Attribute value
279};
280
281/**
282 * @class HepMC3::FloatAttribute
283 * @brief Attribute that holds a real number as a float.
284 *
285 * @ingroup attributes
286 */
287class FloatAttribute : public Attribute {
288public:
289
290 /** @brief Default constructor */
291 FloatAttribute(): Attribute(), m_val(0.0) {}
292
293 /** @brief Constructor initializing attribute value */
294 FloatAttribute(float val): Attribute(), m_val(val) {}
295
296 /** @brief Implementation of Attribute::from_string */
297 bool from_string(const std::string &att) override{
298 m_val = float(atof( att.c_str() ));
299 return true;
300 }
301
302 /** @brief Implementation of Attribute::to_string */
303 bool to_string(std::string &att) const override{
304 std::ostringstream oss;
305 oss << std::setprecision(std::numeric_limits<float>::digits10)
306 << m_val;
307 att = oss.str();
308 return true;
309 }
310
311 /** @brief get the value associated to this Attribute. */
312 float value() const {
313 return m_val;
314 }
315
316 /** @brief set the value associated to this Attribute. */
317 void set_value(const float& f) {
318 m_val = f;
319 }
320
321private:
322
323 float m_val; ///< Attribute value
324};
325
326/**
327 * @class HepMC3::StringAttribute
328 * @brief Attribute that holds a string
329 *
330 * Default attribute constructed when reading input files.
331 * It can be then parsed by other attributes or left as a string.
332 *
333 * @ingroup attributes
334 *
335 */
336class StringAttribute : public Attribute {
337public:
338
339 /** @brief Default constructor - empty string */
340 StringAttribute():Attribute() {}
341
342 /** @brief String-based constructor
343 *
344 * The Attribute constructor used here marks that this is an unparsed
345 * string that can be (but does not have to be) parsed
346 *
347 */
348 StringAttribute(const std::string &st):Attribute(st) {}
349
350 /** @brief Implementation of Attribute::from_string */
351 bool from_string(const std::string &att) override{
352 set_unparsed_string(att);
353 return true;
354 }
355
356 /** @brief Implementation of Attribute::to_string */
357 bool to_string(std::string &att) const override{
358 att = unparsed_string();
359 return true;
360 }
361
362 /** @brief get the value associated to this Attribute. */
363 std::string value() const {
364 return unparsed_string();
365 }
366
367 /** @brief set the value associated to this Attribute. */
368 void set_value(const std::string& s) {
369 set_unparsed_string(s);
370 }
371
372};
373
374/**
375 * @class HepMC3::CharAttribute
376 * @brief Attribute that holds an Chareger implemented as an int
377 *
378 * @ingroup attributes
379 */
380class CharAttribute : public Attribute {
381public:
382
383 /** @brief Default constructor */
384 CharAttribute():Attribute(),m_val(0) {}
385
386 /** @brief Constructor initializing attribute value */
387 CharAttribute(char val):Attribute(),m_val(val) {}
388
389 /** @brief Implementation of Attribute::from_string */
390 bool from_string(const std::string &att) override {
391 if (att.size())
392 {
393 m_val = att.at(0);
394 return true;
395 }
396 return false;
397 }
398
399 /** @brief Implementation of Attribute::to_string */
400 bool to_string(std::string &att) const override {
401 att = std::to_string(m_val);
402 return true;
403 }
404
405 /** @brief get the value associated to this Attribute. */
406 char value() const {
407 return m_val;
408 }
409
410 /** @brief set the value associated to this Attribute. */
411 void set_value(const char& i) {
412 m_val = i;
413 }
414
415private:
416 char m_val; ///< Attribute value
417};
418
419/**
420 * @class HepMC3::LongLongAttribute
421 * @brief Attribute that holds an Integer implemented as an int
422 *
423 * @ingroup attributes
424 */
425class LongLongAttribute : public Attribute {
426public:
427
428 /** @brief Default constructor */
429 LongLongAttribute(): Attribute(), m_val(0) {}
430
431 /** @brief Constructor initializing attribute value */
432 LongLongAttribute(long long val): Attribute(), m_val(val) {}
433
434 /** @brief Implementation of Attribute::from_string */
435 bool from_string(const std::string &att) override{
436 m_val = atoll( att.c_str() );
437 return true;
438 }
439
440 /** @brief Implementation of Attribute::to_string */
441 bool to_string(std::string &att) const override{
442 att = std::to_string(m_val);
443 return true;
444 }
445
446 /** @brief get the value associated to this Attribute. */
447 long long value() const {
448 return m_val;
449 }
450
451 /** @brief set the value associated to this Attribute. */
452 void set_value(const long long& l) {
453 m_val = l;
454 }
455
456private:
457
458 long long m_val; ///< Attribute value
459
460};
461
462/**
463 * @class HepMC3::LongDoubleAttribute
464 * @brief Attribute that holds a real number as a double.
465 *
466 * @ingroup attributes
467 */
468class LongDoubleAttribute : public Attribute {
469public:
470
471 /** @brief Default constructor */
472 LongDoubleAttribute(): Attribute(), m_val(0.0) {}
473
474 /** @brief Constructor initializing attribute value */
475 LongDoubleAttribute(long double val): Attribute(), m_val(val) {}
476
477 /** @brief Implementation of Attribute::from_string */
478 bool from_string(const std::string &att) override {
479 m_val = strtold( att.c_str(),NULL);
480 return true;
481 }
482
483 /** @brief Implementation of Attribute::to_string */
484 bool to_string(std::string &att) const override{
485 std::ostringstream oss;
486 oss << std::setprecision(std::numeric_limits<long double>::digits10)
487 << m_val;
488 att = oss.str();
489 return true;
490 }
491
492 /** @brief get the value associated to this Attribute. */
493 long double value() const {
494 return m_val;
495 }
496
497 /** @brief set the value associated to this Attribute. */
498 void set_value(const long double& d) {
499 m_val = d;
500 }
501
502private:
503
504 long double m_val; ///< Attribute value
505};
506
507
508
509/**
510 * @class HepMC3::UIntAttribute
511 * @brief Attribute that holds an unsigned int
512 *
513 * @ingroup attributes
514 */
515class UIntAttribute : public Attribute {
516public:
517
518 /** @brief Default constructor */
519 UIntAttribute():Attribute(),m_val(0) {}
520
521 /** @brief Constructor initializing attribute value */
522 UIntAttribute(unsigned int val):Attribute(),m_val(val) {}
523
524 /** @brief Implementation of Attribute::from_string */
525 bool from_string(const std::string &att) override{
526 m_val = strtoul(att.c_str(), NULL, 0);
527 return true;
528 }
529
530 /** @brief Implementation of Attribute::to_string */
531 bool to_string(std::string &att) const override{
532 att = std::to_string(m_val);
533 return true;
534 }
535
536 /** @brief get the value associated to this Attribute. */
537 unsigned int value() const {
538 return m_val;
539 }
540
541 /** @brief set the value associated to this Attribute. */
542 void set_value(const unsigned int& i) {
543 m_val = i;
544 }
545
546private:
547 unsigned int m_val; ///< Attribute value
548};
549
550
551
552/**
553 * @class HepMC3::ULongAttribute
554 * @brief Attribute that holds an unsigned long
555 *
556 * @ingroup attributes
557 */
558class ULongAttribute : public Attribute {
559public:
560
561 /** @brief Default constructor */
562 ULongAttribute():Attribute(),m_val(0) {}
563
564 /** @brief Constructor initializing attribute value */
565 ULongAttribute(unsigned long val):Attribute(),m_val(val) {}
566
567 /** @brief Implementation of Attribute::from_string */
568 bool from_string(const std::string &att) override{
569 m_val = strtoul(att.c_str(), NULL, 0);
570 return true;
571 }
572
573 /** @brief Implementation of Attribute::to_string */
574 bool to_string(std::string &att) const override{
575 att = std::to_string(m_val);
576 return true;
577 }
578
579 /** @brief get the value associated to this Attribute. */
580 unsigned long value() const {
581 return m_val;
582 }
583
584 /** @brief set the value associated to this Attribute. */
585 void set_value(const unsigned long& i) {
586 m_val = i;
587 }
588
589private:
590 unsigned long m_val; ///< Attribute value
591};
592
593
594/**
595 * @class HepMC3::ULongLongAttribute
596 * @brief Attribute that holds an unsigned long long
597 *
598 * @ingroup attributes
599 */
600class ULongLongAttribute : public Attribute {
601public:
602
603 /** @brief Default constructor */
604 ULongLongAttribute():Attribute(),m_val(0) {}
605
606 /** @brief Constructor initializing attribute value */
607 ULongLongAttribute(unsigned long long val):Attribute(),m_val(val) {}
608
609 /** @brief Implementation of Attribute::from_string */
610 bool from_string(const std::string &att) override{
611 m_val = strtoull(att.c_str(), NULL, 0);
612 return true;
613 }
614
615 /** @brief Implementation of Attribute::to_string */
616 bool to_string(std::string &att) const override{
617 att = std::to_string(m_val);
618 return true;
619 }
620
621 /** @brief get the value associated to this Attribute. */
622 unsigned long long value() const {
623 return m_val;
624 }
625
626 /** @brief set the value associated to this Attribute. */
627 void set_value(const unsigned long long& i) {
628 m_val = i;
629 }
630
631private:
632 unsigned long long m_val; ///< Attribute value
633};
634/**
635 * @class HepMC3::BoolAttribute
636 * @brief Attribute that holds an Booleger implemented as an int
637 *
638 * @ingroup attributes
639 */
640class BoolAttribute : public Attribute {
641public:
642
643 /** @brief Default constructor */
644 BoolAttribute():Attribute(),m_val(false) {}
645
646 /** @brief Constructor initializing attribute value */
647 BoolAttribute(bool val):Attribute(),m_val(val) {}
648
649 /** @brief Implementation of Attribute::from_string */
650 bool from_string(const std::string &att) override{
651 if (att.size()!=1) return false;
652 if(att==std::string("1")) {m_val = true; return true;}
653 if(att==std::string("0")) {m_val = false; return true;}
654 return false;
655 }
656
657 /** @brief Implementation of Attribute::to_string */
658 bool to_string(std::string &att) const override{
659 att = std::to_string(m_val);
660 return true;
661 }
662
663 /** @brief get the value associated to this Attribute. */
664 bool value() const {
665 return m_val;
666 }
667
668 /** @brief set the value associated to this Attribute. */
669 void set_value(const bool& i) {
670 m_val = i;
671 }
672
673private:
674 bool m_val; ///< Attribute value
675};
676
677/**
678 * @class HepMC3::VectorCharAttribute
679 * @brief Attribute that holds a vector of charegers of type char
680 *
681 * @ingroup attributes
682 */
683class VectorCharAttribute : public Attribute {
684public:
685
686 /** @brief Default constructor */
687 VectorCharAttribute():Attribute(),m_val() {}
688
689 /** @brief Constructor initializing attribute value */
690 VectorCharAttribute(std::vector<char> val):Attribute(),m_val(val) {}
691
692 /** @brief Implementation of Attribute::from_string */
693 bool from_string(const std::string &att) override {
694 char datafoo;
695 m_val.clear();
696 std::stringstream datastream(att);
697 while (datastream >> datafoo) m_val.push_back(datafoo);
698 return true;
699 }
700
701 /** @brief Implementation of Attribute::to_string */
702 bool to_string(std::string &att) const override{
703 att.clear();
704 for (auto a: m_val) {if (att.length()) att+=" "; att+=std::to_string(a);}
705 return true;
706 }
707
708 /** @brief get the value associated to this Attribute. */
709 std::vector<char> value() const {
710 return m_val;
711 }
712
713 /** @brief set the value associated to this Attribute. */
714 void set_value(const std::vector<char>& i) {
715 m_val = i;
716 }
717
718private:
719 std::vector<char> m_val; ///< Attribute value
720};
721
722/**
723 * @class HepMC3::VectorFloatAttribute
724 * @brief Attribute that holds a vector of floategers of type float
725 *
726 * @ingroup attributes
727 */
728class VectorFloatAttribute : public Attribute {
729public:
730
731 /** @brief Default constructor */
732 VectorFloatAttribute():Attribute(),m_val() {}
733
734 /** @brief Constructor initializing attribute value */
735 VectorFloatAttribute(std::vector<float> val):Attribute(),m_val(val) {}
736
737 /** @brief Implementation of Attribute::from_string */
738 bool from_string(const std::string &att) override {
739 float datafoo;
740 m_val.clear();
741 std::stringstream datastream(att);
742 while (datastream >> datafoo) m_val.push_back(datafoo);
743 return true;
744 }
745
746 /** @brief Implementation of Attribute::to_string */
747 bool to_string(std::string &att) const override{
748 att.clear();
749 for (auto a: m_val) {if (att.length()) att+=" "; att+=std::to_string(a);}
750 return true;
751 }
752
753 /** @brief get the value associated to this Attribute. */
754 std::vector<float> value() const {
755 return m_val;
756 }
757
758 /** @brief set the value associated to this Attribute. */
759 void set_value(const std::vector<float>& i) {
760 m_val = i;
761 }
762
763private:
764 std::vector<float> m_val; ///< Attribute value
765};
766
767
768/**
769 * @class HepMC3::VectorLongDoubleAttribute
770 * @brief Attribute that holds a vector of long doubleegers of type long double
771 *
772 * @ingroup attributes
773 */
774class VectorLongDoubleAttribute : public Attribute {
775public:
776
777 /** @brief Default constructor */
778 VectorLongDoubleAttribute():Attribute(),m_val() {}
779
780 /** @brief Constructor initializing attribute value */
781 VectorLongDoubleAttribute(std::vector<long double> val):Attribute(),m_val(val) {}
782
783 /** @brief Implementation of Attribute::from_string */
784 bool from_string(const std::string &att) override {
785 long double datafoo;
786 m_val.clear();
787 std::stringstream datastream(att);
788 while (datastream >> datafoo) m_val.push_back(datafoo);
789 return true;
790 }
791
792 /** @brief Implementation of Attribute::to_string */
793 bool to_string(std::string &att) const override{
794 att.clear();
795 for (auto a: m_val) {if (att.length()) att+=" "; att+=std::to_string(a);}
796 return true;
797 }
798
799 /** @brief get the value associated to this Attribute. */
800 std::vector<long double> value() const {
801 return m_val;
802 }
803
804 /** @brief set the value associated to this Attribute. */
805 void set_value(const std::vector<long double>& i) {
806 m_val = i;
807 }
808
809private:
810 std::vector<long double> m_val; ///< Attribute value
811};
812
813
814
815/**
816 * @class HepMC3::VectorLongLongAttribute
817 * @brief Attribute that holds a vector of long longegers of type long long
818 *
819 * @ingroup attributes
820 */
821class VectorLongLongAttribute : public Attribute {
822public:
823
824 /** @brief Default constructor */
825 VectorLongLongAttribute():Attribute(),m_val() {}
826
827 /** @brief Constructor initializing attribute value */
828 VectorLongLongAttribute(std::vector<long long> val):Attribute(),m_val(val) {}
829
830 /** @brief Implementation of Attribute::from_string */
831 bool from_string(const std::string &att) override {
832 long long datafoo;
833 m_val.clear();
834 std::stringstream datastream(att);
835 while (datastream >> datafoo) m_val.push_back(datafoo);
836 return true;
837 }
838
839 /** @brief Implementation of Attribute::to_string */
840 bool to_string(std::string &att) const override{
841 att.clear();
842 for (auto a: m_val) {if (att.length()) att+=" "; att+=std::to_string(a);}
843 return true;
844 }
845
846 /** @brief get the value associated to this Attribute. */
847 std::vector<long long> value() const {
848 return m_val;
849 }
850
851 /** @brief set the value associated to this Attribute. */
852 void set_value(const std::vector<long long>& i) {
853 m_val = i;
854 }
855
856private:
857 std::vector<long long> m_val; ///< Attribute value
858};
859
860/**
861 * @class HepMC3::VectorUIntAttribute
862 * @brief Attribute that holds a vector of unsigned integers of type unsigned int
863 *
864 * @ingroup attributes
865 */
866class VectorUIntAttribute : public Attribute {
867public:
868
869 /** @brief Default constructor */
870 VectorUIntAttribute():Attribute(),m_val() {}
871
872 /** @brief Constructor initializing attribute value */
873 VectorUIntAttribute(std::vector<unsigned int> val):Attribute(),m_val(val) {}
874
875 /** @brief Implementation of Attribute::from_string */
876 bool from_string(const std::string &att) override {
877 unsigned int datafoo;
878 m_val.clear();
879 std::stringstream datastream(att);
880 while (datastream >> datafoo) m_val.push_back(datafoo);
881 return true;
882 }
883
884 /** @brief Implementation of Attribute::to_string */
885 bool to_string(std::string &att) const override{
886 att.clear();
887 for (auto a: m_val) {if (att.length()) att+=" "; att+=std::to_string(a);}
888 return true;
889 }
890
891 /** @brief get the value associated to this Attribute. */
892 std::vector<unsigned int> value() const {
893 return m_val;
894 }
895
896 /** @brief set the value associated to this Attribute. */
897 void set_value(const std::vector<unsigned int>& i) {
898 m_val = i;
899 }
900
901private:
902 std::vector<unsigned int> m_val; ///< Attribute value
903};
904
905/**
906 * @class HepMC3::VectorULongAttribute
907 * @brief Attribute that holds a vector of unsigned longegers of type unsigned long
908 *
909 * @ingroup attributes
910 */
911class VectorULongAttribute : public Attribute {
912public:
913
914 /** @brief Default constructor */
915 VectorULongAttribute():Attribute(),m_val() {}
916
917 /** @brief Constructor initializing attribute value */
918 VectorULongAttribute(std::vector<unsigned long> val):Attribute(),m_val(val) {}
919
920 /** @brief Implementation of Attribute::from_string */
921 bool from_string(const std::string &att) override {
922 unsigned long datafoo;
923 m_val.clear();
924 std::stringstream datastream(att);
925 while (datastream >> datafoo) m_val.push_back(datafoo);
926 return true;
927 }
928
929 /** @brief Implementation of Attribute::to_string */
930 bool to_string(std::string &att) const override{
931 att.clear();
932 for (auto a: m_val) {if (att.length()) att+=" "; att+=std::to_string(a);}
933 return true;
934 }
935
936 /** @brief get the value associated to this Attribute. */
937 std::vector<unsigned long> value() const {
938 return m_val;
939 }
940
941 /** @brief set the value associated to this Attribute. */
942 void set_value(const std::vector<unsigned long>& i) {
943 m_val = i;
944 }
945
946private:
947 std::vector<unsigned long> m_val; ///< Attribute value
948};
949
950
951/**
952 * @class HepMC3::VectorULongLongAttribute
953 * @brief Attribute that holds a vector of unsigned long longegers of type unsigned long long
954 *
955 * @ingroup attributes
956 */
957class VectorULongLongAttribute : public Attribute {
958public:
959
960 /** @brief Default constructor */
961 VectorULongLongAttribute():Attribute(),m_val() {}
962
963 /** @brief Constructor initializing attribute value */
964 VectorULongLongAttribute(std::vector<unsigned long long> val):Attribute(),m_val(val) {}
965
966 /** @brief Implementation of Attribute::from_string */
967 bool from_string(const std::string &att) override {
968 unsigned long long datafoo;
969 m_val.clear();
970 std::stringstream datastream(att);
971 while (datastream >> datafoo) m_val.push_back(datafoo);
972 return true;
973 }
974
975 /** @brief Implementation of Attribute::to_string */
976 bool to_string(std::string &att) const override{
977 att.clear();
978 for (auto a: m_val) {if (att.length()) att+=" "; att+=std::to_string(a);}
979 return true;
980 }
981
982 /** @brief get the value associated to this Attribute. */
983 std::vector<unsigned long long> value() const {
984 return m_val;
985 }
986
987 /** @brief set the value associated to this Attribute. */
988 void set_value(const std::vector<unsigned long long>& i) {
989 m_val = i;
990 }
991
992private:
993 std::vector<unsigned long long> m_val; ///< Attribute value
994};
995
996/**
997 * @class HepMC3::VectorIntAttribute
998 * @brief Attribute that holds a vector of integers of type int
999 *
1000 * @ingroup attributes
1001 */
1002class VectorIntAttribute : public Attribute {
1003public:
1004
1005 /** @brief Default constructor */
1006 VectorIntAttribute():Attribute(),m_val() {}
1007
1008 /** @brief Constructor initializing attribute value */
1009 VectorIntAttribute(std::vector<int> val):Attribute(),m_val(val) {}
1010
1011 /** @brief Implementation of Attribute::from_string */
1012 bool from_string(const std::string &att) override {
1013 int datafoo;
1014 m_val.clear();
1015 std::stringstream datastream(att);
1016 while (datastream >> datafoo) m_val.push_back(datafoo);
1017 return true;
1018 }
1019
1020 /** @brief Implementation of Attribute::to_string */
1021 bool to_string(std::string &att) const override{
1022 att.clear();
1023 for (auto a: m_val) {if (att.length()) att+=" "; att+=std::to_string(a);}
1024 return true;
1025 }
1026
1027 /** @brief get the value associated to this Attribute. */
1028 std::vector<int> value() const {
1029 return m_val;
1030 }
1031
1032 /** @brief set the value associated to this Attribute. */
1033 void set_value(const std::vector<int>& i) {
1034 m_val = i;
1035 }
1036
1037private:
1038 std::vector<int> m_val; ///< Attribute value
1039};
1040
1041/**
1042 * @class HepMC3::VectorIntAttribute
1043 * @brief Attribute that holds a vector of integers of type int
1044 *
1045 * @ingroup attributes
1046 */
1047class VectorLongIntAttribute : public Attribute {
1048public:
1049
1050 /** @brief Default constructor */
1051 VectorLongIntAttribute():Attribute(),m_val() {}
1052
1053 /** @brief Constructor initializing attribute value */
1054 VectorLongIntAttribute(std::vector<long int> val):Attribute(),m_val(val) {}
1055
1056 /** @brief Implementation of Attribute::from_string */
1057 bool from_string(const std::string &att) override {
1058 long int datafoo;
1059 m_val.clear();
1060 std::stringstream datastream(att);
1061 while (datastream >> datafoo) m_val.push_back(datafoo);
1062 return true;
1063 }
1064
1065 /** @brief Implementation of Attribute::to_string */
1066 bool to_string(std::string &att) const override{
1067 att.clear();
1068 for (auto a: m_val) {if (att.length()) att+=" "; att+=std::to_string(a);}
1069 return true;
1070 }
1071
1072 /** @brief get the value associated to this Attribute. */
1073 std::vector<long int> value() const {
1074 return m_val;
1075 }
1076
1077 /** @brief set the value associated to this Attribute. */
1078 void set_value(const std::vector<long int>& i) {
1079 m_val = i;
1080 }
1081
1082private:
1083 std::vector<long int> m_val; ///< Attribute value
1084};
1085
1086/**
1087 * @class HepMC3::VectorIntAttribute
1088 * @brief Attribute that holds a vector of FPs of type double
1089 *
1090 * @ingroup attributes
1091 */
1092class VectorDoubleAttribute : public Attribute {
1093public:
1094
1095 /** @brief Default constructor */
1096 VectorDoubleAttribute():Attribute(),m_val() {}
1097
1098 /** @brief Constructor initializing attribute value */
1099 VectorDoubleAttribute(std::vector<double> val):Attribute(),m_val(val) {}
1100
1101 /** @brief Implementation of Attribute::from_string */
1102 bool from_string(const std::string &att) override {
1103 double datafoo;
1104 m_val.clear();
1105 std::stringstream datastream(att);
1106 while (datastream >> datafoo) m_val.push_back(datafoo);
1107 return true;
1108 }
1109
1110 /** @brief Implementation of Attribute::to_string */
1111 bool to_string(std::string &att) const override{
1112 att.clear();
1113 for (auto a: m_val) {if (att.length()) att+=" "; att+=std::to_string(a);}
1114 return true;
1115 }
1116
1117 /** @brief get the value associated to this Attribute. */
1118 std::vector<double> value() const {
1119 return m_val;
1120 }
1121
1122 /** @brief set the value associated to this Attribute. */
1123 void set_value(const std::vector<double>& i) {
1124 m_val = i;
1125 }
1126
1127private:
1128 std::vector<double> m_val; ///< Attribute value
1129};
1130
1131
1132/**
1133 * @class HepMC3::VectorIntAttribute
1134 * @brief Attribute that holds a vector of FPs of type string
1135 *
1136 * @ingroup attributes
1137 */
1138class VectorStringAttribute : public Attribute {
1139public:
1140
1141 /** @brief Default constructor */
1142 VectorStringAttribute():Attribute(),m_val() {}
1143
1144 /** @brief Constructor initializing attribute value */
1145 VectorStringAttribute(std::vector<std::string> val):Attribute(),m_val(val) {}
1146
1147 /** @brief Implementation of Attribute::from_string */
1148 bool from_string(const string &att) override {
1149 size_t posb = att.find_first_not_of(' ');
1150 do {
1151 size_t pose = att.find_first_of(' ', posb);
1152 m_val.push_back(att.substr(posb, pose - posb));
1153 posb = att.find_first_not_of(' ', pose);
1154 } while (posb != std::string::npos);
1155 return true;
1156 }
1157
1158 /** @brief Implementation of Attribute::to_string */
1159 bool to_string(std::string &att) const override{
1160 att.clear();
1161 for (auto a: m_val) {if (att.length()) att+=" "; att+=a;}
1162 return true;
1163 }
1164
1165 /** @brief get the value associated to this Attribute. */
1166 std::vector<std::string> value() const {
1167 return m_val;
1168 }
1169
1170 /** @brief set the value associated to this Attribute. */
1171 void set_value(const std::vector<std::string>& i) {
1172 m_val = i;
1173 }
1174
1175private:
1176 std::vector<std::string> m_val; ///< Attribute value
1177};
1178
1179
1180} // namespace HepMC3
1181
1182#endif
Note: See TracBrowser for help on using the repository browser.