Diana Software
QVdt.hh
Go to the documentation of this file.
1 
17 #ifndef _Q_VDT_H
18 #define _Q_VDT_H
19 #include <string>
20 #include <iostream>
21 #include <set>
22 #include <vector>
23 #include "QDiana.hh"
24 
25 
26 class QVdt {
27 
28  public:
29 
30  enum QVdt_type {
31  Int_QVdt = 'I',
32  Double_QVdt = 'F',
33  String_QVdt = 'S',
34  Vector_QVdt = 'V',
35  Unassigned_QVdt = 'U'
36  };
37 
38  typedef std::vector<QVdt> QVdt_vector;
39 
41 
42  QVdt (const std::string& value, const std::string& name = "", QVdt_type type = Unassigned_QVdt);
43 
44  QVdt (long int value): name_s("") { value_i = value; type_internal = Int_QVdt; fBuiltFromString = false;}
45 
46  QVdt (double value): name_s("") { value_f = value; type_internal = Double_QVdt; fBuiltFromString = false;}
47 
48  template<typename V> QVdt (const std::vector<V, std::allocator<V> >& value): name_s("") { // This should stay in .cc file, but
49  // gcc does not have export for templates
50 
52  std::back_insert_iterator<std::vector<QVdt> > inserter(value_v);
53  std::copy (value.begin (), value.end (), inserter);
54  fBuiltFromString = false;
55  }
56  // Since cast is not called in some onstructor operator these methods are necessary
57  QVdt (int value): name_s("") { value_i = value; type_internal = Int_QVdt; fBuiltFromString = false;}
58 
59  QVdt (short value): name_s("") { value_i = value; type_internal = Int_QVdt; fBuiltFromString = false;}
60 
61  QVdt (char value): name_s("") { value_i = value; type_internal = Int_QVdt; fBuiltFromString = false;}
62  QVdt (unsigned long value): name_s("") { value_i = value; type_internal = Int_QVdt; fBuiltFromString = false;}
63  QVdt (unsigned short value): name_s("") { value_i = value; type_internal = Int_QVdt; fBuiltFromString = false;}
64  QVdt (unsigned char value): name_s("") { value_i = value; type_internal = Int_QVdt; fBuiltFromString = false;}
65  QVdt (float value): name_s("") { value_f = value; type_internal = Double_QVdt; fBuiltFromString = false;}
66 
67  void SetName (const std::string& name) { name_s = name; }
68  std::string GetName () const { return name_s; }
69 
70  const QVdt& operator= (const std::string& value);
71  const QVdt& operator= (long int value);
72  const QVdt& operator= (double value);
73 
74  template<typename V> const QVdt& operator= (const std::vector<V, std::allocator<V> >& value) { // This should stay in .cc file, but
75  // gcc does not have export for templates
78  std::back_insert_iterator<std::vector<QVdt> > inserter(value_v);
79  std::copy (value.begin (), value.end (), inserter);
80  fBuiltFromString = false;
81  return *this;
82  }
83 
84  // Since cast is not called in assignement operator these methods are necessary
85  const QVdt& operator = (int value) { return (*this) = (long int)(value); }
86  const QVdt& operator = (short value) { return (*this) = (long int)(value); }
87  const QVdt& operator = (char value) { return (*this) = (long int)(value); }
88  const QVdt& operator = (unsigned long value) { return (*this) = (long int)(value); }
89  const QVdt& operator = (unsigned short value) { return (*this) = (long int)(value); }
90  const QVdt& operator = (unsigned char value) { return (*this) = (long int)(value); }
91  const QVdt& operator = (float value) { return (*this) = double(value); }
92  const QVdt& operator = (const QVdt& other);
93 
94  bool is_valid () const { return (type_internal == Unassigned_QVdt) ? 0 : 1; }
95 
96  QVdt_type GetType () const { return type_internal; }
97  bool IsScalar() const { return type_internal == Int_QVdt || type_internal == Double_QVdt; }
98  bool IsVector() const { return type_internal == Vector_QVdt; }
99  long int GetInt () const;
100  double GetDouble () const;
101  const std::string& GetString () const;
102  bool GetBool () const { return bool (GetInt ()); }
103  const QVdt_vector& GetVector () const;
104  std::vector<int> GetVectorInt() const { return GetStdVector<int>(); }
105  std::vector<double> GetVectorDouble() const { return GetStdVector<double>(); }
106  std::vector<std::string> GetVectorString() const { return GetStdVector<std::string>(); }
107  std::vector<bool> GetVectorBool() const { return GetStdVector<bool>(); }
108 
109  std::set<int> GetSetInt() const { return GetStdSet<int>(); }
110  std::set<std::string> GetSetString() const { return GetStdSet<std::string>(); }
111 
112  private:
114 
115  long int value_i;
116  double value_f;
117  mutable std::string value_s;
119 
120  std::string name_s;
122  void CheckType (QVdt_type new_type) const; // Throws an exception if the internal type
123  std::string ToString() const;
124  // is different from the required type
125  QVdt_type SearchType (const std::string& str);
126  void AssignValue (const std::string& str);
127 
128  template<class T> std::vector<T> GetStdVector() const
129  {
130  const QVdt_vector& v = GetVector();
131  std::vector<T> out;
132  for(size_t i = 0; i < v.size(); i++) {
133  out.push_back(v[i].Get<T>());
134  }
135  return out;
136  }
137 
138  template<class T> std::set<T> GetStdSet() const
139  {
140  const QVdt_vector &v=GetVector();
141  std::set<T> out;
142  for(size_t i=0; i < v.size(); i++){
143  out.insert(v[i].Get<T>());
144  }
145  return out;
146  }
147 
148  template<class T> T Get() const {
149  DianaThrow("QVdt::Get<T> not implemented for this type");
150  T dummy;
151  return dummy;
152  }
153 };
154 
155 template <> int QVdt::Get<int>() const;
156 template <> double QVdt::Get<double>() const;
157 template <> std::string QVdt::Get<std::string>() const;
158 template <> bool QVdt::Get<bool>() const;
159 
160 std::ostream& operator<< (std::ostream&, const QVdt&);
161 
162 
163 #endif
#define DianaThrow(obj)
Definition: QDianaDebug.hh:26
std::ostream & operator<<(std::ostream &, const QVdt &)
Definition: QVdt.cc:253
Variable Data Type.
Definition: QVdt.hh:26
void CheckType(QVdt_type new_type) const
Definition: QVdt.cc:239
QVdt(short value)
Definition: QVdt.hh:59
QVdt_type
Definition: QVdt.hh:30
@ Vector_QVdt
Definition: QVdt.hh:34
@ String_QVdt
Definition: QVdt.hh:33
@ Double_QVdt
Definition: QVdt.hh:32
@ Int_QVdt
Definition: QVdt.hh:31
@ Unassigned_QVdt
Definition: QVdt.hh:35
bool GetBool() const
Definition: QVdt.hh:102
T Get() const
Definition: QVdt.hh:148
std::set< T > GetStdSet() const
Definition: QVdt.hh:138
double GetDouble() const
Definition: QVdt.cc:219
std::vector< int > GetVectorInt() const
Definition: QVdt.hh:104
QVdt(long int value)
Definition: QVdt.hh:44
QVdt_vector value_v
Definition: QVdt.hh:118
bool fBuiltFromString
Definition: QVdt.hh:121
double value_f
Definition: QVdt.hh:116
std::string value_s
Definition: QVdt.hh:117
void AssignValue(const std::string &str)
Definition: QVdt.cc:38
std::set< std::string > GetSetString() const
Definition: QVdt.hh:110
std::vector< T > GetStdVector() const
Definition: QVdt.hh:128
QVdt(unsigned char value)
Definition: QVdt.hh:64
QVdt()
Definition: QVdt.hh:40
const QVdt & operator=(const std::string &value)
Definition: QVdt.cc:161
long int value_i
Definition: QVdt.hh:115
std::string ToString() const
Definition: QVdt.cc:111
std::vector< QVdt > QVdt_vector
Definition: QVdt.hh:38
std::vector< bool > GetVectorBool() const
Definition: QVdt.hh:107
std::string name_s
Definition: QVdt.hh:120
std::set< int > GetSetInt() const
Definition: QVdt.hh:109
const std::string & GetString() const
Definition: QVdt.cc:225
QVdt(const std::vector< V, std::allocator< V > > &value)
Definition: QVdt.hh:48
std::vector< std::string > GetVectorString() const
Definition: QVdt.hh:106
QVdt(unsigned long value)
Definition: QVdt.hh:62
QVdt(double value)
Definition: QVdt.hh:46
std::string GetName() const
Definition: QVdt.hh:68
const QVdt_vector & GetVector() const
Definition: QVdt.cc:233
QVdt_type GetType() const
Definition: QVdt.hh:96
long int GetInt() const
Definition: QVdt.cc:213
QVdt(int value)
Definition: QVdt.hh:57
bool IsVector() const
Definition: QVdt.hh:98
QVdt_type type_internal
Definition: QVdt.hh:113
QVdt(unsigned short value)
Definition: QVdt.hh:63
QVdt(float value)
Definition: QVdt.hh:65
QVdt(char value)
Definition: QVdt.hh:61
void SetName(const std::string &name)
Definition: QVdt.hh:67
QVdt_type SearchType(const std::string &str)
Definition: QVdt.cc:10
std::vector< double > GetVectorDouble() const
Definition: QVdt.hh:105
bool IsScalar() const
Definition: QVdt.hh:97
bool is_valid() const
Definition: QVdt.hh:94