Dev Essential
Loading...
Searching...
No Matches
access_vector.h
Go to the documentation of this file.
1
20
21#ifndef DD__UTILITY_ACCESS_VECTOR_H_INCLUDED
22#define DD__UTILITY_ACCESS_VECTOR_H_INCLUDED
23
26
27#include <algorithm>
28#include <memory>
29
30namespace ddl {
31namespace utility {
40 vector_item_added,
41 vector_item_removed,
42 vector_item_changed,
43 vector_item_renamed,
44 vector_subitem_added,
45 vector_subitem_removed,
46 vector_subitem_changed,
47 vector_item_popped,
48 vector_subitem_popped,
49 vector_item_inserted,
50 vector_subitem_inserted,
51 vector_subitem_renamed,
52 vector_item_removing,
53 vector_subitem_removing,
54 vector_item_renaming,
55 vector_subitem_renaming
56};
57
64template <typename T>
66
73template <typename T>
75
82template <typename DDL_TYPE_TO_ACCESS, typename TYPE_VALIDATOR_CLASS>
83class TypeAccessVector : public TypeAccessVectorObserver<DDL_TYPE_TO_ACCESS> {
84public:
86 typedef DDL_TYPE_TO_ACCESS access_type;
88 typedef std::shared_ptr<DDL_TYPE_TO_ACCESS> value_type;
90 typedef std::vector<value_type> container_type;
92 typedef typename container_type::iterator iterator;
94 typedef typename container_type::const_iterator const_iterator;
102 typedef typename parent_type::event_code_type event_code_type;
104 typedef typename parent_type::subject_type subject_type;
107
108public:
120 TypeAccessVector(TYPE_VALIDATOR_CLASS* validator, const std::string& validation_info)
121 : _validator(validator), _validation_info(validation_info)
122 {
123 }
124
129 {
130 clear();
131 }
132
136 {
137 *this = std::move(other);
138 }
139
145 {
146 clear();
147 _validation_info = std::move(other._validation_info);
148 _types = std::move(other._types);
149 // the other will not observe this anymore after this clear
150 for (const auto& current: _types) {
151 // the other will not observe this anymore
152 (static_cast<observer_subject_type*>(current.get()))
153 ->detachObserver(static_cast<observer_type*>(&other));
154 // i want to observe this
155 (static_cast<observer_subject_type*>(current.get()))
156 ->attachObserver(static_cast<observer_type*>(this));
157 }
158 return *this;
159 }
160
167 : _validator(nullptr), _validation_info(other._validation_info)
168 {
169 for (auto current: other._types) {
170 auto new_type = std::make_shared<DDL_TYPE_TO_ACCESS>(*current);
171 // i want to observe this
172 (static_cast<observer_subject_type*>(new_type.get()))
173 ->attachObserver(static_cast<observer_type*>(this));
174 _types.push_back(new_type);
175 }
176 }
177
184 {
185 clear();
186 _validation_info = other._validation_info;
187 for (auto current: other._types) {
188 auto new_type = std::make_shared<DDL_TYPE_TO_ACCESS>(*current);
189 // i want to observe this
190 (static_cast<observer_subject_type*>(new_type.get()))
191 ->attachObserver(static_cast<observer_type*>(this));
192 _types.push_back(new_type);
193 }
194 return *this;
195 }
196
197public:
204 std::shared_ptr<const DDL_TYPE_TO_ACCESS> get(size_t index) const
205 {
206 if (_types.size() > index) {
207 std::shared_ptr<const DDL_TYPE_TO_ACCESS> const_return = _types[index];
208 return const_return;
209 }
210 return {};
211 }
212
219 void add(const DDL_TYPE_TO_ACCESS& type_to_add)
220 {
221 auto new_type_value = std::make_shared<DDL_TYPE_TO_ACCESS>(type_to_add);
222 // i want to observe this
223 (static_cast<observer_subject_type*>(new_type_value.get()))
224 ->attachObserver(static_cast<observer_type*>(this));
225 _types.push_back(new_type_value);
226
227 if (_validator) {
228 _validator->notifyChangedVectorContent(TypeAccessVectorEventCode::vector_item_added,
229 *new_type_value,
230 std::to_string(_types.size() - 1));
231 }
232 }
233
242 void insert(const size_t pos_idx, const DDL_TYPE_TO_ACCESS& type_to_add)
243 {
244 if (pos_idx > getSize()) {
245 throw ddl::dd::Error(
246 _validation_info + "::insert", {type_to_add.getName()}, "given pos_idx is invalid");
247 }
248 else if (pos_idx == getSize()) {
249 add(type_to_add);
250 }
251 else {
252 auto new_type_value = std::make_shared<DDL_TYPE_TO_ACCESS>(type_to_add);
253 // i want to observe this
254 (static_cast<observer_subject_type*>(new_type_value.get()))
255 ->attachObserver(static_cast<observer_type*>(this));
256
257 auto cit = cbegin();
258 std::advance(cit, pos_idx);
259 _types.insert(cit, new_type_value);
260
261 if (_validator) {
262 _validator->notifyChangedVectorContent(
263 TypeAccessVectorEventCode::vector_item_inserted,
264 *new_type_value,
265 std::to_string(pos_idx));
266 }
267 }
268 }
269
276 void emplace(DDL_TYPE_TO_ACCESS&& type_to_add)
277 {
278 auto new_type_value = std::make_shared<DDL_TYPE_TO_ACCESS>(std::move(type_to_add));
279 // i want to observe this
280 (static_cast<observer_subject_type*>(new_type_value.get()))
281 ->attachObserver(static_cast<observer_type*>(this));
282 _types.push_back(new_type_value);
283 if (_validator) {
284 _validator->notifyChangedVectorContent(TypeAccessVectorEventCode::vector_item_added,
285 *new_type_value,
286 std::to_string(_types.size() - 1));
287 }
288 }
289
295 void remove(size_t index)
296 {
297 if (_types.size() > index) {
298 value_type removed_value;
299 typename container_type::iterator current_it = begin();
300 std::advance(current_it, index);
301 removed_value = *current_it;
302 if (removed_value) {
303 if (_validator) {
304 _validator->notifyChangedVectorContent(
305 TypeAccessVectorEventCode::vector_item_removing,
306 *removed_value,
307 std::to_string(index));
308 }
309 _types.erase(current_it);
310 (static_cast<subject_type*>(removed_value.get()))
311 ->detachObserver(static_cast<observer_type*>(this));
312 if (_validator) {
313 _validator->notifyChangedVectorContent(
314 TypeAccessVectorEventCode::vector_item_removed,
315 *removed_value,
316 std::to_string(index));
317 }
318 return;
319 }
320 }
321 throw ddl::dd::Error(_validation_info + "::remove",
322 {std::to_string(index)},
323 "value with the index does not exist");
324 }
325
332 std::shared_ptr<DDL_TYPE_TO_ACCESS> access(size_t index)
333 {
334 if (index < _types.size()) {
335 return _types[index];
336 }
337 return {};
338 }
339
345 size_t getSize() const
346 {
347 return _types.size();
348 }
349
356 {
357 return _types.begin();
358 }
359
366 {
367 return _types.end();
368 }
369
376 {
377 return _types.cbegin();
378 }
379
386 {
387 return _types.cend();
388 }
389
396 {
397 return cbegin();
398 }
399
406 {
407 return cend();
408 }
409
417 bool operator==(const TypeAccessVector& other) const
418 {
419 return std::equal(
420 begin(), end(), other.begin(), other.end(), [](const auto& local, const auto& other) {
421 return *(local) == *(other);
422 });
423 }
424
432 bool operator!=(const TypeAccessVector& other) const
433 {
434 return !operator==(other);
435 }
436
441 void clear()
442 {
443 for (auto& current: _types) {
444 // unregister me as observer
445 (static_cast<subject_type*>(current.get()))
446 ->detachObserver(static_cast<observer_type*>(this));
447 }
448 _types.clear();
449 }
450
455 void popBack()
456 {
457 if (!_types.empty()) {
458 auto last_element = _types[_types.size() - 1];
459 // unregister me as observer
460 (static_cast<subject_type*>(last_element.get()))
461 ->detachObserver(static_cast<observer_type*>(this));
462 _types.pop_back();
463 if (_validator) {
464 _validator->notifyChangedVectorContent(
465 TypeAccessVectorEventCode::vector_item_popped, *last_element, "-1");
466 }
467 }
468 else {
469 throw ddl::dd::Error(_validation_info + "::popBack", {}, "vector is empty");
470 }
471 }
472
473public:
482 subject_type& subject_changed,
483 const std::string& additional_info)
484 {
485 if (_validator) {
486 // i should discover the shared_ptr here, but not needed at the moment
487 _validator->notifyChangedVectorContent(event_code, subject_changed, additional_info);
488 }
489 }
490
491private:
500 void deepCopy(TypeAccessVector& destination, TYPE_VALIDATOR_CLASS* validator) const
501 {
502 destination = *this;
503 destination.setValidator(validator);
504 }
505
506 void setValidator(TYPE_VALIDATOR_CLASS* validator)
507 {
508 _validator = validator;
509 }
510
511 container_type _types;
512 TYPE_VALIDATOR_CLASS* _validator = {};
513 std::string _validation_info;
514};
515
516} // namespace utility
517} // namespace ddl
518
519#endif // DD__UTILITY_ACCESS_VECTOR_H_INCLUDED
Exception helper class to collect information while parsing, adding DD Objects or other failed operat...
Definition ddl_error.h:31
The ModelObserver utility template.
Definition access_observer.h:34
Model Subject utility to define a Model Subject that notifies one or more observers.
Definition access_observer.h:68
ModelObserverUtility< MODEL_SUBJECT_T, EVENT_CODE_T > observer_type
local definition of the observer type to notify
Definition access_observer.h:75
Utility class for observable named items where the order is important.
Definition access_vector.h:83
const_iterator cend() const
const end iterator access
Definition access_vector.h:385
size_t getSize() const
Get the Size.
Definition access_vector.h:345
TypeAccessVectorSubject< Trigger > observer_subject_type
Definition access_vector.h:98
void popBack()
removes the last element if exists.
Definition access_vector.h:455
parent_type::subject_type subject_type
Definition access_vector.h:104
TypeAccessVector & operator=(TypeAccessVector &&other)
move assignment operator
Definition access_vector.h:144
iterator begin()
the range based begin iterator
Definition access_vector.h:355
parent_type::event_code_type event_code_type
Definition access_vector.h:102
std::vector< value_type > container_type
Definition access_vector.h:90
container_type::iterator iterator
Definition access_vector.h:92
void clear()
clears the list and remove this as observer
Definition access_vector.h:441
TypeAccessVector(const TypeAccessVector &other)
copies (deepcopy!) CTOR
Definition access_vector.h:166
void remove(size_t index)
item to remove
Definition access_vector.h:295
iterator end()
the range based end iterator
Definition access_vector.h:365
std::shared_ptr< const DDL_TYPE_TO_ACCESS > get(size_t index) const
get the item with the given index
Definition access_vector.h:204
const_iterator end() const
range based end operator for const access
Definition access_vector.h:405
friend TYPE_VALIDATOR_CLASS
Definition access_vector.h:106
void emplace(DDL_TYPE_TO_ACCESS &&type_to_add)
emplace the given item
Definition access_vector.h:276
TypeAccessVector()=delete
no default CTOR!
const_iterator cbegin() const
const begin iterator access
Definition access_vector.h:375
std::shared_ptr< DDL_TYPE_TO_ACCESS > access(size_t index)
change access to an item
Definition access_vector.h:332
container_type::const_iterator const_iterator
Definition access_vector.h:94
void insert(const size_t pos_idx, const DDL_TYPE_TO_ACCESS &type_to_add)
inserts the given item at the given pos
Definition access_vector.h:242
observer_subject_type::observer_type observer_type
Definition access_vector.h:100
virtual ~TypeAccessVector()
DTOR.
Definition access_vector.h:128
void modelChanged(event_code_type event_code, subject_type &subject_changed, const std::string &additional_info)
overrides the observers utility function.
Definition access_vector.h:481
void add(const DDL_TYPE_TO_ACCESS &type_to_add)
adds the given item
Definition access_vector.h:219
Trigger access_type
Definition access_vector.h:86
bool operator!=(const TypeAccessVector &other) const
non equality operator.
Definition access_vector.h:432
TypeAccessVector(TypeAccessVector &&other)
move CTOR
Definition access_vector.h:135
TypeAccessVector & operator=(const TypeAccessVector &other)
copies (deepcopy!) and overwrite the current content.
Definition access_vector.h:183
TypeAccessVector(TYPE_VALIDATOR_CLASS *validator, const std::string &validation_info)
CTOR.
Definition access_vector.h:120
const_iterator begin() const
range based begin operator for const access
Definition access_vector.h:395
std::shared_ptr< Trigger > value_type
Definition access_vector.h:88
bool operator==(const TypeAccessVector &other) const
equality operator.
Definition access_vector.h:417
TypeAccessVectorObserver< Trigger > parent_type
Definition access_vector.h:96
definition of the utility namespace
Definition ddl.h:66
ModelObserverUtility< T, TypeAccessVectorEventCode > TypeAccessVectorObserver
helper template to observe vector content.
Definition access_vector.h:74
ModelSubjectUtility< T, TypeAccessVectorEventCode > TypeAccessVectorSubject
helper template to observe vector content.
Definition access_vector.h:65
TypeAccessVectorEventCode
Internal event code to inform the parent DD Object about the change of an item within the list.
Definition access_vector.h:39
definition of the ddl namespace
Definition codec.h:21