KaliVeda
Toolkit for HIC analysis
KVNameValueList.h
1 
4 #ifndef __KVNAMEVALUELIST_H
5 #define __KVNAMEVALUELIST_H
6 
7 #include "KVHashList.h"
8 #include "TNamed.h"
9 #include "TRegexp.h"
10 #include "KVNamedParameter.h"
11 class KVEnv;
12 
116 class KVNameValueList : public TNamed {
117 protected:
119  Bool_t fIgnoreBool=kFALSE;//do not convert "yes", "false", "on", etc. in TEnv file to boolean
120 
121 public:
122  KVNameValueList();
123  KVNameValueList(std::initializer_list<KVNamedParameter>);
124  KVNameValueList(const Char_t* name, const Char_t* title = "");
126 
128 
129  class Iterator {
130  public:
131  typedef std::forward_iterator_tag iterator_category;
133  typedef std::ptrdiff_t difference_type;
136  private:
137  TIter fIter;// iterator over hash list
139  {
141  return reinterpret_cast<KVNamedParameter*>(*fIter);
142  }
143 
144  public:
146  : fIter(static_cast<TIterator*>(nullptr))
147  {}
148  Iterator(const Iterator& other)
149  : fIter(other.fIter)
150  {}
152  : fIter(N->GetList())
153  {
154  fIter.Begin();
155  }
157  : fIter(N.GetList())
158  {
159  fIter.Begin();
160  }
162  {
164 
165  return *(current());
166  }
167  Bool_t operator!= (const Iterator& it) const
168  {
170  return current() != it.current();
171  }
172  Bool_t operator== (const Iterator& it) const
173  {
175  return current() == it.current();
176  }
178  {
180  ++fIter;
181  return *this;
182  }
184  {
187  Iterator tmp(*this);
188  operator++();
189  return tmp;
190  }
192  {
194  if (this != &rhs) { // check self-assignment based on address of object
195  fIter = rhs.fIter;
196  }
197  return *this;
198  }
199  static Iterator End()
200  {
201  return Iterator();
202  }
203  virtual ~Iterator() {}
204 
205  ClassDef(Iterator, 0) //Iterator for KVNameValueList
206  };
207  Iterator begin() const
208  {
210  return Iterator(this);
211  }
212  Iterator end() const
213  {
215  return Iterator::End();
216  }
217 
218  bool Set(const KVString&);
219  KVString Get() const;
220 
221  KVHashList* GetList() const;
222 
223  void Clear(Option_t* opt = "") override;
224  virtual void ClearSelection(const TRegexp&);
225  void Print(Option_t* opt = "") const override;
226  TString List(Option_t* opt = "") const;
227  void ls(Option_t* opt = "") const override;
228 
229  void Copy(TObject& nvl) const override;
230  Int_t Compare(const TObject* nvl) const override;
231 
232  template<typename value_type>
233  void SetValue(const Char_t* name, value_type value)
234  {
239  par ? par->Set(name, value) : fList.Add(new KVNamedParameter(name, value));
240  }
241  void SetValue(const KVNamedParameter&);
242  void SetValue64bit(const Char_t* name, ULong64_t);
243  ULong64_t GetValue64bit(const Char_t* name) const;
244  Bool_t HasValue64bit(const Char_t* name) const;
245 
246  template <typename value_type>
247  void SetValueAt(const Char_t* name, value_type value, Int_t idx)
248  {
255  if (par) {
256  par->Set(value);
257  if (fList.IndexOf(par) != idx) {
258  fList.GetCollection()->Remove(par);
259  fList.AddAt(par, idx);
260  }
261  }
262  else fList.AddAt(new KVNamedParameter(name, value), idx);
263  }
264 
265  template <typename value_type>
266  void SetFirstValue(const Char_t* name, value_type value)
267  {
274  if (par) {
275  par->Set(value);
276  if (fList.First() != par) {
277  fList.GetCollection()->Remove(par);
278  fList.AddFirst(par);
279  }
280  }
282  }
283 
284  template <typename value_type>
285  void SetLastValue(const Char_t* name, value_type value)
286  {
293  if (par) {
294  par->Set(value);
295  if (fList.Last() != par) {
296  fList.GetCollection()->Remove(par);
297  fList.AddLast(par);
298  }
299  }
301  }
302 
303  template <typename value_type>
304  void IncrementValue(const Char_t* name, value_type value)
305  {
311  }
312 
313  template <typename value_type>
314  Bool_t IsValue(const Char_t* name, value_type value) const
315  {
318  if (par) {
320  return (*par) == tmp;
321  }
322  return kFALSE;
323  }
324 
325  KVNamedParameter* FindParameter(const Char_t* name) const;
326  KVNamedParameter* GetParameter(Int_t idx) const;
327  void RemoveParameter(const Char_t* name);
328  Bool_t HasParameter(const Char_t* name) const;
329  template <typename value_type>
330  Bool_t HasParameter(const Char_t* name) const
331  {
333 
335  return (p && p->Is<value_type>());
336  }
337  Bool_t HasIntParameter(const Char_t* name) const
338  {
345  return HasParameter<int>(name);
346  }
347  Bool_t HasBoolParameter(const Char_t* name) const
348  {
349  return HasParameter<bool>(name);
350  }
351  Bool_t HasDoubleParameter(const Char_t* name) const
352  {
359  return HasParameter<double>(name);
360  }
361  Bool_t HasStringParameter(const Char_t* name) const
362  {
363  return HasParameter<TString>(name);
364  }
365  Bool_t HasNumericParameter(const Char_t* name) const
366  {
374  }
375  Int_t GetNameIndex(const Char_t* name) const;
376  const Char_t* GetNameAt(Int_t idx) const;
377  Int_t GetNpar() const;
379  {
380  return GetNpar();
381  }
382  Bool_t IsEmpty() const
383  {
384  return GetNpar() == 0;
385  }
386 
387  template <typename value_type>
388  value_type GetValue(const Char_t* name) const
389  {
393 
395  return (par ? par->Get<value_type>() : KVNamedParameter::DefaultValue<value_type>());
396  }
397 
398  Int_t GetIntValue(const Char_t* name) const
399  {
400  return GetValue<int>(name);
401  }
402  Bool_t GetBoolValue(const Char_t* name) const
403  {
404  return GetValue<bool>(name);
405  }
406  Double_t GetDoubleValue(const Char_t* name) const
407  {
408  return GetValue<double>(name);
409  }
410  const Char_t* GetStringValue(const Char_t* name) const
411  {
412  return GetValue<cstring>(name);
413  }
414  TString GetTStringValue(const Char_t* name) const;
415 
416  template <typename value_type>
417  value_type GetValue(Int_t idx) const
418  {
422  KVNamedParameter* par = GetParameter(idx);
423  return (par ? par->Get<value_type>() : KVNamedParameter::DefaultValue<value_type>());
424  }
425 
427  {
428  return GetValue<int>(idx);
429  }
431  {
432  return GetValue<bool>(idx);
433  }
435  {
436  return GetValue<double>(idx);
437  }
438  const Char_t* GetStringValue(Int_t idx) const
439  {
440  return GetValue<cstring>(idx);
441  }
442  TString GetTStringValue(Int_t idx) const;
443 
444  virtual void ReadEnvFile(const Char_t* filename);
445  void ReadEnv(const TEnv&);
446  std::unique_ptr<KVEnv> ProduceEnvFile();
447  virtual void WriteEnvFile(const Char_t* filename);
448 
449  void Sort(Bool_t order = kSortAscending)
450  {
453  fList.Sort(order);
454  }
455 
456  void Concatenate(const KVNameValueList& nvl);
457 
458  void WriteClass(const Char_t* classname, const Char_t* classdesc, const Char_t* base_class = "");
459 
460  void SetIgnoreBool(Bool_t ignore = kTRUE)
461  {
466  fIgnoreBool = ignore;
467  }
468  void SetFromEnv(TEnv* tenv, const TString& prefix = "");
469  void WriteToEnv(TEnv* tenv, const TString& prefix = "");
470  void Merge(const KVNameValueList&);
471  void AddValue(const KVNamedParameter& p);
472 
473  ClassDefOverride(KVNameValueList, 4) //A general-purpose list of parameters
474 };
475 #endif
int Int_t
bool Bool_t
char Char_t
constexpr Bool_t kFALSE
double Double_t
const char Option_t
#define ClassDef(name, id)
#define ClassDefOverride(name, id)
#define N
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
char name[80]
Extension of TEnv to allow the writing of comments in the file.
Definition: KVEnv.h:18
Extended version of ROOT THashList.
Definition: KVHashList.h:29
void Sort(Bool_t order=kSortAscending)
Definition: KVHashList.h:43
std::forward_iterator_tag iterator_category
KVNamedParameter * pointer
Iterator(const Iterator &other)
KVNamedParameter & operator*() const
KVNamedParameter & reference
Iterator(const KVNameValueList *N)
Iterator(const KVNameValueList &N)
Bool_t operator==(const Iterator &it) const
Bool_t operator!=(const Iterator &it) const
const Iterator & operator++()
KVNamedParameter * current() const
Iterator & operator=(const Iterator &rhs)
std::ptrdiff_t difference_type
KVNamedParameter value_type
Handles lists of named parameters with different types, a list of KVNamedParameter objects.
KVNamedParameter * GetParameter(Int_t idx) const
return the parameter object with index idx
Iterator begin() const
void Copy(TObject &nvl) const override
Int_t GetIntValue(const Char_t *name) const
Double_t GetDoubleValue(const Char_t *name) const
Bool_t HasValue64bit(const Char_t *name) const
void SetValue(const Char_t *name, value_type value)
std::unique_ptr< KVEnv > ProduceEnvFile()
Put all name-value pairs in this list as a TEnv format.
Bool_t HasNumericParameter(const Char_t *name) const
void RemoveParameter(const Char_t *name)
void SetValue64bit(const Char_t *name, ULong64_t)
void SetFromEnv(TEnv *tenv, const TString &prefix="")
void IncrementValue(const Char_t *name, value_type value)
const Char_t * GetNameAt(Int_t idx) const
void ls(Option_t *opt="") const override
Int_t GetNpar() const
return the number of stored parameters
Bool_t HasStringParameter(const Char_t *name) const
void ReadEnv(const TEnv &)
Bool_t IsEmpty() const
virtual void ClearSelection(const TRegexp &)
void SetFirstValue(const Char_t *name, value_type value)
virtual void ReadEnvFile(const Char_t *filename)
Bool_t IsValue(const Char_t *name, value_type value) const
Int_t GetEntries() const
TString List(Option_t *opt="") const
void SetValueAt(const Char_t *name, value_type value, Int_t idx)
void Concatenate(const KVNameValueList &nvl)
virtual void WriteEnvFile(const Char_t *filename)
Write all name-value pairs in this list as a TEnv format file.
Int_t GetNameIndex(const Char_t *name) const
void Merge(const KVNameValueList &)
Iterator end() const
KVNamedParameter * FindParameter(const Char_t *name) const
return the parameter object with the asking name
Int_t Compare(const TObject *nvl) const override
KVNameValueList & operator=(const KVNameValueList &)
KVHashList fList
list of KVNamedParameter objects
Bool_t HasIntParameter(const Char_t *name) const
void Sort(Bool_t order=kSortAscending)
value_type GetValue(Int_t idx) const
void Clear(Option_t *opt="") override
Bool_t GetBoolValue(Int_t idx) const
Double_t GetDoubleValue(Int_t idx) const
void WriteClass(const Char_t *classname, const Char_t *classdesc, const Char_t *base_class="")
Bool_t HasBoolParameter(const Char_t *name) const
Bool_t GetBoolValue(const Char_t *name) const
ULong64_t GetValue64bit(const Char_t *name) const
KVString Get() const
value_type GetValue(const Char_t *name) const
const Char_t * GetStringValue(Int_t idx) const
Int_t GetIntValue(Int_t idx) const
const Char_t * GetStringValue(const Char_t *name) const
Bool_t HasParameter(const Char_t *name) const
void SetLastValue(const Char_t *name, value_type value)
bool Set(const KVString &)
Bool_t HasDoubleParameter(const Char_t *name) const
Bool_t HasParameter(const Char_t *name) const
void Print(Option_t *opt="") const override
void AddValue(const KVNamedParameter &p)
void SetIgnoreBool(Bool_t ignore=kTRUE)
KVHashList * GetList() const
TString GetTStringValue(const Char_t *name) const
void WriteToEnv(TEnv *tenv, const TString &prefix="")
A generic named parameter storing values of different types.
void Set(const char *, const char *)
void Add(const KVNamedParameter &p)
TObject * First() const override
void Add(TObject *obj) override
void AddLast(TObject *obj) override
TObject * Last() const override
TSeqCollection * GetCollection() const
void AddAt(TObject *obj, Int_t idx) override
void AddFirst(TObject *obj) override
Extension of ROOT TString class which allows backwards compatibility with ROOT v3....
Definition: KVString.h:73
virtual TObject * Remove(TObject *obj)=0
TIter & Begin()
virtual Int_t IndexOf(const TObject *obj) const
Bool_t IsFloat() const
unsigned long long ULong64_t