EventAnalysis  1.3.0
ObjectProducer.h
Go to the documentation of this file.
1 /*
2  * ObjectProducer.h
3  *
4  * Created on: 22 Oct 2019
5  * Author: Nicola Mori
6  */
9 #ifndef OBJECTPRODUCER_H_
10 #define OBJECTPRODUCER_H_
11 
12 #include "core/Exception.h"
13 #include "data/ObjectCategory.h"
14 #include "utils/Memory.h"
15 
16 #include <algorithm>
17 #include <memory>
18 #include <set>
19 #include <string>
20 #include <vector>
21 
22 namespace EA {
23 
30 public:
33 
35  virtual ~ObjectProducer() {}
36 
41  struct ProducedObject {
42  std::string name;
44  std::set<std::string> aliases;
45  std::string store;
46  ProducedObject() : name{""}, category{ObjectCategory::NONE}, store{""} {}
47  template <typename C>
48  ProducedObject(std::string objName, ObjectCategory objCategory, C objAliases, std::string objStore);
49 
58  bool operator==(const ProducedObject &rhs) const {
59  return (name == rhs.name) && (category == rhs.category) && (store == rhs.store);
60  }
61  };
62 
79  template <typename C>
80  void DeclareProducedObject(std::string name, ObjectCategory category, C aliases, std::string store);
81 
82  // In-class exceptions
84  class BadName : public Exception {
86  };
88  class BadCategory : public Exception {
90  };
91 
93  using ProducedObjects = std::vector<ProducedObject>;
94 
103  if (_disabled) {
104  static const ProducedObjects emptyList;
105  return emptyList;
106  } else {
107  return _repr->objs;
108  }
109  }
110 
118  bool SetDisabled(bool disabled = true) {
119  _disabled = disabled;
120  return true;
121  }
133  virtual std::vector<std::string> FreeObjects(const std::vector<std::string> &objs, Memory::Status memStatus);
134 
135 protected:
137  friend class ObjectProducer;
139  };
140 
141  ObjectProducer(const std::shared_ptr<Representation> &impl) : _repr{impl} {}
142  std::shared_ptr<Representation> &GetRepresentation() { return _repr; }
143 
144 private:
145  std::shared_ptr<Representation> _repr;
146  bool _disabled = false;
147 };
148 
149 // Definition of template member functions
150 
151 // C is required to be a container of std::string. User specializations may relax these requirements.
152 template <typename C>
153 ObjectProducer::ProducedObject::ProducedObject(std::string objName, ObjectCategory objCategory, C objAliases,
154  std::string objStore)
155  : name{std::move(objName)}, category{std::move(objCategory)}, store{std::move(objStore)} {
156 
157  for (auto &alias : objAliases) {
158  if (alias != "") {
159  aliases.emplace(std::move(alias));
160  }
161  }
162 }
163 
164 template <typename C>
166 
167  const std::string routineName("ObjectProducer::DeclareProducedObject");
168 
169  if (name == "") {
170  throw BadName(routineName + ": no name given for produced object");
171  }
172  if (category == ObjectCategory::NONE) {
173  throw BadCategory(routineName + ": bad category for produced object " + name);
174  }
175 
176  ProducedObject declaredObj(std::move(name), std::move(category), "", std::move(store));
177  auto objIter = std::find(_repr->objs.begin(), _repr->objs.end(), declaredObj);
178  auto &effectiveObj = (objIter != _repr->objs.end() ? *objIter : declaredObj);
179  for (auto &alias : aliases) {
180  if (alias != "") {
181  effectiveObj.aliases.emplace(std::move(alias));
182  }
183  }
184  if (objIter == _repr->objs.end()) {
185  _repr->objs.emplace_back(declaredObj);
186  }
187 }
188 
189 // To avoid the template code being compiled in multiple translation units repeatedly for the most common template
190 // arguments, explicitly instantiate them as extern templates (except when compiling ObjectProducer.cpp where some full
191 // specialization is defined and the actual instantiation occurs).
192 #ifndef OBJECTPRODUCER_CPP
193 extern template ObjectProducer::ProducedObject::ProducedObject(std::string objName, ObjectCategory objCategory,
194  std::set<std::string> objAliases, std::string objStore);
195 extern template ObjectProducer::ProducedObject::ProducedObject(std::string objName, ObjectCategory objCategory,
196  std::string objAlias, std::string objStore);
197 extern template ObjectProducer::ProducedObject::ProducedObject(std::string objName, ObjectCategory objCategory,
198  const char *objAlias, std::string objStore);
199 
200 extern template void ObjectProducer::DeclareProducedObject(std::string name, ObjectCategory category,
201  std::set<std::string> alias, std::string store);
202 extern template void ObjectProducer::DeclareProducedObject(std::string name, ObjectCategory category, std::string alias,
203  std::string store);
204 extern template void ObjectProducer::DeclareProducedObject(std::string name, ObjectCategory category, const char *alias,
205  std::string store);
206 #endif
207 
208 } // namespace EA
209 
210 #endif /* OBJECTPRODUCER_H_ */
Exception class for bad object category.
Definition: ObjectProducer.h:88
const ProducedObjects & GetProducedObjects() const
Getter method for produced objects.
Definition: ObjectProducer.h:102
std::string store
Definition: ObjectProducer.h:45
Exception(std::string msg="")
Definition: Exception.h:31
void DeclareProducedObject(std::string name, ObjectCategory category, C aliases, std::string store)
Declare a produced objects.
Definition: ObjectProducer.h:165
bool operator==(const ProducedObject &rhs) const
Comparison operator.
Definition: ObjectProducer.h:58
virtual ~ObjectProducer()
Destructor.
Definition: ObjectProducer.h:35
IncludeFileExc.h IncludeFileExc class declaration.
Definition: Algorithm.h:21
bool SetDisabled(bool disabled=true)
Disables/enables the producer.
Definition: ObjectProducer.h:118
Descriptor structure for a produced object.
Definition: ObjectProducer.h:41
std::vector< ProducedObject > ProducedObjects
Type for container of produced objects.
Definition: ObjectProducer.h:93
std::shared_ptr< Representation > & GetRepresentation()
Definition: ObjectProducer.h:142
ProducedObjects objs
Definition: ObjectProducer.h:138
Definition: ObjectProducer.h:136
std::set< std::string > aliases
Definition: ObjectProducer.h:44
ObjectProducer()
Constructor.
Definition: ObjectProducer.cpp:17
ObjectCategory category
Definition: ObjectProducer.h:43
ObjectCategory
Category of objects.
Definition: ObjectCategory.h:17
Exception class for bad object name.
Definition: ObjectProducer.h:84
std::string name
Definition: ObjectProducer.h:42
Interface for a class which produced data objects.
Definition: ObjectProducer.h:29
Status
Aliases for memory occupation levels.
Definition: Memory.h:23
ProducedObject()
Definition: ObjectProducer.h:46
Definition: Exception.h:24
bool _disabled
Definition: ObjectProducer.h:146
virtual std::vector< std::string > FreeObjects(const std::vector< std::string > &objs, Memory::Status memStatus)
Free the memory for given objects.
Definition: ObjectProducer.cpp:82
ObjectProducer(const std::shared_ptr< Representation > &impl)
Definition: ObjectProducer.h:141
std::shared_ptr< Representation > _repr
Definition: ObjectProducer.h:145