ADTF
Loading...
Searching...
No Matches
infomodel_base.h
Go to the documentation of this file.
1
14
15#ifndef UTILITIES_INFOMODEL_BASE_H_INCLUDED
16#define UTILITIES_INFOMODEL_BASE_H_INCLUDED
17
21
23
24#include <array>
25#include <cstddef>
26#include <cstdint>
27#include <memory>
28#include <string>
29#include <typeinfo>
30#include <utility>
31
32namespace ddl {
33
34namespace dd {
35
36namespace datamodel {
37
41class IInfo {
42public:
47 virtual ~IInfo() = default;
48
54 virtual uint8_t getInfoType() const noexcept = 0;
55};
56
62template <typename INFO_T>
63class Info : public IInfo {
64protected:
71 uint8_t getInfoType() const noexcept override
72 {
73 return INFO_T::INFO_TYPE_ID;
74 }
75};
76
83template <typename INFO_T>
84class InfoSlot {
85public:
87 InfoSlot() = default;
89 InfoSlot(const InfoSlot& /*other*/) noexcept
90 {
91 }
92
93 InfoSlot(InfoSlot&& /*other*/) noexcept
94 {
95 }
96
97 InfoSlot& operator=(const InfoSlot& /*other*/) noexcept
98 {
99 return *this;
100 }
101
102 InfoSlot& operator=(InfoSlot&& /*other*/) noexcept
103 {
104 return *this;
105 }
106
107 ~InfoSlot() = default;
108
113 const INFO_T* get() const noexcept
114 {
115 return _info.get();
116 }
117
121 INFO_T* get() noexcept
122 {
123 return _info.get();
124 }
125
129 void set(std::shared_ptr<INFO_T> info) noexcept
130 {
131 _info = std::move(info);
132 }
133
134private:
135 std::shared_ptr<INFO_T> _info;
136};
137
142class InfoMap {
143public:
147 InfoMap() = default;
152 InfoMap(const InfoMap&){};
164 {
165 return *this;
166 };
167
173 {
174 return *this;
175 };
176
182 template <typename INFO_T>
183 const INFO_T* getInfo() const noexcept
184 {
185 static_assert(INFO_T::INFO_TYPE_ID < info_type_count, "INFO_TYPE_ID is no InfoType");
186 const IInfo* const info = _infos[INFO_T::INFO_TYPE_ID].get();
187 // Different info types may share an id, so the type has to be checked. Comparing the
188 // exact type is much cheaper than dynamic_cast, which remains for everything else.
189 if (info != nullptr && typeid(*info) == typeid(INFO_T)) {
190 return static_cast<const INFO_T*>(info);
191 }
192 return dynamic_cast<const INFO_T*>(info);
193 }
194
200 template <typename INFO_T>
201 INFO_T* getInfo() noexcept
202 {
203 return const_cast<INFO_T*>(static_cast<const InfoMap*>(this)->getInfo<INFO_T>());
204 }
205
212 template <typename INFO_T>
213 void setInfo(const std::shared_ptr<INFO_T>& info)
214 {
215 static_assert(INFO_T::INFO_TYPE_ID < info_type_count, "INFO_TYPE_ID is no InfoType");
216 std::shared_ptr<IInfo> info_downcast = info;
217 if (info_downcast->getInfoType() == INFO_T::INFO_TYPE_ID) {
218 _infos[INFO_T::INFO_TYPE_ID] = std::move(info_downcast);
219 }
220 else {
221 throw Error(
222 "InfoMap::setInfo",
223 "Registration not possible, runtime info is differnt then compiletime info");
224 }
225 }
226
227private:
228 static constexpr std::size_t info_type_count = InfoType::validation_service_info + 1;
229
230#if defined(__GNUC__) && ((__GNUC__ == 5) && (__GNUC_MINOR__ == 2))
231#pragma GCC diagnostic push
232#pragma GCC diagnostic ignored "-Wattributes"
233#endif // defined(__GNUC__) && ((__GNUC__ == 5) && (__GNUC_MINOR__ == 2))
234
237 std::array<std::shared_ptr<IInfo>, info_type_count> _infos;
238
239#if defined(__GNUC__) && ((__GNUC__ == 5) && (__GNUC_MINOR__ == 2))
240#pragma GCC diagnostic pop
241#endif // defined(__GNUC__) && ((__GNUC__ == 5) && (__GNUC_MINOR__ == 2))
242};
243
244} // namespace datamodel
245} // namespace dd
246
247namespace utility {
251}
252
253} // namespace ddl
254
255#endif // UTILITIES_INFOMODEL_BASE_H_INCLUDED
Exception helper class to collect information while parsing, adding DD Objects or other failed operat...
Definition ddl_error.h:31
Basic Info interface can be added to the ddl::dd::datamodel::InfoMap.
Definition infomodel_base.h:41
virtual ~IInfo()=default
virtual DTOR for IInfo object
virtual uint8_t getInfoType() const noexcept=0
Get the Info Type ID.
Info Map for the datamodel to hold a set of optional IInfo instances. Only one instance of one Info t...
Definition infomodel_base.h:142
InfoMap & operator=(InfoMap &&)
move assignment operator
Definition infomodel_base.h:172
void setInfo(const std::shared_ptr< INFO_T > &info)
Set the Info object as shared pointer.
Definition infomodel_base.h:213
INFO_T * getInfo() noexcept
Get the Info Pointer.
Definition infomodel_base.h:201
InfoMap(InfoMap &&)
move CTOR
Definition infomodel_base.h:157
InfoMap & operator=(const InfoMap &)
copy assignment operator
Definition infomodel_base.h:163
const INFO_T * getInfo() const noexcept
Get the Info Pointer.
Definition infomodel_base.h:183
InfoMap(const InfoMap &)
copy CTOR
Definition infomodel_base.h:152
InfoSlot(const InfoSlot &) noexcept
Creates an empty slot, the info of other is not copied.
Definition infomodel_base.h:89
~InfoSlot()=default
Destroys the slot and releases its info.
InfoSlot()=default
Creates an empty slot.
InfoSlot(InfoSlot &&) noexcept
Creates an empty slot, the info of other is not moved.
Definition infomodel_base.h:93
InfoSlot & operator=(const InfoSlot &) noexcept
Keeps the own info, the info of other is not copied.
Definition infomodel_base.h:97
const INFO_T * get() const noexcept
Get the info.
Definition infomodel_base.h:113
InfoSlot & operator=(InfoSlot &&) noexcept
Keeps the own info, the info of other is not moved.
Definition infomodel_base.h:102
void set(std::shared_ptr< INFO_T > info) noexcept
Set the info.
Definition infomodel_base.h:129
INFO_T * get() noexcept
Get the info.
Definition infomodel_base.h:121
Helper template class to create valid IInfo objects.
Definition infomodel_base.h:63
uint8_t getInfoType() const noexcept override
Get the Info Type object.
Definition infomodel_base.h:71
definition of the datamodel namespace
Definition datamodel_base.h:28
definition of the dd namespace
Definition datamodel_base.h:26
@ validation_service_info
Validation Info Service.
Definition dd_infomodel_type.h:36
definition of the ddl namespace
Definition codec.h:21