HOPS
HOPS class reference
MHO_ParameterStore.hh
Go to the documentation of this file.
1 #ifndef MHO_ParameterStore_HH__
2 #define MHO_ParameterStore_HH__
3 
4 #include <string>
5 
7 #include "MHO_Tokenizer.hh"
8 
9 namespace hops
10 {
11 
52 {
53  public:
55  {
56  fTokenizer.SetDelimiter("/");
58  fTokenizer.SetIncludeEmptyTokensFalse();
59  };
60 
62 
66  void Dump() { std::cout << fStore.dump(2) << std::endl; }
67 
73  void CopyFrom(const MHO_ParameterStore& copy) { fStore = copy.fStore; }
74 
80  void DumpData(mho_json& data) { data = fStore; }
81 
87  void FillData(const mho_json& data) { fStore = data; }
88 
92  void ClearData() { fStore.clear(); }
93 
94  //returns true if no error adding value
102  template< typename XValueType > bool Set(const std::string& value_path, const XValueType& value);
103 
109  bool Set(const std::string& value_path, const mho_json& value)
110  {
111  try
112  {
113  fStore[mho_json::json_pointer(SanitizePath(value_path))] = value;
114  return true;
115  }
116  catch(...)
117  {
118  return false;
119  }
120  }
121 
122  //returns true if found
131  template< typename XValueType > bool Get(const std::string& value_path, XValueType& value) const;
132 
133  //always returns a value, if not found the value returned is XValueType()
148  template< typename XValueType > XValueType GetAs(const std::string& value_path) const;
149 
150  bool IsPresent(const std::string& value_path) const
151  {
152  std::string path = SanitizePath(value_path);
153  fPath.clear();
154  fTokenizer.SetString(&path);
155  fTokenizer.GetTokens(&fPath);
156 
157  auto* p = &fStore;
158  for(auto it = fPath.begin(); it != fPath.end(); it++)
159  {
160  auto jit = p->find(*it);
161  if(jit == p->end())
162  {
163  return false;
164  }
165  else if(*it == *(fPath.rbegin()))
166  {
167  return true;
168  }
169  else
170  {
171  p = &(jit.value());
172  }
173  }
174  return false;
175  }
176 
177  private:
178  //sanitize the value_path string -- for example a trailing '/' is no good
185  std::string SanitizePath(const std::string& value_path) const
186  {
187  std::string vpath = MHO_Tokenizer::TrimLeadingAndTrailingWhitespace(value_path);
188  if(vpath.size() > 0 && vpath.back() == '/') //trim any trailing '/'
189  {
190  vpath.pop_back();
191  }
192  return vpath;
193  }
194 
195  //helpers
196  mutable MHO_Tokenizer fTokenizer;
197  mutable std::vector< std::string > fPath;
198 
199  //stash data in a json object
200  mho_json fStore;
201 };
202 
210 template< typename XValueType > bool MHO_ParameterStore::Set(const std::string& value_path, const XValueType& value)
211 {
212  //first we tokenize the value path into a sequence of names
213  std::string path = SanitizePath(value_path);
214  fPath.clear();
215  fTokenizer.SetString(&path);
216  fTokenizer.GetTokens(&fPath);
217 
218  bool ok = false;
219  if(fPath.size() > 0)
220  {
221  //insert and/or replace an object
222  auto* p = &fStore;
223  for(auto it = fPath.begin(); it != fPath.end(); it++)
224  {
225  if(*it != *(fPath.rbegin()))
226  {
227  if(p->contains(*it)) //item with path exists
228  {
229  p = &(p->at(*it)); //point to this item
230  }
231  else
232  {
233  //item doesn't exist yet, so create an entry with this key (*it)
234  p = &(*p)[*it];
235  }
236  }
237  else //we've arrived at the terminal point, so set this item to the value
238  {
239  p = &(*p)[*it];
240  *p = value;
241  ok = true;
242  }
243  }
244  }
245  return ok;
246 }
247 
255 template< typename XValueType > bool MHO_ParameterStore::Get(const std::string& value_path, XValueType& value) const
256 {
257  //NOTE: we do not use json_pointer to access values specified by path
258  //because it will throw an exception if used when the path is not present/complete
259  std::string path = SanitizePath(value_path);
260  fPath.clear();
261  fTokenizer.SetString(&path);
262  fTokenizer.GetTokens(&fPath);
263 
264  bool present = false;
265  auto* p = &fStore;
266  for(auto it = fPath.begin(); it != fPath.end(); it++)
267  {
268  auto jit = p->find(*it);
269  if(jit == p->end())
270  {
271  return false;
272  }
273  else if(*it == *(fPath.rbegin()))
274  {
275  value = jit->get< XValueType >();
276  return true;
277  }
278  else
279  {
280  p = &(jit.value());
281  }
282  }
283  return false;
284 }
285 
292 template< typename XValueType > XValueType MHO_ParameterStore::GetAs(const std::string& value_path) const
293 {
294  XValueType v = XValueType(); //default constructor (zero for int, double, etc)
295  bool ok = Get(value_path, v);
296  if(!ok)
297  {
298  msg_error("utility", "failed to retrieve value: " << value_path << " returning a default value." << eom);
299  }
300  return v;
301 }
302 
303 } // namespace hops
304 
305 #endif
nlohmann::json mho_json
Definition: MHO_JSONHeaderWrapper.hh:5
#define msg_error(xKEY, xCONTENT)
Definition: MHO_Message.hh:238
Class MHO_ParameterStore.
Definition: MHO_ParameterStore.hh:52
~MHO_ParameterStore()
Definition: MHO_ParameterStore.hh:61
bool Set(const std::string &value_path, const XValueType &value)
Setter for value at specified path in the parameter store.
Definition: MHO_ParameterStore.hh:210
void Dump()
Dumps the store JSON data to std:cout (for debugging)
Definition: MHO_ParameterStore.hh:66
void ClearData()
Clears all data from fStore.
Definition: MHO_ParameterStore.hh:92
void FillData(const mho_json &data)
Stores input json data for later use.
Definition: MHO_ParameterStore.hh:87
MHO_ParameterStore()
Definition: MHO_ParameterStore.hh:54
void DumpData(mho_json &data)
Copies data from internal storage to provided json object.
Definition: MHO_ParameterStore.hh:80
XValueType GetAs(const std::string &value_path) const
Function IsPresent.
Definition: MHO_ParameterStore.hh:292
bool Get(const std::string &value_path, XValueType &value) const
Retrieves a value by path and returns it as XValueType, using default constructor if not found.
Definition: MHO_ParameterStore.hh:255
bool IsPresent(const std::string &value_path) const
Definition: MHO_ParameterStore.hh:150
void CopyFrom(const MHO_ParameterStore &copy)
Copies the parameter store from a given source.
Definition: MHO_ParameterStore.hh:73
bool Set(const std::string &value_path, const mho_json &value)
Explicit overload for mho_json values. Uses nlohmann::json_pointer to avoid template deduction ambigu...
Definition: MHO_ParameterStore.hh:109
void SetIncludeEmptyTokensFalse()
Definition: MHO_Tokenizer.cc:30
void SetString(const std::string *aString)
Definition: MHO_Tokenizer.cc:65
void GetTokens(std::vector< std::string > *tokens)
Definition: MHO_Tokenizer.cc:75
static std::string TrimLeadingAndTrailingWhitespace(const std::string &value)
Definition: MHO_Tokenizer.cc:278
void SetRemoveLeadingTrailingWhitespaceTrue()
Setter for remove leading trailing whitespace true.
Definition: MHO_Tokenizer.cc:55
void SetDelimiter(const std::string &aDelim)
Definition: MHO_Tokenizer.cc:70
Definition: fit_gsl.h:54
Definition: MHO_AdhocFlagging.hh:18
Definition: vex.h:175