ADTF  3.18.2
infomodel_base.h
Go to the documentation of this file.
1 
15 #ifndef DD_DATA_MODEL_INFO_H_INCLUDED
16 #define DD_DATA_MODEL_INFO_H_INCLUDED
17 
19 #include <ddl/dd/dd_error.h>
21 
22 #include <memory>
23 #include <string>
24 #include <unordered_map>
25 
26 namespace ddl {
27 
28 namespace dd {
29 
30 namespace datamodel {
31 
35 class IInfo {
36 public:
41  virtual ~IInfo() = default;
42 
48  virtual uint8_t getInfoType() const = 0;
49 };
50 
56 template <typename INFO_T>
57 class Info : public IInfo {
58 protected:
65  uint8_t getInfoType() const override
66  {
67  return INFO_T::INFO_TYPE_ID;
68  }
69 };
70 
75 class InfoMap {
76 public:
80  InfoMap() = default;
85  InfoMap(const InfoMap&){};
97  {
98  return *this;
99  };
106  {
107  return *this;
108  };
115  template <typename INFO_T>
116  const INFO_T* getInfo() const
117  {
118  return dynamic_cast<const INFO_T*>(getInfo(INFO_T::INFO_TYPE_ID));
119  }
126  template <typename INFO_T>
127  INFO_T* getInfo()
128  {
129  return dynamic_cast<INFO_T*>(getInfo(INFO_T::INFO_TYPE_ID));
130  }
138  template <typename INFO_T>
139  void setInfo(const std::shared_ptr<INFO_T>& info)
140  {
141  std::shared_ptr<IInfo> info_downcast = info;
142  if (info_downcast->getInfoType() == INFO_T::INFO_TYPE_ID) {
143  setInfo(info_downcast);
144  }
145  else {
146  throw Error(
147  "InfoMap::setInfo",
148  "Registration not possible, runtime info is differnt then compiletime info");
149  }
150  }
151 
152 private:
153  IInfo* getInfo(uint8_t info_type)
154  {
155  auto found = _infos.find(info_type);
156  if (found != _infos.end()) {
157  return found->second.get();
158  }
159  else {
160  return nullptr;
161  }
162  }
163  const IInfo* getInfo(uint8_t info_type) const
164  {
165  auto found = _infos.find(info_type);
166  if (found != _infos.cend()) {
167  return found->second.get();
168  }
169  else {
170  return nullptr;
171  }
172  }
173  void setInfo(const std::shared_ptr<IInfo>& info)
174  {
175  _infos[info->getInfoType()] = info;
176  }
177 
178 #if defined(__GNUC__) && ((__GNUC__ == 5) && (__GNUC_MINOR__ == 2))
179 #pragma GCC diagnostic push
180 #pragma GCC diagnostic ignored "-Wattributes"
181 #endif // defined(__GNUC__) && ((__GNUC__ == 5) && (__GNUC_MINOR__ == 2))
182 
183  std::unordered_map<uint8_t, std::shared_ptr<IInfo>> _infos;
184 
185 #if defined(__GNUC__) && ((__GNUC__ == 5) && (__GNUC_MINOR__ == 2))
186 #pragma GCC diagnostic pop
187 #endif // defined(__GNUC__) && ((__GNUC__ == 5) && (__GNUC_MINOR__ == 2))
188 };
189 
190 } // namespace datamodel
191 } // namespace dd
192 } // namespace ddl
193 
194 #endif // DD_DATA_MODEL_INFO_H_INCLUDED
Basic Info interface can be added to the InfoMap.
virtual ~IInfo()=default
virtual DTOR for IInfo object
virtual uint8_t getInfoType() const =0
Get the Info Type ID.
Helper template class to create valid IInfo objects.
uint8_t getInfoType() const override
Get the Info Type object.
Info Map for the datamodel to hold a set of optional IInfo instances.
INFO_T * getInfo()
Get the Info Pointer.
InfoMap & operator=(InfoMap &&)
move assignment operator
const INFO_T * getInfo() const
Get the Info Pointer.
InfoMap(const InfoMap &)
copy CTOR
InfoMap & operator=(const InfoMap &)
copy assignment operator
InfoMap(InfoMap &&)
move CTOR
void setInfo(const std::shared_ptr< INFO_T > &info)
Set the Info object as shared pointer.
DataDefinition Model-Observer pattern utility.
OO DataDefinition Redesign.
Definition of preprocessor macros to disable/enable compiler warnings.