HOPS
HOPS class reference
MHO_IndexLabelInterface.hh
Go to the documentation of this file.
1 #ifndef MHO_IndexLabelInterface_HH__
2 #define MHO_IndexLabelInterface_HH__
3 
5 
6 namespace hops
7 {
8 
22 {
23 
24  protected:
25  MHO_IndexLabelInterface(): fIndexLabelObjectPtr(nullptr)
26  {
27  fDummy["index"] = -1; //dummy object for invalid returns, always has an invalid index
28  };
29 
30  MHO_IndexLabelInterface(const MHO_IndexLabelInterface& copy) { fIndexLabelObjectPtr = copy.fIndexLabelObjectPtr; };
31 
37  void SetIndexLabelObject(mho_json* obj) { fIndexLabelObjectPtr = obj; }
38 
39  public:
41 
47  std::size_t GetIndexLabelSize() const { return fIndexLabelObjectPtr->size(); }
48 
53  {
54  //(*fIndexLabelObjectPtr) = mho_json();
55  fIndexLabelObjectPtr->clear();
56  }
57 
66  template< typename XValueType >
67  void InsertIndexLabelKeyValue(std::size_t index, const std::string& key, const XValueType& value)
68  {
69  if(fIndexLabelObjectPtr != nullptr)
70  {
71  std::string ikey = index2key(index);
72  if(!(fIndexLabelObjectPtr->contains(ikey)))
73  {
74  //no such object, so insert one, make sure it gets an 'index' value
75  (*fIndexLabelObjectPtr).emplace(ikey, fDummy);
76  (*fIndexLabelObjectPtr)[ikey]["index"] = index;
77  }
78  //now update
79  mho_json obj;
80  obj[key] = value;
81  (*fIndexLabelObjectPtr)[ikey].update(obj);
82  }
83  else
84  {
85  msg_error("utilities", "cannot insert key:value pair, index label interface is missing object!" << eom);
86  }
87  }
88 
98  template< typename XValueType >
99  bool RetrieveIndexLabelKeyValue(std::size_t index, const std::string& key, XValueType& value) const
100  {
101  if(fIndexLabelObjectPtr != nullptr)
102  {
103  std::string ikey = index2key(index);
104  if((*fIndexLabelObjectPtr)[ikey].contains(key))
105  {
106  value = (*fIndexLabelObjectPtr)[ikey][key].get< XValueType >();
107  return true;
108  }
109  }
110  else
111  {
112  msg_error("utilities", "cannot retrieve key:value pair, index label interface is missing object!" << eom);
113  }
114  return false;
115  }
116 
123  void SetLabelObject(mho_json& obj, std::size_t index)
124  {
125  if(fIndexLabelObjectPtr != nullptr)
126  {
127  if(obj.is_null())
128  {
129  return;
130  }
131  if(!(fIndexLabelObjectPtr->is_object()))
132  {
133  (*fIndexLabelObjectPtr) = mho_json();
134  } //something blew away our object, reset
135  std::string ikey = index2key(index);
136  if(!(fIndexLabelObjectPtr->contains(ikey)))
137  {
138  //no such object, so insert one, make sure it gets an 'index' value
139  // (*fIndexLabelObjectPtr).emplace(ikey, fDummy);
140  (*fIndexLabelObjectPtr)[ikey] = fDummy;
141  (*fIndexLabelObjectPtr)[ikey]["index"] = index;
142  }
143 
144  //make sure the object also contains the index value:
145  obj["index"] = index;
146  (*fIndexLabelObjectPtr)[ikey].update(obj);
147  }
148  else
149  {
150  msg_error("utilities", "cannot insert label object, index label interface is missing object!" << eom);
151  }
152  }
153 
160  mho_json& GetLabelObject(std::size_t index)
161  {
162  if(fIndexLabelObjectPtr != nullptr)
163  {
164  std::string ikey = index2key(index);
165  return (*fIndexLabelObjectPtr)[ikey];
166  }
167  else
168  {
169  msg_error("utilities", "cannot retrieve label object, index label interface is missing object!" << eom);
170  return fDummy;
171  }
172  }
173 
180  mho_json GetLabelObject(std::size_t index) const
181  {
182  if(fIndexLabelObjectPtr != nullptr)
183  {
184  std::string ikey = index2key(index);
185  return (*fIndexLabelObjectPtr)[ikey];
186  }
187  else
188  {
189  msg_error("utilities", "cannot retrieve label object, index label interface is missing object!" << eom);
190  return fDummy;
191  }
192  }
193 
200  std::vector< std::size_t > GetMatchingIndexes(std::string& key) const
201  {
202  std::vector< std::size_t > idx;
203  if(fIndexLabelObjectPtr != nullptr)
204  {
205  for(std::size_t i = 0; i < fIndexLabelObjectPtr->size(); i++)
206  {
207  std::string ikey = index2key(i);
208  if((*fIndexLabelObjectPtr)[ikey].contains(key))
209  {
210  idx.push_back(i);
211  }
212  }
213  }
214  else
215  {
216  msg_error("utilities", "cannot determine matching indexes, index label interface is missing object!" << eom);
217  }
218  return idx;
219  }
220 
229  template< typename XValueType >
230  std::vector< std::size_t > GetMatchingIndexes(std::string& key, const XValueType& value) const
231  {
232  std::vector< std::size_t > idx;
233  if(fIndexLabelObjectPtr != nullptr)
234  {
235  for(std::size_t i = 0; i < fIndexLabelObjectPtr->size(); i++)
236  {
237  std::string ikey = index2key(i);
238  if((*fIndexLabelObjectPtr)[ikey].contains(key))
239  {
240  XValueType v = (*fIndexLabelObjectPtr)[ikey][key].get< XValueType >();
241  if(v == value)
242  {
243  idx.push_back(i);
244  }
245  }
246  }
247  }
248  else
249  {
250  msg_error("utilities", "cannot determine matching indexes, index label interface is missing object!" << eom);
251  }
252  return idx;
253  }
254 
255  private:
263  static std::string index2key(std::size_t idx) { return std::to_string(idx); }
264 
265  mho_json* fIndexLabelObjectPtr; //array of mho_json objects holding key:value pairs
266  mho_json fDummy;
267 };
268 
269 } // namespace hops
270 
271 #endif
nlohmann::json mho_json
Definition: MHO_JSONHeaderWrapper.hh:5
#define msg_error(xKEY, xCONTENT)
Definition: MHO_Message.hh:238
Class MHO_IndexLabelInterface - adds indexes associated with key:value pairs (used by MHO_Axis) const...
Definition: MHO_IndexLabelInterface.hh:22
std::size_t GetIndexLabelSize() const
Getter for index label size.
Definition: MHO_IndexLabelInterface.hh:47
std::vector< std::size_t > GetMatchingIndexes(std::string &key, const XValueType &value) const
Get a vector of indexes which contain a key with a value which matches the passed value.
Definition: MHO_IndexLabelInterface.hh:230
bool RetrieveIndexLabelKeyValue(std::size_t index, const std::string &key, XValueType &value) const
Retrieves value associated with given key and index from IndexLabelObjectPtr if it exists.
Definition: MHO_IndexLabelInterface.hh:99
mho_json & GetLabelObject(std::size_t index)
get a reference to the dictionary object associated with this index
Definition: MHO_IndexLabelInterface.hh:160
virtual ~MHO_IndexLabelInterface()
Definition: MHO_IndexLabelInterface.hh:40
void SetLabelObject(mho_json &obj, std::size_t index)
get a reference to the dictionary object associated with this index
Definition: MHO_IndexLabelInterface.hh:123
void SetIndexLabelObject(mho_json *obj)
Setter for index label object.
Definition: MHO_IndexLabelInterface.hh:37
void InsertIndexLabelKeyValue(std::size_t index, const std::string &key, const XValueType &value)
Function InsertIndexLabelKeyValue.
Definition: MHO_IndexLabelInterface.hh:67
std::vector< std::size_t > GetMatchingIndexes(std::string &key) const
get a vector of indexes which contain a key with the same name
Definition: MHO_IndexLabelInterface.hh:200
MHO_IndexLabelInterface(const MHO_IndexLabelInterface &copy)
Definition: MHO_IndexLabelInterface.hh:30
mho_json GetLabelObject(std::size_t index) const
Getter for label object.
Definition: MHO_IndexLabelInterface.hh:180
void ClearIndexLabels()
Clears all index labels.
Definition: MHO_IndexLabelInterface.hh:52
MHO_IndexLabelInterface()
Definition: MHO_IndexLabelInterface.hh:25
Definition: MHO_AdhocFlagging.hh:18
Definition: vex.h:175