Logo ROOT   6.08/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 #ifndef ROOT_TNamed
19 #include<TNamed.h>
20 #endif
21 
22 #ifndef ROOT_TMVA_MsgLogger
23 #include "TMVA/MsgLogger.h"
24 #endif
25 
26 #ifndef ROOT_TObjString
27 #include "TObjString.h"
28 #endif
29 
30 #ifndef ROOT_TObjArray
31 #include "TObjArray.h"
32 #endif
33 
34 
35 namespace TMVA {
36 
37  /**
38  * \class OptionMap
39  * class to storage options for the differents methods
40  * \ingroup TMVA
41  */
42 
43  class OptionMap
44  {
45  protected:
47  std::map<TString,TString> fOptMap; //
49  class Binding
50  {
51  private:
52  std::map<TString,TString> &fInternalMap;
54  public:
55  Binding(std::map<TString,TString> &fmap,TString key):fInternalMap(fmap),fInternalKey(key){}
56  Binding(const Binding &obj):fInternalMap(obj.fInternalMap)
57  {
58  fInternalKey = obj.fInternalKey;
59  }
61  void SetKey(TString key){fInternalKey=key;}
63  Binding &operator=(const Binding &obj)
64  {
65  fInternalMap = obj.fInternalMap;
66  fInternalKey = obj.fInternalKey;
67  return *this;
68  }
69 
70  template<class T> Binding& operator=(const T &value)
71  {
72  ParseValue(fInternalMap[fInternalKey],*const_cast<T*>(&value));
73  return *this;
74  }
75 
76  template<class T> operator T()
77  {
78  return GetValue<T>();
79  }
80  template<class T> T GetValue()
81  {
82  T result;
83  ParseValue(fInternalMap[fInternalKey],result,kFALSE);
84  return result;
85  }
86 
87  template<class T> void ParseValue(TString &str,T &value,Bool_t input=kTRUE)
88  {
89  std::stringstream fStringStream;
90  if(input)
91  {
92  fStringStream<<value;
93  str=fStringStream.str();
94  }else{
95  fStringStream<<str.Data();
96  fStringStream>>value;
97  }
98 
99  }
100 
101 
102  };
104  public:
105  OptionMap(const TString options="",const TString name="Option"):fName(name),fLogger(name.Data()),fBinder(fOptMap,""){
106  ParseOption(options);
107  }
108 
109  OptionMap(const Char_t *options,const TString name="Option"):fName(name),fLogger(name.Data()),fBinder(fOptMap,""){
110  ParseOption(options);
111  }
112  OptionMap(const OptionMap &obj):fBinder(obj.fBinder)
113  {
114  fName = obj.fName;
115  fLogger = obj.fLogger;
116  }
117 // OptionMap(const Char_t *options,const TString name="Option"):fName(name),fLogger(name.Data()),fBinder(fOptMap,"")
118 // {
119 // ParseOption(options);
120 // }
121 
122  virtual ~OptionMap(){}
123 
124  Bool_t IsEmpty(){return fOptMap.empty();}
125 
127  {
128  return fOptMap.count( key )==1;
129  }
130 
132  {
133  fBinder.SetKey(key);
134  return fBinder;
135  }
136 
138  {
139  ParseOption(options);
140  return *this;
141  }
142 
143  void Print() const
144  {
145  MsgLogger Log(fLogger);
146  for(auto &item:fOptMap)
147  {
148  Log<<kINFO<<item.first.Data()<<": "<<item.second.Data()<<Endl;
149  }
150  }
151 
152  template<class T> T GetValue(const TString & key)
153  {
154  T result;
155  fBinder.ParseValue(fOptMap[key],result,kFALSE);
156  return result;
157  }
158 
159 
160  template<class T> T GetValue(const TString & key) const
161  {
162  T result;
163  std::stringstream oss;
164  oss<<fOptMap.at(key);
165  oss>>result;
166  return result;
167  }
168  void ParseOption(TString options)
169  {
170  options.ReplaceAll(" ","");
171  auto opts=options.Tokenize(":");
172  for(auto opt:*opts)
173  {
174  TObjString *objstr=(TObjString*)opt;
175 
176  if(objstr->GetString().Contains("="))
177  {
178  auto pair=objstr->String().Tokenize("=");
179  TObjString *key = (TObjString *)pair->At(0);
180  TObjString *value = (TObjString *)pair->At(1);
181 
182  fOptMap[key->GetString()] = value->GetString();
183  }else{
184  if(objstr->GetString().BeginsWith("!"))
185  {
186  objstr->GetString().ReplaceAll("!","");
187  fOptMap[objstr->GetString()]=TString("0");
188  }else{
189  fOptMap[objstr->GetString()]=TString("1");
190  }
191  }
192  }
193 
194  }
195  ClassDef(OptionMap,1);
196  };
197 
198 }
199 
200 #endif
std::map< TString, TString > & fInternalMap
Definition: OptionMap.h:52
MsgLogger & Endl(MsgLogger &ml)
Definition: MsgLogger.h:162
Double_t Log(Double_t x)
Definition: TMath.h:526
Collectable string class.
Definition: TObjString.h:32
void SetKey(TString key)
Definition: OptionMap.h:61
Binding & operator=(const Binding &obj)
Definition: OptionMap.h:63
OptionMap(const TString options="", const TString name="Option")
Definition: OptionMap.h:105
T GetValue(const TString &key)
Definition: OptionMap.h:152
double T(double x)
Definition: ChebyshevPol.h:34
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition: TString.h:635
class to storage options for the differents methods
Definition: OptionMap.h:43
Binding & operator=(const T &value)
Definition: OptionMap.h:70
OptionMap(const Char_t *options, const TString name="Option")
Definition: OptionMap.h:109
Basic string class.
Definition: TString.h:137
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
TMVA::MsgLogger fLogger
Definition: OptionMap.h:48
Binding & operator[](TString key)
Definition: OptionMap.h:131
#define ClassDef(name, id)
Definition: Rtypes.h:254
std::vector< std::vector< double > > Data
TString fName
Definition: OptionMap.h:46
Binding fBinder
Definition: OptionMap.h:103
Bool_t IsEmpty()
Definition: OptionMap.h:124
void ParseValue(TString &str, T &value, Bool_t input=kTRUE)
Definition: OptionMap.h:87
std::map< TString, TString > fOptMap
Definition: OptionMap.h:47
void Print() const
Definition: OptionMap.h:143
Binding(std::map< TString, TString > &fmap, TString key)
Definition: OptionMap.h:55
Bool_t BeginsWith(const char *s, ECaseCompare cmp=kExact) const
Definition: TString.h:558
void ParseOption(TString options)
Definition: OptionMap.h:168
Bool_t HasKey(TString key)
Definition: OptionMap.h:126
TString & String()
Definition: TObjString.h:52
TString GetString() const
Definition: TObjString.h:50
OptionMap & operator=(TString options)
Definition: OptionMap.h:137
TObjArray * Tokenize(const TString &delim) const
This function is used to isolate sequential tokens in a TString.
Definition: TString.cxx:2241
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition: TString.h:567
Binding(const Binding &obj)
Definition: OptionMap.h:56
char Char_t
Definition: RtypesCore.h:29
Abstract ClassifierFactory template that handles arbitrary types.
T GetValue(const TString &key) const
Definition: OptionMap.h:160
virtual ~OptionMap()
Definition: OptionMap.h:122
double result[121]
const Bool_t kTRUE
Definition: Rtypes.h:91
OptionMap(const OptionMap &obj)
Definition: OptionMap.h:112
char name[80]
Definition: TGX11.cxx:109
const char * Data() const
Definition: TString.h:349