Dev Essential  1.8.1
Loading...
Searching...
No Matches
access_list.h
Go to the documentation of this file.
1
14
15#ifndef DDL_UTILITY_ACCESS_LIST_H_INCLUDED
16#define DDL_UTILITY_ACCESS_LIST_H_INCLUDED
17
22
23#include <algorithm>
24#include <cstddef>
25#include <memory>
26#include <string>
27#include <string_view>
28#include <unordered_map>
29
30namespace ddl {
31namespace utility {
32 // forward declaration
33 template <typename DDL_TYPE_TO_ACCESS, typename TYPE_VALIDATOR_CLASS>
34 class TypeAccessList;
35}
36namespace dd {
37namespace utility {
46 list_item_added,
47 list_item_removed,
48 list_item_changed,
49 list_item_renamed,
50 list_subitem_added,
51 list_subitem_removed,
52 list_subitem_changed,
53 list_item_popped,
54 list_subitem_popped,
55 list_item_inserted,
56 list_subitem_inserted,
57 list_subitem_renamed,
58 list_item_removing,
59 list_subitem_removing,
60 list_item_renaming,
61 list_subitem_renaming
62};
63
70template <typename T>
72
79template <typename T>
81
88template <typename DDL_TYPE_TO_ACCESS, typename TYPE_VALIDATOR_CLASS>
89class TypeAccessList : public TypeAccessListObserver<DDL_TYPE_TO_ACCESS> {
90public:
92 typedef DDL_TYPE_TO_ACCESS access_type;
94 typedef std::shared_ptr<DDL_TYPE_TO_ACCESS> value_type;
96 typedef std::vector<value_type> container_type;
98 typedef std::unordered_map<std::string_view, value_type> container_named_type;
100 typedef typename container_type::iterator iterator;
102 typedef typename container_type::const_iterator const_iterator;
115
116public:
121 TypeAccessList() = delete;
128 TypeAccessList(TYPE_VALIDATOR_CLASS* validator, std::string validation_info) noexcept
129 : _validator(validator), _validation_info(std::move(validation_info))
130 {
131 }
132
137 {
138 clear();
139 }
140
144 {
145 *this = std::move(other);
146 }
147
153 {
154 clear();
155 _validation_info = std::move(other._validation_info);
156 _types = std::move(other._types);
157 _named_types = std::move(other._named_types);
158 other._named_types.clear();
159 // the other will not observe this anymore after this clear
160 for (const auto& current: _types) {
161 // the other will not observe this anymore
162 (static_cast<map_subject_type*>(current.get()))
163 ->detachObserver(static_cast<observer_type*>(&other));
164 // i want to observe this
165 (static_cast<map_subject_type*>(current.get()))
166 ->attachObserver(static_cast<observer_type*>(this));
167 }
168 return *this;
169 }
170
177 : _validator(nullptr), _validation_info(other._validation_info)
178 {
179 for (auto current: other._types) {
180 auto new_type = std::make_shared<DDL_TYPE_TO_ACCESS>(*current);
181 // i want to observe this
182 (static_cast<map_subject_type*>(new_type.get()))
183 ->attachObserver(static_cast<observer_type*>(this));
184 _types.push_back(new_type);
185 _named_types.emplace(new_type->getName(), new_type);
186 }
187 }
188
195 {
196 if (this == &other) {
197 return *this;
198 }
199 clear();
200 _validation_info = other._validation_info;
201 for (auto current: other._types) {
202 auto new_type = std::make_shared<DDL_TYPE_TO_ACCESS>(*current);
203 // i want to observe this
204 (static_cast<map_subject_type*>(new_type.get()))
205 ->attachObserver(static_cast<observer_type*>(this));
206 _types.push_back(new_type);
207 _named_types.emplace(new_type->getName(), new_type);
208 }
209 _validator = nullptr;
210 return *this;
211 }
212
213public:
220 std::shared_ptr<const DDL_TYPE_TO_ACCESS> get(std::string_view type_name) const noexcept
221 {
222 const auto found = _named_types.find(type_name);
223 if (found != _named_types.end()) {
224 return found->second;
225 }
226 return {};
227 }
228
236 Optional<size_t> getPosOf(const std::string& type_name) const
237 {
238 size_t current_pos = 0;
239 for (auto& current: _types) {
240 if (current->getName() == type_name) {
241 return current_pos;
242 }
243 ++current_pos;
244 }
245
246 return {};
247 }
248
256 bool contains(const std::string& type_name) const noexcept
257 {
258 return _named_types.find(type_name) != _named_types.end();
259 }
260
268 bool contains(const DDL_TYPE_TO_ACCESS& type_to_find) const
269 {
270 const auto value_found = get(type_to_find.getName());
271 if (value_found) {
272 return type_to_find == *value_found;
273 }
274 return false;
275 }
276
284 bool contains(const TypeAccessList& other) const
285 {
286 for (const auto& other_content: other._types) {
287 if (!contains(*other_content)) {
288 return false;
289 }
290 }
291 return true;
292 }
293
301 std::shared_ptr<DDL_TYPE_TO_ACCESS> create(const DDL_TYPE_TO_ACCESS& type_to_add)
302 {
303 if (checkExistenceAndEquality(type_to_add)) {
304 // this will immediatelly return if the type exists and is equal
305 // otherwise the check will throw!
306 return access(type_to_add.getName());
307 }
308
309 return appendNew(std::make_shared<DDL_TYPE_TO_ACCESS>(type_to_add));
310 }
311
318 void add(const DDL_TYPE_TO_ACCESS& type_to_add)
319 {
320 create(type_to_add);
321 }
322
331 void insert(const size_t pos_idx, const DDL_TYPE_TO_ACCESS& type_to_add)
332 {
333 if (pos_idx > getSize()) {
334 throw ddl::dd::Error(
335 _validation_info + "::insert", {type_to_add.getName()}, "given pos_idx is invalid");
336 }
337 else if (pos_idx == getSize()) {
338 add(type_to_add);
339 }
340 else {
341 if (checkExistenceAndEquality(type_to_add)) {
342 // this will immediatelly return if the type exists and is equal
343 // otherwise the check will throw!
344 return;
345 }
346 auto new_type_value = std::make_shared<DDL_TYPE_TO_ACCESS>(type_to_add);
347 // i want to observe this
348 (static_cast<map_subject_type*>(new_type_value.get()))
349 ->attachObserver(static_cast<observer_type*>(this));
350
351 auto cit = cbegin();
352 std::advance(cit, pos_idx);
353 _types.insert(cit, new_type_value);
354 _named_types.insert_or_assign(new_type_value->getName(), new_type_value);
355 if (_validator) {
356 _validator->notifyChangedListContent(TypeAccessListEventCode::list_item_inserted,
357 *new_type_value,
358 type_to_add.getName());
359 }
360 }
361 }
362
370 std::shared_ptr<DDL_TYPE_TO_ACCESS> create(DDL_TYPE_TO_ACCESS&& type_to_add)
371 {
372 if (checkExistenceAndEquality(type_to_add)) {
373 // this will immediatelly return if the type exists and is equal
374 // otherwise the check will throw!
375 return access(type_to_add.getName());
376 }
377 return appendNew(std::make_shared<DDL_TYPE_TO_ACCESS>(std::move(type_to_add)));
378 }
379
390 std::shared_ptr<DDL_TYPE_TO_ACCESS> create(std::shared_ptr<DDL_TYPE_TO_ACCESS> type_to_add)
391 {
392 if (!type_to_add) {
393 throw ddl::dd::Error(_validation_info + "::create", {}, "item to add is null");
394 }
395 if (checkExistenceAndEquality(*type_to_add)) {
396 // An equal item exists already, an item that differs would have made the check
397 // throw.
398 return access(type_to_add->getName());
399 }
400 return appendNew(std::move(type_to_add));
401 }
402
409 void emplace(DDL_TYPE_TO_ACCESS&& type_to_add)
410 {
411 create(std::move(type_to_add));
412 }
413
419 void remove(const std::string& type_name)
420 {
421 value_type removed_value;
422 typename container_type::iterator current_it;
423 for (current_it = _types.begin(); current_it != _types.end(); ++current_it) {
424 if ((*current_it)->getName() == type_name) {
425 removed_value = (*current_it);
426 break;
427 }
428 }
429 if (removed_value) {
430 if (_validator) {
431 _validator->notifyChangedListContent(
432 TypeAccessListEventCode::list_item_removing, *removed_value, type_name);
433 }
434 _types.erase(current_it);
435 _named_types.erase(type_name);
436 (static_cast<subject_type*>(removed_value.get()))
437 ->detachObserver(static_cast<observer_type*>(this));
438 if (_validator) {
439 _validator->notifyChangedListContent(
440 TypeAccessListEventCode::list_item_removed, *removed_value, type_name);
441 }
442 return;
443 }
444 throw ddl::dd::Error(
445 _validation_info + "::remove", {type_name}, "value with the given name does not exist");
446 }
447
454 std::shared_ptr<DDL_TYPE_TO_ACCESS> access(const std::string& type_name) noexcept
455 {
456 const auto found = _named_types.find(type_name);
457 if (found != _named_types.end()) {
458 return found->second;
459 }
460 return {};
461 }
462
468 size_t getSize() const
469 {
470 return _types.size();
471 }
472
479 void reserve(size_t count) noexcept
480 {
481 _types.reserve(count);
482 _named_types.reserve(count);
483 }
484
491 {
492 return _types.begin();
493 }
494
501 {
502 return _types.end();
503 }
504
511 {
512 return _types.cbegin();
513 }
514
521 {
522 return _types.cend();
523 }
524
531 {
532 return cbegin();
533 }
534
541 {
542 return cend();
543 }
544
552 bool operator==(const TypeAccessList& other) const
553 {
554 return std::equal(
555 begin(), end(), other.begin(), other.end(), [](const auto& local, const auto& other) {
556 return *(local) == *(other);
557 });
558 }
559
567 bool operator!=(const TypeAccessList& other) const
568 {
569 return !operator==(other);
570 }
571
576 void clear() noexcept
577 {
578 for (auto& current: _types) {
579 // unregister me as observer
580 (static_cast<subject_type*>(current.get()))
581 ->detachObserver(static_cast<observer_type*>(this));
582 }
583 _named_types.clear();
584 _types.clear();
585 }
586
591 void popBack()
592 {
593 if (!_types.empty()) {
594 auto last_element = _types[_types.size() - 1];
595 if (_validator) {
596 _validator->notifyChangedListContent(TypeAccessListEventCode::list_item_removing,
597 *last_element,
598 last_element->getName());
599 }
600 // unregister me as observer
601 (static_cast<subject_type*>(last_element.get()))
602 ->detachObserver(static_cast<observer_type*>(this));
603 // pop last element
604 _named_types.erase(last_element->getName());
605 _types.pop_back();
606 if (_validator) {
607 _validator->notifyChangedListContent(TypeAccessListEventCode::list_item_popped,
608 *last_element,
609 last_element->getName());
610 }
611 }
612 else {
613 throw ddl::dd::Error(_validation_info + "::popBack", {}, "list is empty");
614 }
615 }
616
617public:
626 subject_type& subject_changed,
627 const std::string& additional_info)
628 {
629 if (event_code == list_item_renaming) {
630 // the validator can not be asked for the renaming here, (because of ABI compatibility, the validateContains can not be changed!)
631 // if you need to have unique names depending on other data structures use the TypeAccessMap instead of TypeAccessList!
632 if (countContains(additional_info) != 0) {
633 throw ddl::dd::Error(
634 _validation_info + "::modelChanged",
635 {additional_info},
636 "Renaming not possible. Value with the given name already exists");
637 }
638 }
639 else if (event_code == list_item_renamed) {
640 for (const auto& type: _types) {
641 if (type->getName() == subject_changed.getName()) {
642 _named_types.emplace(type->getName(), type);
643 break;
644 }
645 }
646 }
647 if (_validator) {
648 // i should discover the shared_ptr here, but not needed at the moment
649 _validator->notifyChangedListContent(event_code, subject_changed, additional_info);
650 }
651 if (event_code == list_item_renaming) {
652 // The validator accepted the renaming, the item is re-added after renaming.
653 _named_types.erase(subject_changed.getName());
654 }
655 }
656
657private:
658 // forward declaration
659 friend class ::ddl::utility::TypeAccessList<DDL_TYPE_TO_ACCESS,
669 void deepCopy(TypeAccessList& destination, TYPE_VALIDATOR_CLASS* validator) const
670 {
671 destination = *this;
672 destination.setValidator(validator);
673 }
681 size_t countContains(const std::string& type_name) const
682 {
683 return std::count_if(_types.begin(), _types.end(), [type_name](const value_type& value) {
684 return value->getName() == type_name;
685 });
686 }
687
688 std::shared_ptr<DDL_TYPE_TO_ACCESS> appendNew(
689 std::shared_ptr<DDL_TYPE_TO_ACCESS> new_type_value)
690 {
691 // I want to observe this.
692 (static_cast<map_subject_type*>(new_type_value.get()))
693 ->attachObserver(static_cast<observer_type*>(this));
694 _types.push_back(new_type_value);
695 _named_types.insert_or_assign(new_type_value->getName(), new_type_value);
696 if (_validator) {
697 _validator->notifyChangedListContent(TypeAccessListEventCode::list_item_added,
698 *new_type_value,
699 new_type_value->getName());
700 }
701 return new_type_value;
702 }
703
704 bool checkExistenceAndEquality(const DDL_TYPE_TO_ACCESS& type_to_add)
705 {
706 // has already another type
707 bool already_exists = false;
708 if (_validator) {
709 already_exists = _validator->validateContains(type_to_add);
710 }
711 else {
712 already_exists = contains(type_to_add.getName());
713 }
714 if (already_exists) {
715 // have a look if the type does exist in this list
716 const auto type_found = get(type_to_add.getName());
717 // have a look if the type already exists here
718 if (type_found) {
719 // check if they are equal
720 if (*type_found == type_to_add) {
721 // its okay ... its the same type/content
722 return true;
723 }
724 throw ddl::dd::Error(
725 _validation_info + "::create",
726 {type_to_add.getName()},
727 "value with the given name already exists and differs in " +
729 type_to_add));
730 }
731 else {
732 throw ddl::dd::Error(
733 _validation_info + "::create",
734 {type_to_add.getName()},
735 std::string("value with the given name already exists with another type of ") +
737 }
738 }
739 return false;
740 }
741
742 void setValidator(TYPE_VALIDATOR_CLASS* validator) noexcept
743 {
744 _validator = validator;
745 }
746
747 container_type _types;
749 container_named_type _named_types;
750 TYPE_VALIDATOR_CLASS* _validator = {};
752 std::string _validation_info;
753};
754
755} // namespace utility
756} // namespace dd
757
758namespace utility {
759
760// (usually these enums and templates should be moved to utility namespace, but we stay binary
761// compatible for now!)
763
770template <typename T>
772
779template <typename T>
781
788template <typename DDL_TYPE_TO_ACCESS, typename TYPE_VALIDATOR_CLASS>
791 DDL_TYPE_TO_ACCESS,
792 ::ddl::utility::TypeAccessList<DDL_TYPE_TO_ACCESS, TYPE_VALIDATOR_CLASS>> {
793public:
798 DDL_TYPE_TO_ACCESS,
802 friend base_type;
804
808 TypeAccessList() = delete;
814 TypeAccessList(TYPE_VALIDATOR_CLASS* validator, std::string validation_info) noexcept
815 : base_type(this, std::move(validation_info)), _validator(validator)
816 {
817 }
818
822 {
823 base_type::_validator = {};
824 }
825
829 TypeAccessList(TypeAccessList&& other) = default;
836
841 TypeAccessList(const TypeAccessList& other) : base_type(this, other._validation_info)
842 {
843 other.deepCopy(*this, this);
844 }
845
852 {
853 other.deepCopy(*this, this);
854 return *this;
855 }
856
857private:
858 bool validateContains(const typename base_type::access_type& subject) const
859 {
860 if (_validator) {
861 return _validator->validateContains(subject);
862 }
863 return false;
864 }
865
866 void notifyChangedListContent(typename base_type::event_code_type code,
867 typename base_type::access_type& subject,
868 const std::string& additional_info)
869 {
870 if (_validator) {
871 return _validator->notifyChangedListContent(code, subject, additional_info);
872 }
873 }
874
875 TYPE_VALIDATOR_CLASS* _validator = {};
876};
877
878} // namespace utility
879} // namespace ddl
880
881#endif // DDL_UTILITY_ACCESS_LIST_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
TypeAccessListEventCode event_code_type
Definition access_observer.h:37
Model Subject utility to define a Model Subject that notifies one or more observers.
Definition access_observer.h:68
ModelObserverUtility< T, TypeAccessListEventCode > observer_type
Definition access_observer.h:75
Utility class for observable named items where the order is important.
Definition access_list.h:89
std::shared_ptr< Struct > value_type
Definition access_list.h:94
iterator end()
the range based end iterator
Definition access_list.h:500
const_iterator end() const
range based end operator for const access
Definition access_list.h:540
parent_type::event_code_type event_code_type
Definition access_list.h:110
bool operator!=(const TypeAccessList &other) const
non equality operator.
Definition access_list.h:567
Struct access_type
Definition access_list.h:92
void add(const DDL_TYPE_TO_ACCESS &type_to_add)
creates the given item by copy
Definition access_list.h:318
TypeAccessList(TypeAccessList &&other) noexcept
move CTOR
Definition access_list.h:143
TypeAccessListSubject< Struct > map_subject_type
Definition access_list.h:106
TypeAccessList & operator=(TypeAccessList &&other) noexcept
move assignment operator
Definition access_list.h:152
size_t getSize() const
Get the Size.
Definition access_list.h:468
TypeAccessList()=delete
no default CTOR!
bool contains(const TypeAccessList &other) const
determines if the other is a subset of this list
Definition access_list.h:284
TypeAccessListObserver< Struct > parent_type
Definition access_list.h:104
std::shared_ptr< DDL_TYPE_TO_ACCESS > create(DDL_TYPE_TO_ACCESS &&type_to_add)
create the given item by emplace
Definition access_list.h:370
map_subject_type::observer_type observer_type
Definition access_list.h:108
void reserve(size_t count) noexcept
Reserve storage for at least count items, e.g. before adding a known number of items one by one.
Definition access_list.h:479
bool contains(const DDL_TYPE_TO_ACCESS &type_to_find) const
determines if the item is in this list.
Definition access_list.h:268
void emplace(DDL_TYPE_TO_ACCESS &&type_to_add)
emplace the given item
Definition access_list.h:409
container_type::iterator iterator
Definition access_list.h:100
void popBack()
removes the last element if exists.
Definition access_list.h:591
std::unordered_map< std::string_view, value_type > container_named_type
Definition access_list.h:98
TypeAccessList(TYPE_VALIDATOR_CLASS *validator, std::string validation_info) noexcept
CTOR.
Definition access_list.h:128
void clear() noexcept
clears the list and remove this as observer
Definition access_list.h:576
std::shared_ptr< DDL_TYPE_TO_ACCESS > access(const std::string &type_name) noexcept
change access to an item
Definition access_list.h:454
const_iterator begin() const
range based begin operator for const access
Definition access_list.h:530
const_iterator cbegin() const
const begin iterator access
Definition access_list.h:510
const_iterator cend() const
const end iterator access
Definition access_list.h:520
parent_type::subject_type subject_type
Definition access_list.h:112
bool contains(const std::string &type_name) const noexcept
determines is the type name exists in this list
Definition access_list.h:256
bool operator==(const TypeAccessList &other) const
equality operator.
Definition access_list.h:552
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_list.h:331
friend TYPE_VALIDATOR_CLASS
Definition access_list.h:114
virtual ~TypeAccessList()
DTOR.
Definition access_list.h:136
Optional< size_t > getPosOf(const std::string &type_name) const
Get the Pos Of the item with the name type_name.
Definition access_list.h:236
void remove(const std::string &type_name)
item to remove
Definition access_list.h:419
std::shared_ptr< DDL_TYPE_TO_ACCESS > create(std::shared_ptr< DDL_TYPE_TO_ACCESS > type_to_add)
Creates the item from an already allocated instance, which avoids copying or moving it into the list.
Definition access_list.h:390
std::shared_ptr< DDL_TYPE_TO_ACCESS > create(const DDL_TYPE_TO_ACCESS &type_to_add)
creates the given item by copy
Definition access_list.h:301
TypeAccessList(const TypeAccessList &other)
copies (deepcopy!) CTOR
Definition access_list.h:176
std::vector< value_type > container_type
Definition access_list.h:96
std::shared_ptr< const DDL_TYPE_TO_ACCESS > get(std::string_view type_name) const noexcept
get the item with the given name type_name
Definition access_list.h:220
iterator begin()
the range based begin iterator
Definition access_list.h:490
container_type::const_iterator const_iterator
Definition access_list.h:102
TypeAccessList & operator=(const TypeAccessList &other)
copies (deepcopy!) and overwrite the current content.
Definition access_list.h:194
void modelChanged(event_code_type event_code, subject_type &subject_changed, const std::string &additional_info)
overrides the observers utility function.
Definition access_list.h:625
ddl::dd::utility::TypeAccessList that forwards the validation to a validator of type TYPE_VALIDATOR_C...
Definition access_list.h:792
TypeAccessList(const TypeAccessList &other)
copies (deepcopy!) CTOR
Definition access_list.h:841
TypeAccessList(TYPE_VALIDATOR_CLASS *validator, std::string validation_info) noexcept
CTOR.
Definition access_list.h:814
TypeAccessList()=delete
no default CTOR!
TypeAccessList(TypeAccessList &&other)=default
move CTOR
::ddl::dd::utility::TypeAccessList< ParameterValue, ::ddl::utility::TypeAccessList< ParameterValue, Assignment > > base_type
Definition access_list.h:797
virtual ~TypeAccessList()
DTOR.
Definition access_list.h:821
TypeAccessList & operator=(TypeAccessList &&other)=default
move assignment operator
TypeAccessList & operator=(const TypeAccessList &other)
copies (deepcopy!) and overwrite the current content.
Definition access_list.h:851
definition of the old compatibility utility namespace
Definition datamodel_keyvalue.h:160
ModelObserverUtility< T, TypeAccessListEventCode > TypeAccessListObserver
helper template to observe list content.
Definition access_list.h:80
ModelSubjectUtility< T, TypeAccessListEventCode > TypeAccessListSubject
helper template to observe list content.
Definition access_list.h:71
TypeAccessListEventCode
Internal event code to inform the parent DD Object about the change of an item within the list.
Definition access_list.h:45
definition of the dd namespace
Definition datamodel_base.h:26
@ validation_info
Validation Info.
Definition dd_infomodel_type.h:32
definition of the utility namespace
Definition ddl.h:66
ModelSubjectUtility< T, TypeAccessListEventCode > TypeAccessListSubject
helper template to observe list content.
Definition access_list.h:771
ModelObserverUtility< T, TypeAccessListEventCode > TypeAccessListObserver
helper template to observe list content.
Definition access_list.h:780
definition of the ddl namespace
Definition codec.h:21
An optional template as long as the std::optional is not available here.
Definition access_optional.h:31
static constexpr auto _other_types
validation other types
Definition access_validation.h:41
static std::string getDifference(const T &, const T &)
validation difference message
Definition access_validation.h:45