KaliVeda
Toolkit for HIC analysis
KVNameValueList.cpp
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 
130 
132 {
133  // If list contains a comma-separated list of parameter/value pairs
134  //
135  //~~~~~~~~~~~~~~~~~~~
136  //list = "param1=val1,param2=val2,..."
137  //~~~~~~~~~~~~~~~~~~~
138  //
139  // then use it to initialise the parameters of the list, and return true (any existing parameters will be removed).
140  //
141  // If list does not contain at least one '=' character, do nothing and return false.
142  //
143  // ### Note on deduction of parameter types
144  //
145  // The type of each parameter is deduced from the given values, using the following methods
146  // in the following order:
147  //
148  // + string in single quotes => string parameter (e.g. val="'7 9'")
149  // + KVString::IsDigit(val) => integer parameter if true (e.g. val="3" _or_ val="7 9")
150  // + KVString::IsFloat(val) => double parameter if true (e.g. val="3.14e+03")
151  // + all other cases => string parameter
152  //
153  // Note that in the case of the quoted string, the parameter is stored without the
154  // enclosing quotes (i.e. for val="'7 9'" the stored string is "7 9")
155 
156  if (!list.Contains("=")) return false;
157 
158  Clear();
159  list.Begin(",");
160  while (!list.End()) {
161  KVString pair = list.Next(kTRUE);
162  pair.Begin("=");
163  KVString parname = pair.Next(kTRUE);
164  KVString parval = pair.Next(kTRUE);
165  if (parval.BeginsWith("'") && parval.EndsWith("'")) {
166  // quoted string
167  parval.Remove(parval.Length() - 1);
168  parval.Remove(0, 1);
169  SetValue(parname, parval);
170  }
171  else if (parval.IsDigit()) {
172  // integer number
173  SetValue(parname, parval.Atoi());
174  }
175  else if (parval.IsFloat()) {
176  // real number
177  SetValue(parname, parval.Atof());
178  }
179  else {
180  // string
181  SetValue(parname, parval);
182  }
183  }
184  return true;
185 }
186 
187 
188 
197 
199 {
200  // Fill and return a string containing a comma-separated list of the parameter/value pairs
201  //
202  //~~~~~~~~~~~~~~~~~~~
203  //"param1=val1,param2=val2,..."
204  //~~~~~~~~~~~~~~~~~~~
205  //
206  // Such a string can be used with method Set() in order to initialize a copy of this list.
207 
208  KVString list;
209  for (auto& par : *this) {
210  if (list.Length()) list += ",";
211  list += Form("%s=", par.GetName());
212  if (par.IsString())
213  list += par.GetString();
214  else if (par.IsDouble())
215  list += Form("%g", par.GetDouble());
216  else if (par.IsInt())
217  list += Form("%d", par.GetInt());
218  }
219  return list;
220 }
221 
222 
223 
227 
229 {
230  //return the pointeur of the KVHashList where
231  //parameters are stored with their values
232  return (KVHashList*)&fList;
233 }
234 
235 
236 
240 
242 {
243  // Copy this to the nvl object.
244  // Any existing parameters will be destroyed
245 
246  TNamed::Copy(nvl);
247  KVNameValueList& _obj = (KVNameValueList&)nvl;
248  fList.Copy(_obj.fList);
249  _obj.fIgnoreBool = fIgnoreBool;
250 }
251 
252 
253 
257 
259 {
260  //Clear all the stored parameters
261  //Deletes the parameter objects if owner & opt!="nodelete"
262  fList.Clear(opt);
263 }
264 
265 
266 
272 
274 {
275  // Remove from list all parameters whose name matches the regular expression
276  // Examples:
277  // remove all parameters starting with "toto": TRegexp sel("^toto")
278  // remove all parameters with "toto" in name: TRegexp sel("toto")
279 
280  TList toBeRemoved;
281  Int_t np1 = GetNpar();
282  for (Int_t ii = 0; ii < np1; ii += 1) {
283  TString name = GetParameter(ii)->GetName();
284  if (name.Contains(sel)) toBeRemoved.Add(new TNamed(name.Data(), ""));
285  }
286  if (toBeRemoved.GetEntries()) {
287  TIter next(&toBeRemoved);
288  TNamed* tbr;
289  while ((tbr = (TNamed*)next())) RemoveParameter(tbr->GetName());
290  toBeRemoved.Delete();
291  }
292 }
293 
294 
295 
300 
301 void KVNameValueList::Print(Option_t* option) const
302 {
303  // Print stored parameters (name, and value)
304  // Option can be used to select type of parameters to print:
305  // option = "int", "double", or "string"
306 
307  if (!GetNpar()) return;
308 
310  cout << "KVNameValueList::" << GetName() << " : " << GetTitle() << " (" << this << ")" << endl;
312  for (Int_t ii = 0; ii < GetNpar(); ii += 1) {
313  GetParameter(ii)->ls(option);
314  }
316 }
317 
318 
319 
321 
323 {
324  if (TString(GetName()) != "") cout << GetName();
325  else cout << "KVNameValueList";
326  cout << " : ";
327  for (int i = 0; i < GetNpar(); ++i) {
328  cout << GetParameter(i)->GetName() << "=";
329  switch (GetParameter(i)->GetType()) {
331  cout << GetParameter(i)->GetDouble();
332  break;
334  cout << GetParameter(i)->GetInt();
335  break;
337  cout << GetParameter(i)->GetString();
338  break;
340  cout << boolalpha << GetParameter(i)->GetBool();
341  break;
342  }
343  if (i < GetNpar() - 1) cout << ",";
344  }
345  cout << endl;
346 }
347 
348 
349 
353 
355 {
356  //set if the KVNameValueList owns its objects or not
357  //by default it is owner
358  fList.SetOwner(enable);
359 }
360 
361 
362 
366 
368 {
369  //return kTRUE if the list owns its objects
370  //kFALSE if not
371  return fList.IsOwner();
372 }
373 
374 
375 
379 
381 {
382  // Compare the contents of two KVNameValueList
383  // Returns the number of same parameters (name and value)
384 
385  KVNameValueList* nvl = (KVNameValueList*)obj;
386  Int_t neq = 0;
387  Int_t np1 = GetNpar();
388  Int_t np2 = nvl->GetNpar();
389  for (Int_t ii = 0; ii < np1; ii += 1) {
390  for (Int_t jj = 0; jj < np2; jj += 1) {
391 
392  if (*(GetParameter(ii)) == *(GetParameter(jj))) neq += 1;
393  }
394  }
395  return neq;
396 
397 }
398 
399 
400 
403 
405 {
406  // add (or replace) a parameter with the same name, type & value as 'p'
407 
409  par ? par->Set(p.GetName(), p) : fList.Add(new KVNamedParameter(p));
410 
411 }
412 
413 
414 
418 
420 {
421  // Store a 64-bit integer in the list as two 32-bit parameters with
422  // names 'name_up' and 'name_lo'
423 
424  TString parname = name;
425  parname += "_hi";
426  SetValue(parname, (Int_t)(x >> 32));
427  parname = name;
428  parname += "_lo";
429  SetValue(parname, (Int_t)((x << 32) >> 32));
430 }
431 
432 
433 
437 
439 {
440  // Return a 64-bit integer stored as two 32-bit parameters with
441  // names 'name_up' and 'name_lo'
442 
443  ULong64_t lo, hi;
444  TString parname = name;
445  parname += "_lo";
446  lo = (UInt_t)GetIntValue(parname);
447  parname = name;
448  parname += "_hi";
449  hi = (UInt_t)GetIntValue(parname);
450  ULong64_t x = (ULong64_t)((hi << 32) + lo);
451  return x;
452 }
453 
454 
455 
459 
461 {
462  // Returns kTRUE if 'name' is stored as a 64-bit value i.e. if
463  // integer parameters 'name_lo' and 'name_hi' are defined
464 
465  TString parname_hi = name;
466  parname_hi += "_hi";
467  TString parname_lo = name;
468  parname_lo += "_lo";
469  return (HasIntParameter(parname_hi) && HasIntParameter(parname_lo));
470 }
471 
472 
473 
479 
481 {
482  // if a parameter with the same name & type as 'p' exists,
483  // add numerical value of p to value of parameter in list,
484  // or for strings we add to a comma-separated list of strings.
485  // otherwise, add a copy of p to list
487  par ? par->Add(p) : fList.Add(new KVNamedParameter(p));
488 }
489 
490 
491 
494 
496 {
497  //return the parameter object with the asking name
499 }
500 
501 
502 
505 
507 {
508  //return the parameter object with index idx
509  return (KVNamedParameter*)fList.At(idx);
510 }
511 
512 
513 
517 
519 {
520  //remove parameter from the list,
521  //Warning the TNamed object associated is deleted
522 
524  if (par) {
525  fList.Remove(par);
526  delete par;
527  }
528 }
529 
530 
531 
537 
539 {
540  //Check if there is a parameter with the asked name
541  //in the list
542  //kTRUE, parameter already present
543  //kFALSE, if not
544  return (FindParameter(name) != nullptr);
545 }
546 
547 
548 
553 
555 {
556  //return the position in the list of a given parameter
557  //using its name
558  //return -1 if no parameter with such name are present
559 
560  TObject* par = 0;
561  Int_t idx = 0;
562  TIter next(&fList);
563  while ((par = next())) {
564  if (!strcmp(par->GetName(), name)) return idx;
565  idx++;
566  }
567  Error("GetNameIndex", "Parameter \"%s\" not found, -1 returned", name);
568  return -1;
569 }
570 
571 
572 
578 
580 {
581  //return the name of the parameter store at the idx position
582  //in the list
583  //if the idx is greater than the number of stored parameters
584  //return empty string
585 
586  if (idx >= GetNpar()) {
587  Error("GetNameAt", "index has to be less than %d, empty string is returned", GetNpar());
588  return "";
589  }
590  return fList.At(idx)->GetName();
591 }
592 
593 
594 
599 
601 {
602  //return the value in TString format
603  //for a parameter using its name
604  //return string "-1" if no parameter with such name are present
605 
607  if (!par) {
608  Error("GetStringValue(const Char_t*)", "\"%s\" does not correspond to an existing parameter, default value \"-1\" is returned", name);
609  return "-1";
610  }
611  return par->GetTString();
612 }
613 
614 
615 
618 
620 {
621  //return the number of stored parameters
622  return fList.GetEntries();
623 }
624 
625 
626 
631 
633 {
634  //return the value in string format
635  //for a parameter using its position
636  //return -1 idx is greater than the number of stored parameters
637  static TString tmp("-1");
638  if (idx >= GetNpar()) {
639  Error("GetStringValue(Int_t)", "index has to be less than %d, \"-1\" is returned\n", GetNpar());
640  return tmp;
641  }
642  return GetParameter(idx)->GetTString();
643 }
644 
645 
646 
663 
665 {
666  // Read all name-value pairs in the TEnv format file and store in list.
667  // Clears any previously stored values.
668  //
669  // values are read as strings from the TEnv and we use
670  // TString::IsDigit, TString::IsFloat to decide whether to store
671  // them as integers, floats, or strings.
672  // booleans are recognized as: TRUE, FALSE, ON, OFF, YES, NO, OK, NOT
673  // (to disable this feature and read such values as strings, call
674  // SetIgnoreBool(kTRUE))
675  //
676  // Special case:
677  // if the parameter name contains the string NumberList
678  // then we store the value string as is, as in this case
679  // it is assumed to be the string representation of a
680  // KVNumberList (easily confused with floating point numbers)
681 
682  Clear();
683  KVEnv env_file;
684  Int_t status = env_file.ReadFile(filename, kEnvAll);
685  if (status == -1) {
686  Error("ReadEnvFile", "The file %s does not exist", filename);
687  return;
688  }
689  THashList* name_value_list = env_file.GetTable();
690  TIter next_nv(name_value_list);
691  TEnvRec* nv_pair;
692  while ((nv_pair = (TEnvRec*)next_nv())) {
693  TString parname(nv_pair->GetName());
694  if (parname == "KVNameValueList.Name") SetName(nv_pair->GetValue());
695  else if (parname == "KVNameValueList.Title") SetTitle(nv_pair->GetValue());
696  else if (parname.Contains("NumberList")) SetValue(parname, nv_pair->GetValue());
697  else {
698  TString parval(nv_pair->GetValue());
699  if (parval.IsDigit()) SetValue(parname, parval.Atoi());
700  else if (parval.IsFloat()) SetValue(parname, parval.Atof());
701  else {
702  TString PARVAL(parval);
703  PARVAL.ToUpper();
704  if (!fIgnoreBool && (PARVAL == "TRUE" || PARVAL == "FALSE" || PARVAL == "ON" || PARVAL == "OFF"
705  || PARVAL == "YES" || PARVAL == "NO" || PARVAL == "OK" || PARVAL == "NOT"))
706  SetValue(parname, (Bool_t)env_file.GetValue(parname, 0));
707  else SetValue(parname, parval);
708  }
709  }
710  }
711 }
712 
713 
714 
718 
720 {
721  // Put all name-value pairs in this list as a TEnv format.
722  // delete after use
723  KVEnv* envfile = new KVEnv();
724  envfile->SetValue("KVNameValueList.Name", GetName());
725  envfile->SetValue("KVNameValueList.Title", GetTitle());
726  WriteToEnv(envfile);
727  return envfile;
728 }
729 
730 
731 
734 
736 {
737  // Write all name-value pairs in this list as a TEnv format file.
738  KVEnv* envfile = ProduceEnvFile();
739  envfile->SetRcName(filename);
740  envfile->Save();
741  delete envfile;
742 }
743 
744 
745 
746 
751 
753 {
754  // Concatenate this list with nvl.
755  //
756  // \note Any parameters with the same name in nvl will **replace** the corresponding parameters in this one
757 
758  TIter it(nvl.GetList());
759  KVNamedParameter* par = 0;
760  while ((par = (KVNamedParameter*)it())) SetValue(*par);
761 }
762 
763 
764 
769 
770 void KVNameValueList::WriteClass(const Char_t* classname, const Char_t* classdesc, const Char_t* base_class)
771 {
772  // Generate a class with member variables and Get/Set methods corresponding
773  // to the names and types of the parameters in the list
774  // For booleans we use Isxxxx/SetIsxxx
775 
776  KVClassFactory cf(classname, classdesc, base_class);
777  cf.AddGetSetMethods(*this);
778  cf.GenerateCode();
779 }
780 
781 
782 
788 
789 void KVNameValueList::SetFromEnv(TEnv* tenv, const TString& prefix)
790 {
791  // Update the values of any parameters in the KVNameValueList which are found
792  // in the TEnv, optionally using the given prefix.
793  // Example: if KVNameValueList contains a parameter "Legs" and if prefix="Large",
794  // then if the TEnv contains a value "Large.Legs", it will be used to update "Legs"
795 
796  for (int i = 0; i < GetNpar(); ++i) GetParameter(i)->Set(tenv, prefix);
797 }
798 
799 
800 
801 
805 
806 void KVNameValueList::WriteToEnv(TEnv* tenv, const TString& prefix)
807 {
808  // Write the values of all parameters in the KVNameValueList in the TEnv,
809  // optionally using the given prefix.
810 
811  for (int i = 0; i < GetNpar(); ++i) GetParameter(i)->WriteToEnv(tenv, prefix);
812 }
813 
814 
815 
822 
824 {
825  // Merge other list into this one.
826  //
827  // Any parameters in 'other' which do not exist in this one are added to this list.
828  //
829  // Any parameters which exist in both have their values summed (see KVNamedParameter::Add()).
830 
831  for (int i = 0; i < other.GetNpar(); ++i) {
832  KVNamedParameter* np_other = other.GetParameter(i);
833  AddValue(*np_other);
834  }
835 }
836 
837 
838 
839 
840 
841 
842 
843 
844 
845 
846 
847 
int Int_t
unsigned int UInt_t
bool Bool_t
char Char_t
constexpr Bool_t kFALSE
constexpr Bool_t kTRUE
const char Option_t
kEnvAll
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t option
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char filename
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t sel
char name[80]
#define hi
char * Form(const char *fmt,...)
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:17
Extended version of ROOT THashList.
Definition: KVHashList.h:29
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
void Copy(TObject &nvl) const override
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)
void SetValue64bit(const Char_t *name, ULong64_t)
void SetFromEnv(TEnv *tenv, const TString &prefix="")
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
virtual void ReadEnvFile(const Char_t *filename)
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 &)
KVNameValueList()
Default constructor.
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 Clear(Option_t *opt="") override
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
KVString Get() const
bool Set(const KVString &)
Bool_t HasParameter(const Char_t *name) const
void Print(Option_t *opt="") const override
void AddValue(const KVNamedParameter &p)
KVHashList * GetList() const
TString GetTStringValue(const Char_t *name) 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
void WriteToEnv(TEnv *, const TString &p="")
Write parameter in TEnv, using optional prefix p as "p.[name]".
TString GetTString() const
Bool_t GetBool() const
void ls(Option_t *opt="") const override
void Copy(TObject &obj) const override
TObject * Remove(TObject *obj) override
Remove object from list.
void Add(TObject *obj) override
TObject * FindObject(const char *name) const override
void Clear(Option_t *option="") override
void SetOwner(Bool_t enable=kTRUE) override
TObject * At(Int_t idx) const override
Extension of ROOT TString class which allows backwards compatibility with ROOT v3....
Definition: KVString.h:73
void Begin(TString delim) const
Definition: KVString.cpp:565
Bool_t End() const
Definition: KVString.cpp:634
KVString Next(Bool_t strip_whitespace=kFALSE) const
Definition: KVString.cpp:695
virtual Int_t GetEntries() const
Bool_t IsOwner() const
const char * GetValue() const
const char * GetName() const override
THashList * GetTable() 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()
void Add(TObject *obj) override
void Delete(Option_t *option="") override
void Copy(TObject &named) const override
virtual void SetTitle(const char *title="")
const char * GetName() const override
const char * GetTitle() const override
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()
Ssiz_t Length() const
Int_t Atoi() const
Bool_t EndsWith(const char *pat, ECaseCompare cmp=kExact) const
Double_t Atof() const
Bool_t IsFloat() const
Bool_t IsDigit() const
void ToUpper()
Bool_t BeginsWith(const char *s, ECaseCompare cmp=kExact) const
TString & Remove(EStripType s, char c)
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)
TLine l
ClassImp(TPyArg)