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 = "");
127 
130 
131  class Iterator {
132  public:
133  typedef std::forward_iterator_tag iterator_category;
135  typedef std::ptrdiff_t difference_type;
138  private:
139  TIter fIter;// iterator over hash list
141  {
143  return reinterpret_cast<KVNamedParameter*>(*fIter);
144  }
145 
146  public:
148  : fIter(static_cast<TIterator*>(nullptr))
149  {}
150  Iterator(const Iterator& other)
151  : fIter(other.fIter)
152  {}
154  : fIter(N->GetList())
155  {
156  fIter.Begin();
157  }
159  : fIter(N.GetList())
160  {
161  fIter.Begin();
162  }
164  {
166 
167  return *(current());
168  }
169  Bool_t operator!= (const Iterator& it) const
170  {
172  return current() != it.current();
173  }
174  Bool_t operator== (const Iterator& it) const
175  {
177  return current() == it.current();
178  }
180  {
182  ++fIter;
183  return *this;
184  }
186  {
189  Iterator tmp(*this);
190  operator++();
191  return tmp;
192  }
194  {
196  if (this != &rhs) { // check self-assignment based on address of object
197  fIter = rhs.fIter;
198  }
199  return *this;
200  }
201  static Iterator End()
202  {
203  return Iterator();
204  }
205  virtual ~Iterator() {}
206 
207  ClassDef(Iterator, 0) //Iterator for KVNameValueList
208  };
209  Iterator begin() const
210  {
212  return Iterator(this);
213  }
214  Iterator end() const
215  {
217  return Iterator::End();
218  }
219 
220  bool Set(const KVString&);
221  KVString Get() const;
222 
223  KVHashList* GetList() const;
224 
225  void Clear(Option_t* opt = "") override;
226  virtual void ClearSelection(const TRegexp&);
227  void Print(Option_t* opt = "") const override;
228  TString List(Option_t* opt = "") const;
229  void ls(Option_t* opt = "") const override;
230 
231  void Copy(TObject& nvl) const override;
232  Int_t Compare(const TObject* nvl) const override;
233 
234  template<typename value_type>
235  void SetValue(const Char_t* name, value_type value)
236  {
241  par ? par->Set(name, value) : fList.Add(new KVNamedParameter(name, value));
242  }
243  void SetValue(const KVNamedParameter&);
244  void SetValue64bit(const Char_t* name, ULong64_t);
245  ULong64_t GetValue64bit(const Char_t* name) const;
246  Bool_t HasValue64bit(const Char_t* name) const;
247 
248  template <typename value_type>
249  void SetValueAt(const Char_t* name, value_type value, Int_t idx)
250  {
257  if (par) {
258  par->Set(value);
259  if (fList.IndexOf(par) != idx) {
260  fList.GetCollection()->Remove(par);
261  fList.AddAt(par, idx);
262  }
263  }
264  else fList.AddAt(new KVNamedParameter(name, value), idx);
265  }
266 
267  template <typename value_type>
268  void SetFirstValue(const Char_t* name, value_type value)
269  {
276  if (par) {
277  par->Set(value);
278  if (fList.First() != par) {
279  fList.GetCollection()->Remove(par);
280  fList.AddFirst(par);
281  }
282  }
284  }
285 
286  template <typename value_type>
287  void SetLastValue(const Char_t* name, value_type value)
288  {
295  if (par) {
296  par->Set(value);
297  if (fList.Last() != par) {
298  fList.GetCollection()->Remove(par);
299  fList.AddLast(par);
300  }
301  }
303  }
304 
305  template <typename value_type>
306  void IncrementValue(const Char_t* name, value_type value)
307  {
313  }
314 
315  template <typename value_type>
316  Bool_t IsValue(const Char_t* name, value_type value) const
317  {
320  if (par) {
322  return (*par) == tmp;
323  }
324  return kFALSE;
325  }
326 
327  KVNamedParameter* FindParameter(const Char_t* name) const;
328  KVNamedParameter* GetParameter(Int_t idx) const;
329  void RemoveParameter(const Char_t* name);
330  Bool_t HasParameter(const Char_t* name) const;
331  template <typename value_type>
332  Bool_t HasParameter(const Char_t* name) const
333  {
335 
337  return (p && p->Is<value_type>());
338  }
339  Bool_t HasIntParameter(const Char_t* name) const
340  {
347  return HasParameter<int>(name);
348  }
349  Bool_t HasBoolParameter(const Char_t* name) const
350  {
351  return HasParameter<bool>(name);
352  }
353  Bool_t HasDoubleParameter(const Char_t* name) const
354  {
361  return HasParameter<double>(name);
362  }
363  Bool_t HasStringParameter(const Char_t* name) const
364  {
365  return HasParameter<TString>(name);
366  }
367  Bool_t HasNumericParameter(const Char_t* name) const
368  {
376  }
377  Int_t GetNameIndex(const Char_t* name) const;
378  const Char_t* GetNameAt(Int_t idx) const;
379  Int_t GetNpar() const;
381  {
382  return GetNpar();
383  }
384  Bool_t IsEmpty() const
385  {
386  return GetNpar() == 0;
387  }
388 
389  KVNamedParameter& operator[](const TString& name) const
390  {
395  if (!par)
396  throw std::runtime_error(Form("KVNameValueList::operator[](const TString&): no parameter with name '%s' in list", name.Data()));
397  return *par;
398  }
399  template <typename value_type>
400  value_type GetValue(const Char_t* name) const
401  {
405 
407  return (par ? par->Get<value_type>() : KVNamedParameter::DefaultValue<value_type>());
408  }
409 
410  Int_t GetIntValue(const Char_t* name) const
411  {
412  return GetValue<int>(name);
413  }
414  Bool_t GetBoolValue(const Char_t* name) const
415  {
416  return GetValue<bool>(name);
417  }
418  Double_t GetDoubleValue(const Char_t* name) const
419  {
420  return GetValue<double>(name);
421  }
422  const Char_t* GetStringValue(const Char_t* name) const
423  {
424  return GetValue<cstring>(name);
425  }
426  TString GetTStringValue(const Char_t* name) const;
427 
428  template <typename value_type>
429  value_type GetValue(Int_t idx) const
430  {
434  KVNamedParameter* par = GetParameter(idx);
435  return (par ? par->Get<value_type>() : KVNamedParameter::DefaultValue<value_type>());
436  }
437 
439  {
440  return GetValue<int>(idx);
441  }
443  {
444  return GetValue<bool>(idx);
445  }
447  {
448  return GetValue<double>(idx);
449  }
450  const Char_t* GetStringValue(Int_t idx) const
451  {
452  return GetValue<cstring>(idx);
453  }
454  TString GetTStringValue(Int_t idx) const;
455 
456  virtual void ReadEnvFile(const Char_t* filename);
457  void ReadEnv(const TEnv&);
458  std::unique_ptr<KVEnv> ProduceEnvFile();
459  virtual void WriteEnvFile(const Char_t* filename);
460 
461  void Sort(Bool_t order = kSortAscending)
462  {
465  fList.Sort(order);
466  }
467 
468  void Concatenate(const KVNameValueList& nvl);
469 
470  void WriteClass(const Char_t* classname, const Char_t* classdesc, const Char_t* base_class = "");
471 
472  void SetIgnoreBool(Bool_t ignore = kTRUE)
473  {
478  fIgnoreBool = ignore;
479  }
480  void SetFromEnv(TEnv* tenv, const TString& prefix = "");
481  void WriteToEnv(TEnv* tenv, const TString& prefix = "");
482  void Merge(const KVNameValueList&);
483  void AddValue(const KVNamedParameter& p);
484 
485  TString AsSQLSelection() const;
486 
487  ClassDefOverride(KVNameValueList, 4) //A general-purpose list of parameters
488 };
489 #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]
char * Form(const char *fmt,...)
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
KVNameValueList & operator=(KVNameValueList &&)=default
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
KVNamedParameter & operator[](const TString &name) const
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
TString AsSQLSelection() 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
KVNameValueList(KVNameValueList &&)=default
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