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