KaliVeda  1.12/06
Heavy-Ion Analysis Toolkit
KVNameValueList.cpp
Go to the documentation of this file.
1 //Created by KVClassFactory on Tue Jun 29 14:44:22 2010
2 //Author: bonnet
3 
4 #include "KVClassFactory.h"
5 #include "KVNameValueList.h"
6 #include "Riostream.h"
7 #include <KVEnv.h>
8 #include <TROOT.h>
9 
10 using namespace std;
11 
13 
14 //______________________________________________
15 
16 
20  : fList(), fIgnoreBool(kFALSE)
21 {
22  // Default constructor
23  fList.SetOwner(kTRUE);
24 }
25 
26 
27 
37 
38 KVNameValueList::KVNameValueList(std::initializer_list<KVNamedParameter> l)
39 {
40  // Constructor using an initializer list for a given set of KVNamedParameter objects,
41  // i.e. this contructor makes it possible to do:
42  //
43  //~~~~{.cpp}
44  //KVNameValueList list{{"A",1.234},{"B",false},{"C","hello"}};
45  //
46  //KVNameValueList list2 = {{"A",1.234},{"B",false},{"C","hello"}};
47  //~~~~
48 
49  for (auto& p : l) SetValue(p);
50 }
51 
52 
53 
59 
61  : TNamed(name, title), fList(), fIgnoreBool(kFALSE)
62 {
63  // Ctor with name & title
64  //
65  // if name contains a comma-separated list of parameter/value pairs,
66  // it will be used to initialise the list (no name set)
68 
69  if (Set(name)) SetName("");
70 }
71 
72 
73 
76 
78 {
79  // Copy constructor
80  NVL.Copy(*this);
82 }
83 
84 
85 
88 
90 {
91  // Destructor
92  fList.Clear();// will delete objects in list if owner
93 }
94 
95 
96 
98 
100 {
101  if (&o != this) o.Copy(*this);
102  return (*this);
103 }
104 
105 
106 
117 
119 {
120  // If list contains a comma-separated list of parameter/value pairs
121  //
122  //~~~~~~~~~~~~~~~~~~~
123  //list = "param1=val1,param2=val2,..."
124  //~~~~~~~~~~~~~~~~~~~
125  //
126  // then use it to initialise the parameters of the list, and return true (any existing parameters will be removed).
127  //
128  // If list does not contain at least one '=' character, do nothing and return false.
129 
130  if (!list.Contains("=")) return false;
131 
132  Clear();
133  list.Begin(",");
134  while (!list.End()) {
135  KVString pair = list.Next(kTRUE);
136  pair.Begin("=");
137  KVString parname = pair.Next(kTRUE);
138  KVString parval = pair.Next(kTRUE);
139  if (parval.IsDigit()) {
140  // integer number
141  SetValue(parname, parval.Atoi());
142  }
143  else if (parval.IsFloat()) {
144  // real number
145  SetValue(parname, parval.Atof());
146  }
147  else {
148  // string
149  SetValue(parname, parval);
150  }
151  }
152  return true;
153 }
154 
155 
156 
160 
162 {
163  //return the pointeur of the KVHashList where
164  //parameters are stored with their values
165  return (KVHashList*)&fList;
166 }
167 
168 
169 
173 
175 {
176  // Copy this to the nvl object.
177  // Any existing parameters will be destroyed
178 
179  TNamed::Copy(nvl);
180  KVNameValueList& _obj = (KVNameValueList&)nvl;
181  fList.Copy(_obj.fList);
182  _obj.fIgnoreBool = fIgnoreBool;
183 }
184 
185 
186 
190 
192 {
193  //Clear all the stored parameters
194  //Deletes the parameter objects if owner & opt!="nodelete"
195  fList.Clear(opt);
196 }
197 
198 
199 
205 
207 {
208  // Remove from list all parameters whose name matches the regular expression
209  // Examples:
210  // remove all parameters starting with "toto": TRegexp sel("^toto")
211  // remove all parameters with "toto" in name: TRegexp sel("toto")
212 
213  TList toBeRemoved;
214  Int_t np1 = GetNpar();
215  for (Int_t ii = 0; ii < np1; ii += 1) {
216  TString name = GetParameter(ii)->GetName();
217  if (name.Contains(sel)) toBeRemoved.Add(new TNamed(name.Data(), ""));
218  }
219  if (toBeRemoved.GetEntries()) {
220  TIter next(&toBeRemoved);
221  TNamed* tbr;
222  while ((tbr = (TNamed*)next())) RemoveParameter(tbr->GetName());
223  toBeRemoved.Delete();
224  }
225 }
226 
227 
228 
233 
234 void KVNameValueList::Print(Option_t* option) const
235 {
236  // Print stored parameters (name, and value)
237  // Option can be used to select type of parameters to print:
238  // option = "int", "double", or "string"
239 
240  if (!GetNpar()) return;
241 
243  cout << "KVNameValueList::" << GetName() << " : " << GetTitle() << " (" << this << ")" << endl;
245  for (Int_t ii = 0; ii < GetNpar(); ii += 1) {
246  GetParameter(ii)->ls(option);
247  }
249 }
250 
251 
252 
254 
256 {
257  if (TString(GetName()) != "") cout << GetName();
258  else cout << "KVNameValueList";
259  cout << " : ";
260  for (int i = 0; i < GetNpar(); ++i) {
261  cout << GetParameter(i)->GetName() << "=";
262  switch (GetParameter(i)->GetType()) {
264  cout << GetParameter(i)->GetDouble();
265  break;
267  cout << GetParameter(i)->GetInt();
268  break;
270  cout << GetParameter(i)->GetString();
271  break;
273  cout << boolalpha << GetParameter(i)->GetBool();
274  break;
275  }
276  if (i < GetNpar() - 1) cout << ",";
277  }
278  cout << endl;
279 }
280 
281 
282 
286 
288 {
289  //set if the KVNameValueList owns its objects or not
290  //by default it is owner
291  fList.SetOwner(enable);
292 }
293 
294 
295 
299 
301 {
302  //return kTRUE if the list owns its objects
303  //kFALSE if not
304  return fList.IsOwner();
305 }
306 
307 
308 
312 
314 {
315  // Compare the contents of two KVNameValueList
316  // Returns the number of same parameters (name and value)
317 
318  KVNameValueList* nvl = (KVNameValueList*)obj;
319  Int_t neq = 0;
320  Int_t np1 = GetNpar();
321  Int_t np2 = nvl->GetNpar();
322  for (Int_t ii = 0; ii < np1; ii += 1) {
323  for (Int_t jj = 0; jj < np2; jj += 1) {
324 
325  if (*(GetParameter(ii)) == *(GetParameter(jj))) neq += 1;
326  }
327  }
328  return neq;
329 
330 }
331 
332 
333 
336 
338 {
339  // add (or replace) a parameter with the same name, type & value as 'p'
340 
342  par ? par->Set(p.GetName(), p) : fList.Add(new KVNamedParameter(p));
343 
344 }
345 
346 
347 
351 
353 {
354  // Store a 64-bit integer in the list as two 32-bit parameters with
355  // names 'name_up' and 'name_lo'
356 
357  TString parname = name;
358  parname += "_hi";
359  SetValue(parname, (Int_t)(x >> 32));
360  parname = name;
361  parname += "_lo";
362  SetValue(parname, (Int_t)((x << 32) >> 32));
363 }
364 
365 
366 
370 
372 {
373  // Return a 64-bit integer stored as two 32-bit parameters with
374  // names 'name_up' and 'name_lo'
375 
376  ULong64_t lo, hi;
377  TString parname = name;
378  parname += "_lo";
379  lo = (UInt_t)GetIntValue(parname);
380  parname = name;
381  parname += "_hi";
382  hi = (UInt_t)GetIntValue(parname);
383  ULong64_t x = (ULong64_t)((hi << 32) + lo);
384  return x;
385 }
386 
387 
388 
392 
394 {
395  // Returns kTRUE if 'name' is stored as a 64-bit value i.e. if
396  // integer parameters 'name_lo' and 'name_hi' are defined
397 
398  TString parname_hi = name;
399  parname_hi += "_hi";
400  TString parname_lo = name;
401  parname_lo += "_lo";
402  return (HasIntParameter(parname_hi) && HasIntParameter(parname_lo));
403 }
404 
405 
406 
412 
414 {
415  // if a parameter with the same name & type as 'p' exists,
416  // add numerical value of p to value of parameter in list,
417  // or for strings we add to a comma-separated list of strings.
418  // otherwise, add a copy of p to list
419 
421  par ? par->Add(p) : fList.Add(new KVNamedParameter(p));
422 }
423 
424 
425 
428 
430 {
431  //return the parameter object with the asking name
432  return (KVNamedParameter*)fList.FindObject(name);
433 }
434 
435 
436 
439 
441 {
442  //return the parameter object with index idx
443  return (KVNamedParameter*)fList.At(idx);
444 }
445 
446 
447 
451 
453 {
454  //remove parameter from the list,
455  //Warning the TNamed object associated is deleted
456 
457  KVNamedParameter* par = FindParameter(name);
458  if (par) {
459  fList.Remove(par);
460  delete par;
461  }
462 }
463 
464 
465 
471 
473 {
474  //Check if there is a parameter with the asked name
475  //in the list
476  //kTRUE, parameter already present
477  //kFALSE, if not
478  return (FindParameter(name) != nullptr);
479 }
480 
481 
482 
487 
489 {
490  //return the position in the list of a given parameter
491  //using its name
492  //return -1 if no parameter with such name are present
493 
494  TObject* par = 0;
495  Int_t idx = 0;
496  TIter next(&fList);
497  while ((par = next())) {
498  if (!strcmp(par->GetName(), name)) return idx;
499  idx++;
500  }
501  Error("GetNameIndex", "Parameter \"%s\" not found, -1 returned", name);
502  return -1;
503 }
504 
505 
506 
512 
514 {
515  //return the name of the parameter store at the idx position
516  //in the list
517  //if the idx is greater than the number of stored parameters
518  //return empty string
519 
520  if (idx >= GetNpar()) {
521  Error("GetNameAt", "index has to be less than %d, empty string is returned", GetNpar());
522  return "";
523  }
524  return fList.At(idx)->GetName();
525 }
526 
527 
528 
533 
535 {
536  //return the value in TString format
537  //for a parameter using its name
538  //return string "-1" if no parameter with such name are present
539 
540  KVNamedParameter* par = FindParameter(name);
541  if (!par) {
542  Error("GetStringValue(const Char_t*)", "\"%s\" does not correspond to an existing parameter, default value \"-1\" is returned", name);
543  return "-1";
544  }
545  return par->GetTString();
546 }
547 
548 
549 
552 
554 {
555  //return the number of stored parameters
556  return fList.GetEntries();
557 }
558 
559 
560 
565 
567 {
568  //return the value in string format
569  //for a parameter using its position
570  //return -1 idx is greater than the number of stored parameters
571  static TString tmp("-1");
572  if (idx >= GetNpar()) {
573  Error("GetStringValue(Int_t)", "index has to be less than %d, \"-1\" is returned\n", GetNpar());
574  return tmp;
575  }
576  return GetParameter(idx)->GetTString();
577 }
578 
579 
580 
597 
599 {
600  // Read all name-value pairs in the TEnv format file and store in list.
601  // Clears any previously stored values.
602  //
603  // values are read as strings from the TEnv and we use
604  // TString::IsDigit, TString::IsFloat to decide whether to store
605  // them as integers, floats, or strings.
606  // booleans are recognized as: TRUE, FALSE, ON, OFF, YES, NO, OK, NOT
607  // (to disable this feature and read such values as strings, call
608  // SetIgnoreBool(kTRUE))
609  //
610  // Special case:
611  // if the parameter name contains the string NumberList
612  // then we store the value string as is, as in this case
613  // it is assumed to be the string representation of a
614  // KVNumberList (easily confused with floating point numbers)
615 
616  Clear();
617  KVEnv env_file;
618  Int_t status = env_file.ReadFile(filename, kEnvAll);
619  if (status == -1) {
620  Error("ReadEnvFile", "The file %s does not exist", filename);
621  return;
622  }
623  THashList* name_value_list = env_file.GetTable();
624  TIter next_nv(name_value_list);
625  TEnvRec* nv_pair;
626  while ((nv_pair = (TEnvRec*)next_nv())) {
627  TString parname(nv_pair->GetName());
628  if (parname == "KVNameValueList.Name") SetName(nv_pair->GetValue());
629  else if (parname == "KVNameValueList.Title") SetTitle(nv_pair->GetValue());
630  else if (parname.Contains("NumberList")) SetValue(parname, nv_pair->GetValue());
631  else {
632  TString parval(nv_pair->GetValue());
633  if (parval.IsDigit()) SetValue(parname, parval.Atoi());
634  else if (parval.IsFloat()) SetValue(parname, parval.Atof());
635  else {
636  TString PARVAL(parval);
637  PARVAL.ToUpper();
638  if (!fIgnoreBool && (PARVAL == "TRUE" || PARVAL == "FALSE" || PARVAL == "ON" || PARVAL == "OFF"
639  || PARVAL == "YES" || PARVAL == "NO" || PARVAL == "OK" || PARVAL == "NOT"))
640  SetValue(parname, (Bool_t)env_file.GetValue(parname, 0));
641  else SetValue(parname, parval);
642  }
643  }
644  }
645 }
646 
647 
648 
652 
654 {
655  // Put all name-value pairs in this list as a TEnv format.
656  // delete after use
657  KVEnv* envfile = new KVEnv();
658  envfile->SetValue("KVNameValueList.Name", GetName());
659  envfile->SetValue("KVNameValueList.Title", GetTitle());
660  WriteToEnv(envfile);
661  return envfile;
662 }
663 
664 
665 
668 
670 {
671  // Write all name-value pairs in this list as a TEnv format file.
672  KVEnv* envfile = ProduceEnvFile();
673  envfile->SetRcName(filename);
674  envfile->Save();
675  delete envfile;
676 }
677 
678 
679 
680 
682 
684 {
685  TIter it(nvl.GetList());
686  KVNamedParameter* par = 0;
687  while ((par = (KVNamedParameter*)it())) SetValue(*par);
688  return *this;
689 }
690 
691 
692 
697 
698 void KVNameValueList::WriteClass(const Char_t* classname, const Char_t* classdesc, const Char_t* base_class)
699 {
700  // Generate a class with member variables and Get/Set methods corresponding
701  // to the names and types of the parameters in the list
702  // For booleans we use Isxxxx/SetIsxxx
703 
704  KVClassFactory cf(classname, classdesc, base_class);
705  cf.AddGetSetMethods(*this);
706  cf.GenerateCode();
707 }
708 
709 
710 
716 
717 void KVNameValueList::SetFromEnv(TEnv* tenv, const TString& prefix)
718 {
719  // Update the values of any parameters in the KVNameValueList which are found
720  // in the TEnv, optionally using the given prefix.
721  // Example: if KVNameValueList contains a parameter "Legs" and if prefix="Large",
722  // then if the TEnv contains a value "Large.Legs", it will be used to update "Legs"
723 
724  for (int i = 0; i < GetNpar(); ++i) GetParameter(i)->Set(tenv, prefix);
725 }
726 
727 
728 
729 
733 
734 void KVNameValueList::WriteToEnv(TEnv* tenv, const TString& prefix)
735 {
736  // Write the values of all parameters in the KVNameValueList in the TEnv,
737  // optionally using the given prefix.
738 
739  for (int i = 0; i < GetNpar(); ++i) GetParameter(i)->WriteToEnv(tenv, prefix);
740 }
741 
742 
743 
748 
750 {
751  // Merge other list into this one.
752  // Any parameters in 'other' which do not exist in this one are added.
753  // Any parameters which exist in both have their values summed.
754 
755  for (int i = 0; i < other.GetNpar(); ++i) {
756  KVNamedParameter* np_other = other.GetParameter(i);
757  AddValue(*np_other);
758  }
759 }
760 
761 
762 
763 
764 
765 
766 
767 
768 
769 
770 
771 
int Int_t
unsigned int UInt_t
ClassImp(KVPartitionList) void KVPartitionList
Initialisation.
char Char_t
const Bool_t kFALSE
bool Bool_t
const Bool_t kTRUE
const char Option_t
kEnvAll
float type_of_call hi(const int &, const int &)
Factory class for generating skeleton files for new classes.
void GenerateCode()
Generate header and implementation file for currently-defined class.
void AddGetSetMethods(const KVNameValueList &)
For each named parameter in the list, we add protected member variables with the name and type of the...
Extension of TEnv to allow the writing of comments in the file.
Definition: KVEnv.h:16
Extended version of ROOT THashList.
Definition: KVHashList.h:28
Handles lists of named parameters with different types, a list of KVNamedParameter objects.
virtual void Print(Option_t *opt="") const
KVNamedParameter * GetParameter(Int_t idx) const
return the parameter object with index idx
virtual void ls(Option_t *opt="") const
Int_t GetIntValue(const Char_t *name) const
Bool_t HasValue64bit(const Char_t *name) const
void SetValue(const Char_t *name, value_type value)
virtual ~KVNameValueList()
Destructor.
Bool_t fIgnoreBool
do not convert "yes", "false", "on", etc. in TEnv file to boolean
Bool_t IsOwner() const
void RemoveParameter(const Char_t *name)
Int_t Compare(const TObject *nvl) const
void SetValue64bit(const Char_t *name, ULong64_t)
void SetFromEnv(TEnv *tenv, const TString &prefix="")
virtual void Clear(Option_t *opt="")
const Char_t * GetNameAt(Int_t idx) const
Int_t GetNpar() const
return the number of stored parameters
virtual void ReadEnvFile(const Char_t *filename)
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 &)
KVNameValueList()
Default constructor.
KVNamedParameter * FindParameter(const Char_t *name) const
return the parameter object with the asking name
KVNameValueList operator+=(const KVNameValueList &nvl)
KVNameValueList & operator=(const KVNameValueList &)
KVHashList fList
list of KVNamedParameter objects
Bool_t HasIntParameter(const Char_t *name) const
void SetOwner(Bool_t enable=kTRUE)
virtual KVEnv * ProduceEnvFile()
void WriteClass(const Char_t *classname, const Char_t *classdesc, const Char_t *base_class="")
ULong64_t GetValue64bit(const Char_t *name) const
bool Set(const KVString &)
Bool_t HasParameter(const Char_t *name) const
void AddValue(const KVNamedParameter &p)
KVHashList * GetList() const
TString GetTStringValue(const Char_t *name) const
void Copy(TObject &nvl) const
virtual void ClearSelection(TRegexp &)
void WriteToEnv(TEnv *tenv, const TString &prefix="")
A generic named parameter storing values of different types.
const Char_t * GetString() const
void Set(const char *, const char *)
Int_t GetInt() const
void Add(const KVNamedParameter &p)
Double_t GetDouble() const
virtual void ls(Option_t *opt="") const
void WriteToEnv(TEnv *, const TString &p="")
Write parameter in TEnv, using optional prefix p as "p.[name]".
TString GetTString() const
Bool_t GetBool() const
virtual void Copy(TObject &obj) const
virtual void SetOwner(Bool_t enable=kTRUE)
virtual void Clear(Option_t *option="")
virtual TObject * At(Int_t idx) const
virtual void Add(TObject *obj)
virtual TObject * Remove(TObject *obj)
Remove object from list.
virtual TObject * FindObject(const char *name) const
Extension of ROOT TString class which allows backwards compatibility with ROOT v3....
Definition: KVString.h:72
void Begin(TString delim) const
Definition: KVString.cpp:562
Bool_t End() const
Definition: KVString.cpp:625
KVString Next(Bool_t strip_whitespace=kFALSE) const
Definition: KVString.cpp:675
virtual Int_t GetEntries() const
Bool_t IsOwner() const
const char * GetName() const
const char * GetValue() const
virtual const char * GetValue(const char *name, const char *dflt) const
virtual void SetRcName(const char *name)
virtual Int_t ReadFile(const char *fname, EEnvLevel level)
virtual void SetValue(const char *name, const char *value, EEnvLevel level=kEnvChange, const char *type=nullptr)
virtual void Save()
THashList * GetTable() const
virtual void Add(TObject *obj)
virtual void Delete(Option_t *option="")
virtual const char * GetName() const
virtual void Copy(TObject &named) const
virtual void SetTitle(const char *title="")
virtual const char * GetTitle() const
virtual void SetName(const char *name)
virtual const char * GetName() const
virtual void Error(const char *method, const char *msgfmt,...) const
static Int_t IncreaseDirLevel()
static void IndentLevel()
static Int_t DecreaseDirLevel()
Int_t Atoi() const
Double_t Atof() const
Bool_t IsFloat() const
Bool_t IsDigit() const
void ToUpper()
const char * Data() const
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
unsigned long long ULong64_t
Double_t x[n]
Type GetType(const std::string &Name)
auto * l