HOPS
HOPS class reference
MHO_OperatorToolbox.hh
Go to the documentation of this file.
1 #ifndef MHO_OperatorToolbox_HH__
2 #define MHO_OperatorToolbox_HH__
3 
4 #include <algorithm>
5 #include <map>
6 #include <memory>
7 #include <string>
8 #include <vector>
9 
10 #include "MHO_Message.hh"
11 #include "MHO_Operator.hh"
12 
13 namespace hops
14 {
15 
28 {
29  public:
31 
34 
35  virtual ~MHO_OperatorToolbox() { Clear(); }
36 
37  //insertion
49  void AddOperator(std::unique_ptr< MHO_Operator > op, const std::string& name, const std::string& category,
50  bool replace_duplicate = true)
51  {
52  msg_debug("operators",
53  "adding an operator to the toolbox with name: " << name << " in category: " << category << eom);
54  auto it = fNameToOperatorMap.find(name);
55  if(it != fNameToOperatorMap.end() && replace_duplicate)
56  {
57  RemoveOperator(name);
58  }
59 
60  MHO_Operator* raw = op.get();
61  fNameToOperatorMap[name] = raw;
62  fCategoryToOperatorMap.emplace(category, raw);
63  fOperators.push_back(std::move(op));
64  }
65 
72  MHO_Operator* GetOperator(const std::string& name)
73  {
74  MHO_Operator* ptr = nullptr;
75  auto it = fNameToOperatorMap.find(name);
76  if(it != fNameToOperatorMap.end())
77  {
78  ptr = it->second;
79  }
80  return ptr;
81  }
82 
89  MHO_Operator* GetOperator(const char* name)
90  {
91  std::string sname(name);
92  return GetOperator(sname);
93  }
94 
102  std::vector< MHO_Operator* > GetAllOperatorsByName(const std::string& name)
103  {
104  std::vector< MHO_Operator* > ops;
105  for(const auto& uptr : fOperators)
106  {
107  if(uptr->GetName() == name)
108  {
109  ops.push_back(uptr.get());
110  }
111  }
112  operator_predicate op_pred;
113  std::sort(ops.begin(), ops.end(), op_pred);
114  return ops;
115  }
116 
123  template< typename XOperatorType > XOperatorType* GetOperatorAs(const std::string& name)
124  {
125  XOperatorType* ptr = nullptr;
126  MHO_Operator* gptr = GetOperator(name);
127  if(gptr != nullptr)
128  {
129  ptr = dynamic_cast< XOperatorType* >(gptr);
130  }
131  return ptr;
132  }
133 
139  std::size_t GetNOperators() { return fOperators.size(); }
140 
141  //get all operators in the toolbox
147  std::vector< MHO_Operator* > GetAllOperators()
148  {
149  std::vector< MHO_Operator* > ops;
150  for(const auto& uptr : fOperators)
151  {
152  ops.push_back(uptr.get());
153  }
154  operator_predicate op_pred;
155  std::sort(ops.begin(), ops.end(), op_pred);
156  return ops;
157  }
158 
166  std::vector< MHO_Operator* > GetOperatorsByPriorityRange(double lower_limit, double upper_limit)
167  {
168  std::vector< MHO_Operator* > ops;
169  for(const auto& uptr : fOperators)
170  {
171  double priority = uptr->Priority();
172  if(priority < upper_limit && lower_limit <= priority)
173  {
174  ops.push_back(uptr.get());
175  }
176  }
177  operator_predicate op_pred;
178  std::sort(ops.begin(), ops.end(), op_pred);
179  return ops;
180  }
181 
182  //get all operators by category
189  std::vector< MHO_Operator* > GetOperatorsByCategory(const std::string& category)
190  {
191  std::vector< MHO_Operator* > ops;
192  auto it1 = fCategoryToOperatorMap.lower_bound(category);
193  auto it2 = fCategoryToOperatorMap.upper_bound(category);
194  if(it1 != fCategoryToOperatorMap.end())
195  {
196  while(it1 != it2)
197  {
198  ops.push_back(it1->second);
199  it1++;
200  }
201  }
202  //sort in order of priority
203  operator_predicate op_pred;
204  std::sort(ops.begin(), ops.end(), op_pred);
205  return ops;
206  }
207 
208  std::size_t GetNOperatorsInCategory(const std::string& cat) { return fCategoryToOperatorMap.count(cat); }
209 
210  //debugging
212  {
213  for(auto it = fNameToOperatorMap.begin(); it != fNameToOperatorMap.end(); it++)
214  {
215  std::string op_name = it->first;
216  MHO_Operator* op_ptr = it->second;
217 
218  //brute force search for category
219  std::string op_category = "none";
220  for(auto it2 = fCategoryToOperatorMap.begin(); it2 != fCategoryToOperatorMap.end(); it2++)
221  {
222  if(it->second == op_ptr)
223  {
224  op_category = it->first;
225  break;
226  }
227  }
228 
229  std::cout << "operator: " << op_name << ", category: " << op_category << std::endl;
230  }
231  }
232 
233  private:
239  void RemoveOperator(const std::string& name)
240  {
241  auto it = fNameToOperatorMap.find(name);
242  if(it != fNameToOperatorMap.end())
243  {
244  MHO_Operator* op_ptr = it->second;
245 
246  fNameToOperatorMap.erase(it);
247 
248  for(auto cat_iter = fCategoryToOperatorMap.begin(); cat_iter != fCategoryToOperatorMap.end(); ++cat_iter)
249  {
250  if(cat_iter->second == op_ptr)
251  {
252  fCategoryToOperatorMap.erase(cat_iter);
253  break;
254  }
255  }
256 
257  // erase from owned storage last - unique_ptr destructor deletes the object
258  auto owned_it =
259  std::find_if(fOperators.begin(), fOperators.end(),
260  [op_ptr](const std::unique_ptr< MHO_Operator >& uptr) { return uptr.get() == op_ptr; });
261  if(owned_it != fOperators.end())
262  {
263  fOperators.erase(owned_it);
264  }
265  }
266  }
267 
271  void Clear()
272  {
273  fOperators.clear(); // unique_ptrs delete their objects
274  fNameToOperatorMap.clear();
275  fCategoryToOperatorMap.clear();
276  }
277 
278  // owned storage - unique_ptrs are the sole owners
279  std::vector< std::unique_ptr< MHO_Operator > > fOperators;
280 
281  //look up operators by name (non-owning)
282  std::map< std::string, MHO_Operator* > fNameToOperatorMap;
283 
284  //look up operators by category (non-owning)
285  std::multimap< std::string, MHO_Operator* > fCategoryToOperatorMap;
286 
287  struct operator_predicate
288  {
289  bool operator()(const MHO_Operator* a, const MHO_Operator* b) const { return a->Priority() < b->Priority(); }
290  };
291 };
292 
293 } // namespace hops
294 
295 #endif
#define msg_debug(xKEY, xCONTENT)
Definition: MHO_Message.hh:291
Class MHO_OperatorToolbox.
Definition: MHO_OperatorToolbox.hh:28
void AddOperator(std::unique_ptr< MHO_Operator > op, const std::string &name, const std::string &category, bool replace_duplicate=true)
Adds an operator to the toolbox with optional replacement if duplicate name exists.
Definition: MHO_OperatorToolbox.hh:49
std::vector< MHO_Operator * > GetAllOperatorsByName(const std::string &name)
Getter for all operators with the given name - returns all operators whose name matches,...
Definition: MHO_OperatorToolbox.hh:102
MHO_OperatorToolbox()
Definition: MHO_OperatorToolbox.hh:30
MHO_Operator * GetOperator(const char *name)
Getter for an operator by name.
Definition: MHO_OperatorToolbox.hh:89
MHO_OperatorToolbox(const MHO_OperatorToolbox &)=delete
void PrintOperatorNames()
Definition: MHO_OperatorToolbox.hh:211
std::vector< MHO_Operator * > GetAllOperators()
Getter for all operators (vector of pointers)
Definition: MHO_OperatorToolbox.hh:147
MHO_OperatorToolbox & operator=(const MHO_OperatorToolbox &)=delete
MHO_Operator * GetOperator(const std::string &name)
Getter for operator - retrieval by name as generic operator, returns nullptr if missing.
Definition: MHO_OperatorToolbox.hh:72
std::vector< MHO_Operator * > GetOperatorsByCategory(const std::string &category)
Getter for operators by category.
Definition: MHO_OperatorToolbox.hh:189
std::size_t GetNOperators()
Getter for number of operators.
Definition: MHO_OperatorToolbox.hh:139
std::vector< MHO_Operator * > GetOperatorsByPriorityRange(double lower_limit, double upper_limit)
Getter for operators by priority range - get all operators within the priority range [low,...
Definition: MHO_OperatorToolbox.hh:166
virtual ~MHO_OperatorToolbox()
Definition: MHO_OperatorToolbox.hh:35
XOperatorType * GetOperatorAs(const std::string &name)
Getter for operator, retrieval by name, with cast to specified type (XOperatorType),...
Definition: MHO_OperatorToolbox.hh:123
std::size_t GetNOperatorsInCategory(const std::string &cat)
Definition: MHO_OperatorToolbox.hh:208
Class MHO_Operator.
Definition: MHO_Operator.hh:21
Definition: MHO_AdhocFlagging.hh:18