15#ifndef DDL_UTILITY_ACCESS_LIST_H_INCLUDED
16#define DDL_UTILITY_ACCESS_LIST_H_INCLUDED
28#include <unordered_map>
33 template <
typename DDL_TYPE_TO_ACCESS,
typename TYPE_VALIDATOR_CLASS>
56 list_subitem_inserted,
59 list_subitem_removing,
88template <
typename DDL_TYPE_TO_ACCESS,
typename TYPE_VALIDATOR_CLASS>
100 typedef typename container_type::iterator
iterator;
145 *
this = std::move(other);
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();
160 for (
const auto& current: _types) {
177 : _validator(nullptr), _validation_info(other._validation_info)
179 for (
auto current: other._types) {
180 auto new_type = std::make_shared<DDL_TYPE_TO_ACCESS>(*current);
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);
196 if (
this == &other) {
200 _validation_info = other._validation_info;
201 for (
auto current: other._types) {
202 auto new_type = std::make_shared<DDL_TYPE_TO_ACCESS>(*current);
206 _types.push_back(new_type);
207 _named_types.emplace(new_type->getName(), new_type);
209 _validator =
nullptr;
220 std::shared_ptr<const DDL_TYPE_TO_ACCESS>
get(std::string_view type_name)
const noexcept
222 const auto found = _named_types.find(type_name);
223 if (found != _named_types.end()) {
224 return found->second;
238 size_t current_pos = 0;
239 for (
auto& current: _types) {
240 if (current->getName() == type_name) {
256 bool contains(
const std::string& type_name)
const noexcept
258 return _named_types.find(type_name) != _named_types.end();
268 bool contains(
const DDL_TYPE_TO_ACCESS& type_to_find)
const
270 const auto value_found =
get(type_to_find.getName());
272 return type_to_find == *value_found;
286 for (
const auto& other_content: other._types) {
301 std::shared_ptr<DDL_TYPE_TO_ACCESS>
create(
const DDL_TYPE_TO_ACCESS& type_to_add)
303 if (checkExistenceAndEquality(type_to_add)) {
306 return access(type_to_add.getName());
309 return appendNew(std::make_shared<DDL_TYPE_TO_ACCESS>(type_to_add));
318 void add(
const DDL_TYPE_TO_ACCESS& type_to_add)
331 void insert(
const size_t pos_idx,
const DDL_TYPE_TO_ACCESS& type_to_add)
335 _validation_info +
"::insert", {type_to_add.getName()},
"given pos_idx is invalid");
337 else if (pos_idx ==
getSize()) {
341 if (checkExistenceAndEquality(type_to_add)) {
346 auto new_type_value = std::make_shared<DDL_TYPE_TO_ACCESS>(type_to_add);
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);
356 _validator->notifyChangedListContent(TypeAccessListEventCode::list_item_inserted,
358 type_to_add.getName());
370 std::shared_ptr<DDL_TYPE_TO_ACCESS>
create(DDL_TYPE_TO_ACCESS&& type_to_add)
372 if (checkExistenceAndEquality(type_to_add)) {
375 return access(type_to_add.getName());
377 return appendNew(std::make_shared<DDL_TYPE_TO_ACCESS>(std::move(type_to_add)));
390 std::shared_ptr<DDL_TYPE_TO_ACCESS>
create(std::shared_ptr<DDL_TYPE_TO_ACCESS> type_to_add)
393 throw ddl::dd::Error(_validation_info +
"::create", {},
"item to add is null");
395 if (checkExistenceAndEquality(*type_to_add)) {
398 return access(type_to_add->getName());
400 return appendNew(std::move(type_to_add));
409 void emplace(DDL_TYPE_TO_ACCESS&& type_to_add)
411 create(std::move(type_to_add));
419 void remove(
const std::string& type_name)
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);
431 _validator->notifyChangedListContent(
432 TypeAccessListEventCode::list_item_removing, *removed_value, type_name);
434 _types.erase(current_it);
435 _named_types.erase(type_name);
439 _validator->notifyChangedListContent(
440 TypeAccessListEventCode::list_item_removed, *removed_value, type_name);
445 _validation_info +
"::remove", {type_name},
"value with the given name does not exist");
454 std::shared_ptr<DDL_TYPE_TO_ACCESS>
access(
const std::string& type_name)
noexcept
456 const auto found = _named_types.find(type_name);
457 if (found != _named_types.end()) {
458 return found->second;
470 return _types.size();
481 _types.reserve(count);
482 _named_types.reserve(count);
492 return _types.begin();
512 return _types.cbegin();
522 return _types.cend();
555 begin(),
end(), other.
begin(), other.
end(), [](
const auto& local,
const auto& other) {
556 return *(local) == *(other);
578 for (
auto& current: _types) {
583 _named_types.clear();
593 if (!_types.empty()) {
594 auto last_element = _types[_types.size() - 1];
596 _validator->notifyChangedListContent(TypeAccessListEventCode::list_item_removing,
598 last_element->getName());
604 _named_types.erase(last_element->getName());
607 _validator->notifyChangedListContent(TypeAccessListEventCode::list_item_popped,
609 last_element->getName());
613 throw ddl::dd::Error(_validation_info +
"::popBack", {},
"list is empty");
627 const std::string& additional_info)
629 if (event_code == list_item_renaming) {
632 if (countContains(additional_info) != 0) {
634 _validation_info +
"::modelChanged",
636 "Renaming not possible. Value with the given name already exists");
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);
649 _validator->notifyChangedListContent(event_code, subject_changed, additional_info);
651 if (event_code == list_item_renaming) {
653 _named_types.erase(subject_changed.getName());
659 friend class ::ddl::utility::TypeAccessList<DDL_TYPE_TO_ACCESS,
672 destination.setValidator(validator);
681 size_t countContains(
const std::string& type_name)
const
683 return std::count_if(_types.begin(), _types.end(), [type_name](
const value_type& value) {
684 return value->getName() == type_name;
688 std::shared_ptr<DDL_TYPE_TO_ACCESS> appendNew(
689 std::shared_ptr<DDL_TYPE_TO_ACCESS> new_type_value)
694 _types.push_back(new_type_value);
695 _named_types.insert_or_assign(new_type_value->getName(), new_type_value);
697 _validator->notifyChangedListContent(TypeAccessListEventCode::list_item_added,
699 new_type_value->getName());
701 return new_type_value;
704 bool checkExistenceAndEquality(
const DDL_TYPE_TO_ACCESS& type_to_add)
707 bool already_exists =
false;
709 already_exists = _validator->validateContains(type_to_add);
712 already_exists =
contains(type_to_add.getName());
714 if (already_exists) {
716 const auto type_found =
get(type_to_add.getName());
720 if (*type_found == type_to_add) {
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 " +
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 ") +
744 _validator = validator;
752 std::string _validation_info;
788template <
typename DDL_TYPE_TO_ACCESS,
typename TYPE_VALIDATOR_CLASS>
792 ::ddl::utility::TypeAccessList<DDL_TYPE_TO_ACCESS, TYPE_VALIDATOR_CLASS>> {
815 :
base_type(
this, std::move(validation_info)), _validator(validator)
823 base_type::_validator = {};
843 other.deepCopy(*
this,
this);
853 other.deepCopy(*
this,
this);
861 return _validator->validateContains(subject);
868 const std::string& additional_info)
871 return _validator->notifyChangedListContent(code, subject, additional_info);
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
T subject_type
Definition access_observer.h:39
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