Logo ROOT   6.12/07
Reference Guide
OptionMap.h
Go to the documentation of this file.
1 // @(#)root/tmva:$Id$
2 // Author: Omar Zapata 2016
3 
4 /*************************************************************************
5  * Copyright (C) 2016, Omar Andres Zapata Mesa *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 #ifndef ROOT_TMVA_OptionMap
12 #define ROOT_TMVA_OptionMap
13 
14 #include <sstream>
15 #include<iostream>
16 #include<map>
17 
18 #include <TNamed.h>
19 
20 #include "TMVA/MsgLogger.h"
21 
22 #include "TObjString.h"
23 
24 #include "TObjArray.h"
25 
26 
27 namespace TMVA {
28 
29  /**
30  * \class TMVA::OptionMap
31  * \ingroup TMVA
32  * class to storage options for the differents methods
33  */
34 
35  class OptionMap
36  {
37  protected:
39  std::map<TString,TString> fOptMap; //
41  class Binding
42  {
43  private:
44  std::map<TString,TString> &fInternalMap;
46  public:
47  Binding(std::map<TString,TString> &fmap,TString key):fInternalMap(fmap),fInternalKey(key){}
48  Binding(const Binding &obj):fInternalMap(obj.fInternalMap)
49  {
50  fInternalKey = obj.fInternalKey;
51  }
53  void SetKey(TString key){fInternalKey=key;}
55  Binding &operator=(const Binding &obj)
56  {
57  fInternalMap = obj.fInternalMap;
58  fInternalKey = obj.fInternalKey;
59  return *this;
60  }
61 
62  template<class T> Binding& operator=(const T &value)
63  {
64  ParseValue(fInternalMap[fInternalKey],*const_cast<T*>(&value));
65  return *this;
66  }
67 
68  template<class T> operator T()
69  {
70  return GetValue<T>();
71  }
72  template<class T> T GetValue()
73  {
74  T result;
75  ParseValue(fInternalMap[fInternalKey],result,kFALSE);
76  return result;
77  }
78 
79  template<class T> void ParseValue(TString &str,T &value,Bool_t input=kTRUE)
80  {
81  std::stringstream fStringStream;
82  if(input)
83  {
84  fStringStream<<value;
85  str=fStringStream.str();
86  }else{
87  fStringStream<<str.Data();
88  fStringStream>>value;
89  }
90 
91  }
92 
93 
94  };
96  public:
97  OptionMap(const TString options="",const TString name="Option"):fName(name),fLogger(name.Data()),fBinder(fOptMap,""){
98  ParseOption(options);
99  }
100 
101  OptionMap(const Char_t *options,const TString name="Option"):fName(name),fLogger(name.Data()),fBinder(fOptMap,""){
102  ParseOption(options);
103  }
104  OptionMap(const OptionMap &obj):fBinder(obj.fBinder)
105  {
106  fName = obj.fName;
107  fLogger = obj.fLogger;
108  fOptMap = obj.fOptMap;
109  }
110 // OptionMap(const Char_t *options,const TString name="Option"):fName(name),fLogger(name.Data()),fBinder(fOptMap,"")
111 // {
112 // ParseOption(options);
113 // }
114 
115  virtual ~OptionMap(){}
116 
117  Bool_t IsEmpty(){return fOptMap.empty();}
118 
120  {
121  return fOptMap.count( key )==1;
122  }
123 
125  {
126  fBinder.SetKey(key);
127  return fBinder;
128  }
129 
131  {
132  ParseOption(options);
133  return *this;
134  }
135 
136  void Print() const
137  {
138  MsgLogger Log(fLogger);
139  for(auto &item:fOptMap)
140  {
141  Log<<kINFO<<item.first.Data()<<": "<<item.second.Data()<<Endl;
142  }
143  }
144 
145  template<class T> T GetValue(const TString & key)
146  {
147  T result;
148  fBinder.ParseValue(fOptMap[key],result,kFALSE);
149  return result;
150  }
151 
152 
153  template<class T> T GetValue(const TString & key) const
154  {
155  T result;
156  std::stringstream oss;
157  oss<<fOptMap.at(key);
158  oss>>result;
159  return result;
160  }
161  void ParseOption(TString options)
162  {
163  options.ReplaceAll(" ","");
164  auto opts=options.Tokenize(":");
165  for(auto opt:*opts)
166  {
167  TObjString *objstr=(TObjString*)opt;
168 
169  if(objstr->GetString().Contains("="))
170  {
171  auto pair=objstr->String().Tokenize("=");
172  TObjString *key = (TObjString *)pair->At(0);
173  TObjString *value = (TObjString *)pair->At(1);
174 
175  fOptMap[key->GetString()] = value->GetString();
176  }else{
177  if(objstr->GetString().BeginsWith("!"))
178  {
179  objstr->String().ReplaceAll("!","");
180  fOptMap[objstr->GetString()]=TString("0");
181  }else{
182  fOptMap[objstr->GetString()]=TString("1");
183  }
184  }
185  }
186 
187  }
188  ClassDef(OptionMap,1);
189  };
190 
191 }
192 
193 #endif
std::map< TString, TString > & fInternalMap
Definition: OptionMap.h:44
MsgLogger & Endl(MsgLogger &ml)
Definition: MsgLogger.h:158
Double_t Log(Double_t x)
Definition: TMath.h:648
Collectable string class.
Definition: TObjString.h:28
void SetKey(TString key)
Definition: OptionMap.h:53
Binding & operator=(const Binding &obj)
Definition: OptionMap.h:55
OptionMap(const TString options="", const TString name="Option")
Definition: OptionMap.h:97
T GetValue(const TString &key)
Definition: OptionMap.h:145
double T(double x)
Definition: ChebyshevPol.h:34
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition: TString.h:638
class to storage options for the differents methods
Definition: OptionMap.h:35
Binding & operator=(const T &value)
Definition: OptionMap.h:62
OptionMap(const Char_t *options, const TString name="Option")
Definition: OptionMap.h:101
Basic string class.
Definition: TString.h:125
bool Bool_t
Definition: RtypesCore.h:59
TMVA::MsgLogger fLogger
Definition: OptionMap.h:40
Binding & operator[](TString key)
Definition: OptionMap.h:124
#define ClassDef(name, id)
Definition: Rtypes.h:320
TString fName
Definition: OptionMap.h:38
Binding fBinder
Definition: OptionMap.h:95
Bool_t IsEmpty()
Definition: OptionMap.h:117
void ParseValue(TString &str, T &value, Bool_t input=kTRUE)
Definition: OptionMap.h:79
const TString & GetString() const
Definition: TObjString.h:47
std::map< TString, TString > fOptMap
Definition: OptionMap.h:39
void Print() const
Definition: OptionMap.h:136
Binding(std::map< TString, TString > &fmap, TString key)
Definition: OptionMap.h:47
Bool_t BeginsWith(const char *s, ECaseCompare cmp=kExact) const
Definition: TString.h:561
void ParseOption(TString options)
Definition: OptionMap.h:161
Bool_t HasKey(TString key)
Definition: OptionMap.h:119
TString & String()
Definition: TObjString.h:49
const Bool_t kFALSE
Definition: RtypesCore.h:88
OptionMap & operator=(TString options)
Definition: OptionMap.h:130
TObjArray * Tokenize(const TString &delim) const
This function is used to isolate sequential tokens in a TString.
Definition: TString.cxx:2251
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition: TString.h:570
Binding(const Binding &obj)
Definition: OptionMap.h:48
ostringstream derivative to redirect and format output
Definition: MsgLogger.h:59
char Char_t
Definition: RtypesCore.h:29
Abstract ClassifierFactory template that handles arbitrary types.
T GetValue(const TString &key) const
Definition: OptionMap.h:153
virtual ~OptionMap()
Definition: OptionMap.h:115
const Bool_t kTRUE
Definition: RtypesCore.h:87
OptionMap(const OptionMap &obj)
Definition: OptionMap.h:104
char name[80]
Definition: TGX11.cxx:109
const char * Data() const
Definition: TString.h:345