Logo ROOT   6.10/09
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  }
109 // OptionMap(const Char_t *options,const TString name="Option"):fName(name),fLogger(name.Data()),fBinder(fOptMap,"")
110 // {
111 // ParseOption(options);
112 // }
113 
114  virtual ~OptionMap(){}
115 
116  Bool_t IsEmpty(){return fOptMap.empty();}
117 
119  {
120  return fOptMap.count( key )==1;
121  }
122 
124  {
125  fBinder.SetKey(key);
126  return fBinder;
127  }
128 
130  {
131  ParseOption(options);
132  return *this;
133  }
134 
135  void Print() const
136  {
137  MsgLogger Log(fLogger);
138  for(auto &item:fOptMap)
139  {
140  Log<<kINFO<<item.first.Data()<<": "<<item.second.Data()<<Endl;
141  }
142  }
143 
144  template<class T> T GetValue(const TString & key)
145  {
146  T result;
147  fBinder.ParseValue(fOptMap[key],result,kFALSE);
148  return result;
149  }
150 
151 
152  template<class T> T GetValue(const TString & key) const
153  {
154  T result;
155  std::stringstream oss;
156  oss<<fOptMap.at(key);
157  oss>>result;
158  return result;
159  }
160  void ParseOption(TString options)
161  {
162  options.ReplaceAll(" ","");
163  auto opts=options.Tokenize(":");
164  for(auto opt:*opts)
165  {
166  TObjString *objstr=(TObjString*)opt;
167 
168  if(objstr->GetString().Contains("="))
169  {
170  auto pair=objstr->String().Tokenize("=");
171  TObjString *key = (TObjString *)pair->At(0);
172  TObjString *value = (TObjString *)pair->At(1);
173 
174  fOptMap[key->GetString()] = value->GetString();
175  }else{
176  if(objstr->GetString().BeginsWith("!"))
177  {
178  objstr->String().ReplaceAll("!","");
179  fOptMap[objstr->GetString()]=TString("0");
180  }else{
181  fOptMap[objstr->GetString()]=TString("1");
182  }
183  }
184  }
185 
186  }
187  ClassDef(OptionMap,1);
188  };
189 
190 }
191 
192 #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:649
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:144
double T(double x)
Definition: ChebyshevPol.h:34
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition: TString.h:640
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:129
bool Bool_t
Definition: RtypesCore.h:59
TMVA::MsgLogger fLogger
Definition: OptionMap.h:40
Binding & operator[](TString key)
Definition: OptionMap.h:123
#define ClassDef(name, id)
Definition: Rtypes.h:297
std::vector< std::vector< double > > Data
TString fName
Definition: OptionMap.h:38
Binding fBinder
Definition: OptionMap.h:95
Bool_t IsEmpty()
Definition: OptionMap.h:116
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:135
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:563
void ParseOption(TString options)
Definition: OptionMap.h:160
Bool_t HasKey(TString key)
Definition: OptionMap.h:118
TString & String()
Definition: TObjString.h:49
const Bool_t kFALSE
Definition: RtypesCore.h:92
OptionMap & operator=(TString options)
Definition: OptionMap.h:129
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:572
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:152
virtual ~OptionMap()
Definition: OptionMap.h:114
double result[121]
const Bool_t kTRUE
Definition: RtypesCore.h:91
OptionMap(const OptionMap &obj)
Definition: OptionMap.h:104
const char * Data() const
Definition: TString.h:347