EventAnalysis  1.3.0
ObjectConsumer.h
Go to the documentation of this file.
1 /*
2  * ObjectConsumer.h
3  *
4  * Created on: 22 Oct 2019
5  * Author: Nicola Mori
6  */
9 #ifndef OBJECTCONSUMER_H_
10 #define OBJECTCONSUMER_H_
11 
12 #include "core/Exception.h"
13 #include "core/ObserverPtr.h"
14 #include "data/ObjectCategory.h"
15 
16 #include <memory>
17 #include <string>
18 #include <vector>
19 
20 namespace EA {
21 
28 public:
30  ObjectConsumer() : _repr{std::make_shared<Representation>()} {}
31 
36  struct ConsumedObject {
37  std::string name;
39  std::string store;
40  bool optional;
41 
50  : name(""), category(ObjectCategory::NONE), store(""), optional(false), _prevAlt(nullptr), _nextAlt(nullptr) {}
51 
56  std::vector<observer_ptr<ConsumedObject>> GetAlternatives() const;
57 
58  private:
59  friend class ObjectConsumer;
60  // Pointers to alternative objects (bidirectional linked list with nullptr at the extrema)
62  };
63 
77  void DeclareConsumedObject(std::string name, ObjectCategory category, std::string store, bool optional = false);
78 
111  void DeclareAlternative(const std::string &name, ObjectCategory category, std::string store,
112  const std::string &alternativeName, ObjectCategory alternativeCategory,
113  std::string alternativeStore);
114 
115  // In-class exceptions
117  class UndeclaredObject : public Exception {
118  using Exception::Exception;
119  };
121  class BadName : public Exception {
122  using Exception::Exception;
123  };
125  class BadCategory : public Exception {
126  using Exception::Exception;
127  };
128 
130  using ConsumedObjects = std::vector<ConsumedObject>;
131 
139  const ConsumedObjects &GetConsumedObjects() const { return _repr->objs; }
140 
141 protected:
143  friend class ObjectConsumer;
144 
145  public:
147  };
148 
149  ObjectConsumer(const std::shared_ptr<Representation> &impl) : _repr{impl} {}
150  std::shared_ptr<Representation> &GetRepresentation() { return _repr; }
151 
152 private:
153  std::shared_ptr<Representation> _repr;
154 };
155 
156 } // namespace EA
157 
158 #endif /* OBJECTCONSUMER_H_ */
bool optional
Definition: ObjectConsumer.h:40
observer_ptr< ConsumedObject > _prevAlt
Definition: ObjectConsumer.h:61
Definition: ObjectConsumer.h:142
ObjectConsumer()
Constructor.
Definition: ObjectConsumer.h:30
ConsumedObject()
Constructor.
Definition: ObjectConsumer.h:49
Exception class for bad object name.
Definition: ObjectConsumer.h:121
A smart pointer not owning the wrapped object.
Definition: ObserverPtr.h:28
std::vector< ConsumedObject > ConsumedObjects
Type for container of produced objects.
Definition: ObjectConsumer.h:130
Exception(std::string msg="")
Definition: Exception.h:31
Exception class for using undeclared objects as parameters of DeclareAlternative. ...
Definition: ObjectConsumer.h:117
IncludeFileExc.h IncludeFileExc class declaration.
Definition: Algorithm.h:21
ConsumedObjects objs
Definition: ObjectConsumer.h:146
const ConsumedObjects & GetConsumedObjects() const
Getter method for consumed objects.
Definition: ObjectConsumer.h:139
std::string name
Definition: ObjectConsumer.h:37
std::string store
Definition: ObjectConsumer.h:39
ObjectCategory
Category of objects.
Definition: ObjectCategory.h:17
Exception class for bad object category.
Definition: ObjectConsumer.h:125
void DeclareConsumedObject(std::string name, ObjectCategory category, std::string store, bool optional=false)
Declare a consumed objects.
Definition: ObjectConsumer.cpp:18
std::shared_ptr< Representation > & GetRepresentation()
Definition: ObjectConsumer.h:150
observer_ptr< ConsumedObject > _nextAlt
Definition: ObjectConsumer.h:61
std::shared_ptr< Representation > _repr
Definition: ObjectConsumer.h:153
std::vector< observer_ptr< ConsumedObject > > GetAlternatives() const
Definition: ObjectConsumer.cpp:82
Descriptor structure for a consumed object.
Definition: ObjectConsumer.h:36
ObjectCategory category
Definition: ObjectConsumer.h:38
void DeclareAlternative(const std::string &name, ObjectCategory category, std::string store, const std::string &alternativeName, ObjectCategory alternativeCategory, std::string alternativeStore)
Declares two already declared objects as alternatives.
Definition: ObjectConsumer.cpp:43
ObjectConsumer(const std::shared_ptr< Representation > &impl)
Definition: ObjectConsumer.h:149
Definition: Exception.h:24
Interface for a class which consumes data objects.
Definition: ObjectConsumer.h:27