Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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:
38  TString fName;
39  std::map<TString,TString> fOptMap; //
40  TMVA::MsgLogger fLogger; //!
41  class Binding
42  {
43  private:
44  std::map<TString,TString> &fInternalMap;
45  TString fInternalKey;
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  }
52  ~Binding(){}
53  void SetKey(TString key){fInternalKey=key;}
54  TString GetKey(){return fInternalKey;}
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  };
95  Binding fBinder; //!
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 
105  virtual ~OptionMap(){}
106 
107  Bool_t IsEmpty(){return fOptMap.empty();}
108 
109  Bool_t HasKey(TString key)
110  {
111  return fOptMap.count( key )==1;
112  }
113 
114  Binding& operator[](TString key)
115  {
116  fBinder.SetKey(key);
117  return fBinder;
118  }
119 
120  OptionMap& operator=(TString options)
121  {
122  ParseOption(options);
123  return *this;
124  }
125 
126  void Print() const
127  {
128  MsgLogger Log(fLogger);
129  for(auto &item:fOptMap)
130  {
131  Log<<kINFO<<item.first.Data()<<": "<<item.second.Data()<<Endl;
132  }
133  }
134 
135  template<class T> T GetValue(const TString & key)
136  {
137  T result;
138  fBinder.ParseValue(fOptMap[key],result,kFALSE);
139  return result;
140  }
141 
142 
143  template<class T> T GetValue(const TString & key) const
144  {
145  T result;
146  std::stringstream oss;
147  oss<<fOptMap.at(key);
148  oss>>result;
149  return result;
150  }
151  void ParseOption(TString options)
152  {
153  options.ReplaceAll(" ","");
154  auto opts=options.Tokenize(":");
155  for(auto opt:*opts)
156  {
157  TObjString *objstr=(TObjString*)opt;
158 
159  if(objstr->GetString().Contains("="))
160  {
161  auto pair=objstr->String().Tokenize("=");
162  TObjString *key = (TObjString *)pair->At(0);
163  TObjString *value = (TObjString *)pair->At(1);
164 
165  fOptMap[key->GetString()] = value->GetString();
166  }else{
167  if(objstr->GetString().BeginsWith("!"))
168  {
169  objstr->String().ReplaceAll("!","");
170  fOptMap[objstr->GetString()]=TString("0");
171  }else{
172  fOptMap[objstr->GetString()]=TString("1");
173  }
174  }
175  }
176 
177  }
178  ClassDef(OptionMap,1);
179  };
180 
181 }
182 
183 #endif