DD4hep - The AIDA detector description toolkit for high energy physics experiments
DD4hep  Rev:Unversioneddirectory
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SegmentationParameter.h
Go to the documentation of this file.
1 /*
2  * SegmentationParameter.h
3  *
4  * Helper class to hold a segmentation parameter with its description.
5  *
6  * Created on: Dec 16, 2013
7  * Author: Christian Grefe, CERN
8  */
9 
10 #ifndef DDSegmentation_SEGMENTATIONPARAMETER_H_
11 #define DDSegmentation_SEGMENTATIONPARAMETER_H_
12 
13 #include <sstream>
14 #include <string>
15 #include <typeinfo>
16 #include <vector>
17 
18 namespace DD4hep {
19 namespace DDSegmentation {
20 
21 
23 inline std::vector<std::string> splitString(const std::string& s, char delimiter = ' ') {
24  std::vector<std::string> elements;
25  std::stringstream ss(s);
26  std::string item;
27  while (std::getline(ss, item, delimiter)) {
28  elements.push_back(item);
29  }
30  return elements;
31 }
32 
34 template<typename TYPE> struct TypeName {
35  static const char* name() {
36  return typeid(TYPE).name();
37  }
38 };
39 
41 template<> struct TypeName<int> {
42  static const char* name() {
43  return "int";
44  }
45 };
46 
48 template<> struct TypeName<float> {
49  static const char* name() {
50  return "float";
51  }
52 };
53 
55 template<> struct TypeName<double> {
56  static const char* name() {
57  return "double";
58  }
59 };
60 
62 template<> struct TypeName<std::string> {
63  static const char* name() {
64  return "string";
65  }
66 };
67 
69 template<> struct TypeName<std::vector<int> > {
70  static const char* name() {
71  return "intvec";
72  }
73 };
74 
76 template<> struct TypeName<std::vector<float> > {
77  static const char* name() {
78  return "floatvec";
79  }
80 };
81 
83 template<> struct TypeName<std::vector<double> > {
84  static const char* name() {
85  return "doublevec";
86  }
87 };
88 
90 template<> struct TypeName<std::vector<std::string> > {
91  static const char* name() {
92  return "stringvec";
93  }
94 };
95 
98 public:
100  enum UnitType {
102  };
105  }
107  const std::string& name() const {
108  return _name;
109  }
111  const std::string& description() const {
112  return _description;
113  }
115  UnitType unitType() const {
116  return _unitType;
117  }
119  virtual std::string type() const = 0;
121  virtual std::string value() const = 0;
123  virtual void setValue(const std::string& value) = 0;
125  virtual std::string defaultValue() const = 0;
127  bool isOptional() const {
128  return _isOptional;
129  }
131  std::string toString() const {
132  std::stringstream s;
133  s << _name << " = " << value();
134  if (not _description.empty()) {
135  s << " (" << _description << ")";
136  }
137  return s.str();
138  }
139 protected:
141  SegmentationParameter(const std::string& nam, const std::string& desc, UnitType unitTyp = NoUnit,
142  bool isOpt = false) :
143  _name(nam), _description(desc), _unitType(unitTyp), _isOptional(isOpt) {
144  }
146  std::string _name;
148  std::string _description;
153 };
154 
155 template<typename TYPE> class TypedSegmentationParameter: public SegmentationParameter {
156 public:
158  TypedSegmentationParameter(const std::string& nam, const std::string& desc, TYPE& val,
159  const TYPE& default_Value, SegmentationParameter::UnitType unitTyp = SegmentationParameter::NoUnit,
160  bool isOpt = false) :
161  SegmentationParameter(nam, desc, unitTyp, isOpt), _value(val), _defaultValue(default_Value) {
162  _value = default_Value;
163  }
164 
166  const TYPE& typedValue() const {
167  return _value;
168  }
169 
171  void setTypedValue(const TYPE& val) {
172  _value = val;
173  }
174 
176  const TYPE& typedDefaultValue() const {
177  return _defaultValue;
178  }
179 
181  std::string type() const {
182  return TypeName<TYPE>::name();
183  }
184 
186  std::string value() const {
187  std::stringstream s;
188  s << _value;
189  return s.str();
190  }
191 
193  void setValue(const std::string& val) {
194  std::stringstream s;
195  s << val;
196  s >> _value;
197  }
198 
200  std::string defaultValue() const {
201  std::stringstream s;
202  s << _defaultValue;
203  return s.str();
204  }
205 
206 protected:
207  TYPE& _value;
209 };
210 
211 template<typename TYPE> class TypedSegmentationParameter<std::vector<TYPE> > : public SegmentationParameter {
212 public:
214  TypedSegmentationParameter(const std::string& nam, const std::string& desc, std::vector<TYPE>& val,
215  const std::vector<TYPE>& defaultVal, SegmentationParameter::UnitType unitTyp =
216  SegmentationParameter::NoUnit, bool isOpt = false) :
217  SegmentationParameter(nam, desc, unitTyp, isOpt), _value(val), _defaultValue(defaultVal) {
218  _value = defaultVal;
219  }
220 
222  const std::vector<TYPE>& typedValue() const {
223  return _value;
224  }
225 
227  void setTypedValue(const std::vector<TYPE>& val) {
228  _value = val;
229  }
230 
232  const std::vector<TYPE>& typedDefaultValue() const {
233  return _defaultValue;
234  }
235 
237  std::string type() const {
238  std::stringstream s;
239  s << TypeName<TYPE>::name() << "vec";
240  return s.str() ;
241  }
242 
244  std::string value() const {
245  std::stringstream s;
246  typename std::vector<TYPE>::const_iterator it = _value.begin();
247  for (; it != _value.end(); ++it) {
248  s << *it;
249  s << " ";
250  }
251  return s.str();
252  }
253 
255  void setValue(const std::string& val) {
256  std::vector<std::string> elements = splitString(val);
257  _value.clear();
258  for (std::vector<std::string>::const_iterator it = elements.begin(); it != elements.end(); ++it) {
259  if (not it->empty()) {
260  TYPE entry;
261  std::stringstream s;
262  s << *it;
263  s >> entry;
264  _value.push_back(entry);
265  }
266  }
267  }
268 
270  std::string defaultValue() const {
271  std::stringstream s;
272  typename std::vector<TYPE>::const_iterator it = _defaultValue.begin();
273  for (; it != _defaultValue.end(); ++it) {
274  s << *it;
275  s << " ";
276  }
277  return s.str();
278  }
279 
280 protected:
281  std::vector<TYPE>& _value;
282  std::vector<TYPE> _defaultValue;
283 
284 };
285 
286 } /* namespace DDSegmentation */
287 } /* namespace DD4hep */
288 #endif /* DDSegmentation_SEGMENTATIONPARAMETER_H_ */
const std::string & description() const
Access to the parameter description.
const TYPE & typedValue() const
Access to the parameter value.
bool _isOptional
Store if parameter is optional.
std::string type() const
Access to the parameter type.
std::vector< std::string > splitString(const std::string &s, char delimiter= ' ')
Helper method to split string into tokens.
std::string value() const
Access to the parameter value in string representation.
Helper class to extract type names.
const std::string & name() const
Access to the parameter name.
UnitType
Defines the parameter unit type (useful to convert to default set of units)
UnitType unitType() const
Access to the unit type.
TGeoShape * s
Definition: Volumes.cpp:294
std::string defaultValue() const
Access to the parameter default value in string representation.
void setValue(const std::string &val)
Set the parameter value in string representation.
TypedSegmentationParameter(const std::string &nam, const std::string &desc, std::vector< TYPE > &val, const std::vector< TYPE > &defaultVal, SegmentationParameter::UnitType unitTyp=SegmentationParameter::NoUnit, bool isOpt=false)
Default constructor.
bool isOptional() const
Check if this parameter is optional.
SegmentationParameter(const std::string &nam, const std::string &desc, UnitType unitTyp=NoUnit, bool isOpt=false)
Default constructor used by derived classes.
void setTypedValue(const std::vector< TYPE > &val)
Set the parameter value.
virtual std::string value() const =0
Access to the parameter value in string representation.
std::string toString() const
Printable version.
virtual void setValue(const std::string &value)=0
Set the parameter value in string representation.
virtual std::string type() const =0
Access to the parameter type.
void setValue(const std::string &val)
Set the parameter value in string representation.
void setTypedValue(const TYPE &val)
Set the parameter value.
std::string _description
The parameter description.
std::string defaultValue() const
Access to the parameter default value in string representation.
const std::vector< TYPE > & typedDefaultValue() const
Access to the parameter default value.
virtual std::string defaultValue() const =0
Access to the parameter default value in string representation.
Class to hold a segmentation parameter with its description.
const TYPE & typedDefaultValue() const
Access to the parameter default value.
std::string value() const
Access to the parameter value in string representation.
const std::vector< TYPE > & typedValue() const
Access to the parameter value.
TypedSegmentationParameter(const std::string &nam, const std::string &desc, TYPE &val, const TYPE &default_Value, SegmentationParameter::UnitType unitTyp=SegmentationParameter::NoUnit, bool isOpt=false)
Default constructor.