EventAnalysis  1.2.2
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 
102  const ProducedObjects &GetProducedObjects() const { return _repr->objs; }
103 
115  virtual std::vector<std::string> FreeObjects(const std::vector<std::string> &objs, Memory::Status memStatus);
116 
117 protected:
119  friend class ObjectProducer;
121  };
122 
123  ObjectProducer(const std::shared_ptr<Representation> &impl) : _repr{impl} {}
124  std::shared_ptr<Representation> &GetRepresentation() { return _repr; }
125 
126 private:
127  std::shared_ptr<Representation> _repr;
128 };
129 
130 // Definition of template member functions
131 
132 // C is required to be a container of std::string. User specializations may relax these requirements.
133 template <typename C>
134 ObjectProducer::ProducedObject::ProducedObject(std::string objName, ObjectCategory objCategory, C objAliases,
135  std::string objStore)
136  : name{std::move(objName)}, category{std::move(objCategory)}, store{std::move(objStore)} {
137 
138  for (auto &alias : objAliases) {
139  if (alias != "") {
140  aliases.emplace(std::move(alias));
141  }
142  }
143 }
144 
145 template <typename C>
147 
148  const std::string routineName("ObjectProducer::DeclareProducedObject");
149 
150  if (name == "") {
151  throw BadName(routineName + ": no name given for produced object");
152  }
153  if (category == ObjectCategory::NONE) {
154  throw BadCategory(routineName + ": bad category for produced object " + name);
155  }
156 
157  ProducedObject declaredObj(std::move(name), std::move(category), "", std::move(store));
158  auto objIter = std::find(_repr->objs.begin(), _repr->objs.end(), declaredObj);
159  auto &effectiveObj = (objIter != _repr->objs.end() ? *objIter : declaredObj);
160  for (auto &alias : aliases) {
161  if (alias != "") {
162  effectiveObj.aliases.emplace(std::move(alias));
163  }
164  }
165  if (objIter == _repr->objs.end()) {
166  _repr->objs.emplace_back(declaredObj);
167  }
168 }
169 
170 // To avoid the template code being compiled in multiple translation units repeatedly for the most common template
171 // arguments, explicitly instantiate them as extern templates (except when compiling ObjectProducer.cpp where some full
172 // specialization is defined and the actual instantiation occurs).
173 #ifndef OBJECTPRODUCER_CPP
174 extern template ObjectProducer::ProducedObject::ProducedObject(std::string objName, ObjectCategory objCategory,
175  std::set<std::string> objAliases, std::string objStore);
176 extern template ObjectProducer::ProducedObject::ProducedObject(std::string objName, ObjectCategory objCategory,
177  std::string objAlias, std::string objStore);
178 extern template ObjectProducer::ProducedObject::ProducedObject(std::string objName, ObjectCategory objCategory,
179  const char *objAlias, std::string objStore);
180 
181 extern template void ObjectProducer::DeclareProducedObject(std::string name, ObjectCategory category,
182  std::set<std::string> alias, std::string store);
183 extern template void ObjectProducer::DeclareProducedObject(std::string name, ObjectCategory category, std::string alias,
184  std::string store);
185 extern template void ObjectProducer::DeclareProducedObject(std::string name, ObjectCategory category, const char *alias,
186  std::string store);
187 #endif
188 
189 } // namespace EA
190 
191 #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:146
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
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:124
ProducedObjects objs
Definition: ObjectProducer.h:120
Definition: ObjectProducer.h:118
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
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:123
std::shared_ptr< Representation > _repr
Definition: ObjectProducer.h:127