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 <string>
7 #include <vector>
8 
9 #include "MHO_Operator.hh"
10 
11 namespace hops
12 {
13 
26 {
27  public:
29 
30  virtual ~MHO_OperatorToolbox() { Clear(); }
31 
32  //insertion
41  void AddOperator(MHO_Operator* op, const std::string& name, const std::string& category, bool replace_duplicate = true)
42  {
43  msg_debug("operators",
44  "adding an operator to the toolbox with name: " << name << " in category: " << category << eom);
45  auto it = fNameToOperatorMap.find(name);
46  if(it != fNameToOperatorMap.end() && replace_duplicate)
47  {
48  RemoveOperator(name);
49  }
50 
51  fNameToOperatorMap[name] = op;
52  fCategoryToOperatorMap.emplace(category, op);
53  fOperators.insert(op);
54  }
55 
62  MHO_Operator* GetOperator(const std::string& name)
63  {
64  MHO_Operator* ptr = nullptr;
65  auto it = fNameToOperatorMap.find(name);
66  if(it != fNameToOperatorMap.end())
67  {
68  ptr = it->second;
69  }
70  return ptr;
71  }
72 
79  MHO_Operator* GetOperator(const char* name)
80  {
81  std::string sname(name);
82  return GetOperator(sname);
83  }
84 
91  template< typename XOperatorType > XOperatorType* GetOperatorAs(const std::string& name)
92  {
93  XOperatorType* ptr = nullptr;
94  MHO_Operator* gptr = GetOperator(name);
95  if(gptr != nullptr)
96  {
97  ptr = dynamic_cast< XOperatorType* >(gptr);
98  }
99  return ptr;
100  }
101 
107  std::size_t GetNOperators() { return fOperators.size(); }
108 
109  //get all operators in the toolbox
115  std::vector< MHO_Operator* > GetAllOperators()
116  {
117  std::vector< MHO_Operator* > ops;
118  for(auto it = fOperators.begin(); it != fOperators.end(); it++)
119  {
120  ops.push_back(*it);
121  }
122 
123  operator_predicate op_pred;
124  std::sort(ops.begin(), ops.end(), op_pred);
125  return ops;
126  }
127 
135  std::vector< MHO_Operator* > GetOperatorsByPriorityRange(double lower_limit, double upper_limit)
136  {
137  std::vector< MHO_Operator* > ops;
138  for(auto it = fOperators.begin(); it != fOperators.end(); it++)
139  {
140  double priority = (*it)->Priority();
141  if(priority < upper_limit && lower_limit <= priority)
142  {
143  ops.push_back(*it);
144  }
145  }
146  //sort in order of priority
147  operator_predicate op_pred;
148  std::sort(ops.begin(), ops.end(), op_pred);
149  return ops;
150  }
151 
152  //get all operators by category
159  std::vector< MHO_Operator* > GetOperatorsByCategory(const std::string& category)
160  {
161  std::vector< MHO_Operator* > ops;
162  auto it1 = fCategoryToOperatorMap.lower_bound(category);
163  auto it2 = fCategoryToOperatorMap.upper_bound(category);
164  if(it1 != fCategoryToOperatorMap.end())
165  {
166  while(it1 != it2)
167  {
168  ops.push_back(it1->second);
169  it1++;
170  }
171  }
172  //sort in order of priority
173  operator_predicate op_pred;
174  std::sort(ops.begin(), ops.end(), op_pred);
175  return ops;
176  }
177 
178  private:
184  void RemoveOperator(const std::string& name)
185  {
186  auto it = fNameToOperatorMap.find(name);
187  if(it != fNameToOperatorMap.end())
188  {
189  auto op_ptr = it->second;
190  //remove from the operator set
191  auto op_iter = fOperators.find(op_ptr);
192  if(op_iter != fOperators.end())
193  {
194  fOperators.erase(op_iter);
195  }
196 
197  //remove from the category map
198  for(auto cat_iter = fCategoryToOperatorMap.begin(); cat_iter != fCategoryToOperatorMap.end(); cat_iter++)
199  {
200  if(cat_iter->second == op_ptr)
201  {
202  fCategoryToOperatorMap.erase(cat_iter);
203  break;
204  }
205  }
206 
207  //remove from the named operator map
208  fNameToOperatorMap.erase(it);
209 
210  //finally delete the operator
211  delete op_ptr;
212  }
213  }
214 
218  void Clear()
219  {
220  //delete all the operators
221  for(auto it = fOperators.begin(); it != fOperators.end(); it++)
222  {
223  delete *it;
224  }
225  fOperators.clear();
226  fNameToOperatorMap.clear();
227  fCategoryToOperatorMap.clear();
228  };
229 
230  //store operator pointers for memory management
231  std::set< MHO_Operator* > fOperators;
232 
233  //look up operators by name
234  std::map< std::string, MHO_Operator* > fNameToOperatorMap;
235 
236  //look up operators by category
237  std::multimap< std::string, MHO_Operator* > fCategoryToOperatorMap;
238 
239  //for sorting operator priorites
243  class operator_predicate
244  {
245  public:
246  operator_predicate(){};
247  virtual ~operator_predicate(){};
248 
249  virtual bool operator()(const MHO_Operator* a, const MHO_Operator* b) { return a->Priority() < b->Priority(); }
250  };
251 };
252 
253 } // namespace hops
254 
255 #endif
#define msg_debug(xKEY, xCONTENT)
Definition: MHO_Message.hh:297
Class MHO_OperatorToolbox.
Definition: MHO_OperatorToolbox.hh:26
MHO_OperatorToolbox()
Definition: MHO_OperatorToolbox.hh:28
MHO_Operator * GetOperator(const char *name)
Getter for an operator by name.
Definition: MHO_OperatorToolbox.hh:79
std::vector< MHO_Operator * > GetAllOperators()
Getter for all operators (vector of pointers)
Definition: MHO_OperatorToolbox.hh:115
MHO_Operator * GetOperator(const std::string &name)
Getter for operator - retrieval by name as generic operator, returns nullptr if missing.
Definition: MHO_OperatorToolbox.hh:62
std::vector< MHO_Operator * > GetOperatorsByCategory(const std::string &category)
Getter for operators by category.
Definition: MHO_OperatorToolbox.hh:159
void AddOperator(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:41
std::size_t GetNOperators()
Getter for number of operators.
Definition: MHO_OperatorToolbox.hh:107
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:135
virtual ~MHO_OperatorToolbox()
Definition: MHO_OperatorToolbox.hh:30
XOperatorType * GetOperatorAs(const std::string &name)
Getter for operator, retrieval by name, with cast to specified type (XOperatorType),...
Definition: MHO_OperatorToolbox.hh:91
Class MHO_Operator.
Definition: MHO_Operator.hh:21
Definition: MHO_ChannelLabeler.hh:17