Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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 <map>
16#include <string>
17
18#include "TMVA/MsgLogger.h"
19
20#include "TObjString.h"
21
22#include "TObjArray.h"
23
24
25namespace TMVA {
26
27 /**
28 * \class TMVA::OptionMap
29 * \ingroup TMVA
30 * class to storage options for the differents methods
31 */
32
34 {
35 protected:
37 std::map<TString,TString> fOptMap; //
39 class Binding
40 {
41 private:
42 std::map<TString,TString> &fInternalMap;
44 public:
45 Binding(std::map<TString,TString> &fmap,TString key):fInternalMap(fmap),fInternalKey(key){}
47 {
49 }
51 void SetKey(TString key){fInternalKey=key;}
54 {
57 return *this;
58 }
59
60 template<class T> Binding& operator=(const T &value)
61 {
62 ParseValue(fInternalMap[fInternalKey],*const_cast<T*>(&value));
63 return *this;
64 }
65
66 template<class T> operator T()
67 {
68 return GetValue<T>();
69 }
70 template<class T> T GetValue()
71 {
72 T result;
74 return result;
75 }
76
77 template<class T> void ParseValue(TString &str,T &value,Bool_t input=kTRUE)
78 {
79 std::stringstream fStringStream;
80 if(input)
81 {
82 fStringStream<<value;
83 str=fStringStream.str();
84 }else{
85 fStringStream<<str.Data();
86 fStringStream>>value;
87 }
88
89 }
90
91
92 };
94 public:
95 OptionMap(const TString options="",const TString name="Option"):fName(name),fLogger(name.Data()),fBinder(fOptMap,""){
96 ParseOption(options);
97 }
98
99 OptionMap(const Char_t *options,const TString name="Option"):fName(name),fLogger(name.Data()),fBinder(fOptMap,""){
100 ParseOption(options);
101 }
102
103 virtual ~OptionMap(){}
104
105 Bool_t IsEmpty(){return fOptMap.empty();}
106
108 {
109 return fOptMap.count( key )==1;
110 }
111
113 {
114 fBinder.SetKey(key);
115 return fBinder;
116 }
117
119 {
120 ParseOption(options);
121 return *this;
122 }
123
124 void Print() const
125 {
126 MsgLogger Log(fLogger);
127 for(auto &item:fOptMap)
128 {
129 Log<<kINFO<<item.first.Data()<<": "<<item.second.Data()<<Endl;
130 }
131 }
132
133 template<class T> T GetValue(const TString & key)
134 {
135 T result;
136 fBinder.ParseValue(fOptMap[key],result,kFALSE);
137 return result;
138 }
139
140
141 template<class T> T GetValue(const TString & key) const
142 {
143 T result;
144 std::stringstream oss;
145 oss<<fOptMap.at(key);
146 oss>>result;
147 return result;
148 }
149 void ParseOption(TString options)
150 {
151 options.ReplaceAll(" ","");
152 auto opts=options.Tokenize(":");
153 for(auto opt:*opts)
154 {
155 TObjString *objstr=(TObjString*)opt;
156
157 if(objstr->GetString().Contains("="))
158 {
159 auto pair=objstr->String().Tokenize("=");
160 TObjString *key = (TObjString *)pair->At(0);
161 TObjString *value = (TObjString *)pair->At(1);
162
163 fOptMap[key->GetString()] = value->GetString();
164 }else{
165 if(objstr->GetString().BeginsWith("!"))
166 {
167 objstr->String().ReplaceAll("!","");
168 fOptMap[objstr->GetString()]=TString("0");
169 }else{
170 fOptMap[objstr->GetString()]=TString("1");
171 }
172 }
173 }
174
175 }
177 };
178
179}
180
181#endif
char Char_t
Definition RtypesCore.h:37
const Bool_t kFALSE
Definition RtypesCore.h:92
const Bool_t kTRUE
Definition RtypesCore.h:91
#define ClassDef(name, id)
Definition Rtypes.h:325
char name[80]
Definition TGX11.cxx:110
ostringstream derivative to redirect and format output
Definition MsgLogger.h:59
Binding(const Binding &obj)
Definition OptionMap.h:46
std::map< TString, TString > & fInternalMap
Definition OptionMap.h:42
Binding(std::map< TString, TString > &fmap, TString key)
Definition OptionMap.h:45
void SetKey(TString key)
Definition OptionMap.h:51
Binding & operator=(const T &value)
Definition OptionMap.h:60
void ParseValue(TString &str, T &value, Bool_t input=kTRUE)
Definition OptionMap.h:77
Binding & operator=(const Binding &obj)
Definition OptionMap.h:53
class to storage options for the differents methods
Definition OptionMap.h:34
OptionMap(const Char_t *options, const TString name="Option")
Definition OptionMap.h:99
T GetValue(const TString &key) const
Definition OptionMap.h:141
OptionMap(const TString options="", const TString name="Option")
Definition OptionMap.h:95
Bool_t IsEmpty()
Definition OptionMap.h:105
Binding fBinder
Definition OptionMap.h:93
std::map< TString, TString > fOptMap
Definition OptionMap.h:37
virtual ~OptionMap()
Definition OptionMap.h:103
OptionMap & operator=(TString options)
Definition OptionMap.h:118
Binding & operator[](TString key)
Definition OptionMap.h:112
T GetValue(const TString &key)
Definition OptionMap.h:133
Bool_t HasKey(TString key)
Definition OptionMap.h:107
TMVA::MsgLogger fLogger
Definition OptionMap.h:38
TString fName
Definition OptionMap.h:36
void ParseOption(TString options)
Definition OptionMap.h:149
void Print() const
Definition OptionMap.h:124
Collectable string class.
Definition TObjString.h:28
const TString & GetString() const
Definition TObjString.h:46
TString & String()
Definition TObjString.h:48
Basic string class.
Definition TString.h:136
const char * Data() const
Definition TString.h:369
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition TString.h:692
TObjArray * Tokenize(const TString &delim) const
This function is used to isolate sequential tokens in a TString.
Definition TString.cxx:2217
Bool_t BeginsWith(const char *s, ECaseCompare cmp=kExact) const
Definition TString.h:615
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition TString.h:624
create variable transformations
MsgLogger & Endl(MsgLogger &ml)
Definition MsgLogger.h:158