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 
104  //returns true if found
113  template< typename XValueType > bool Get(const std::string& value_path, XValueType& value) const;
114 
115  //always returns a value, if not found the value returned is XValueType()
130  template< typename XValueType > XValueType GetAs(const std::string& value_path) const;
131 
132  bool IsPresent(const std::string& value_path) const
133  {
134  std::string path = SanitizePath(value_path);
135  fPath.clear();
136  fTokenizer.SetString(&path);
137  fTokenizer.GetTokens(&fPath);
138 
139  auto* p = &fStore;
140  for(auto it = fPath.begin(); it != fPath.end(); it++)
141  {
142  auto jit = p->find(*it);
143  if(jit == p->end())
144  {
145  return false;
146  }
147  else if(*it == *(fPath.rbegin()))
148  {
149  return true;
150  }
151  else
152  {
153  p = &(jit.value());
154  }
155  }
156  return false;
157  }
158 
159  private:
160 
161  //sanitize the value_path string -- for example a trailing '/' is no good
168  std::string SanitizePath(const std::string& value_path) const
169  {
170  std::string vpath = MHO_Tokenizer::TrimLeadingAndTrailingWhitespace(value_path);
171  if(vpath.size() > 0 && vpath.back() == '/') //trim any trailing '/'
172  {
173  vpath.pop_back();
174  }
175  return vpath;
176  }
177 
178  //helpers
179  mutable MHO_Tokenizer fTokenizer;
180  mutable std::vector< std::string > fPath;
181 
182  //stash data in a json object
183  mho_json fStore;
184 };
185 
193 template< typename XValueType > bool MHO_ParameterStore::Set(const std::string& value_path, const XValueType& value)
194 {
195  //first we tokenize the value path into a sequence of names
196  std::string path = SanitizePath(value_path);
197  fPath.clear();
198  fTokenizer.SetString(&path);
199  fTokenizer.GetTokens(&fPath);
200 
201  bool ok = false;
202  if(fPath.size() > 0)
203  {
204  //insert and/or replace an object
205  auto* p = &fStore;
206  for(auto it = fPath.begin(); it != fPath.end(); it++)
207  {
208  if(*it != *(fPath.rbegin()))
209  {
210  if(p->contains(*it)) //item with path exists
211  {
212  p = &(p->at(*it)); //point to this item
213  }
214  else
215  {
216  //item doesn't exist yet, so create an entry with this key (*it)
217  p = &(*p)[*it];
218  }
219  }
220  else //we've arrived at the terminal point, so set this item to the value
221  {
222  p = &(*p)[*it];
223  *p = value;
224  ok = true;
225  }
226  }
227  }
228  return ok;
229 }
230 
238 template< typename XValueType > bool MHO_ParameterStore::Get(const std::string& value_path, XValueType& value) const
239 {
240  //NOTE: we do not use json_pointer to access values specified by path
241  //because it will throw an exception if used when the path is not present/complete
242  std::string path = SanitizePath(value_path);
243  fPath.clear();
244  fTokenizer.SetString(&path);
245  fTokenizer.GetTokens(&fPath);
246 
247  bool present = false;
248  auto* p = &fStore;
249  for(auto it = fPath.begin(); it != fPath.end(); it++)
250  {
251  auto jit = p->find(*it);
252  if(jit == p->end())
253  {
254  return false;
255  }
256  else if(*it == *(fPath.rbegin()))
257  {
258  value = jit->get< XValueType >();
259  return true;
260  }
261  else
262  {
263  p = &(jit.value());
264  }
265  }
266  return false;
267 }
268 
275 template< typename XValueType > XValueType MHO_ParameterStore::GetAs(const std::string& value_path) const
276 {
277  XValueType v = XValueType(); //default constructor (zero for int, double, etc)
278  bool ok = Get(value_path, v);
279  if(!ok)
280  {
281  msg_error("utility", "failed to retrieve value: " << value_path << " returning a default value." << eom);
282  }
283  return v;
284 }
285 
286 } // namespace hops
287 
288 #endif
nlohmann::json mho_json
Definition: MHO_JSONHeaderWrapper.hh:5
#define msg_error(xKEY, xCONTENT)
Definition: MHO_Message.hh:244
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:193
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:275
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:238
bool IsPresent(const std::string &value_path) const
Definition: MHO_ParameterStore.hh:132
void CopyFrom(const MHO_ParameterStore &copy)
Copies the parameter store from a given source.
Definition: MHO_ParameterStore.hh:73
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: MHO_ChannelLabeler.hh:17
Definition: vex.h:175