15 #ifndef DD_DD_ACCESS_LIST_H_INCLUDED
16 #define DD_DD_ACCESS_LIST_H_INCLUDED
25 #include <unordered_map>
27 #include <string_view>
78 template <
typename DDL_TYPE_TO_ACCESS,
typename TYPE_VALIDATOR_CLASS>
92 #ifndef dev_essential_TYPE_ACCESS_LIST_COMPATIBILITY
93 #if defined(__GNUC__) && (__GNUC__ > 7)
94 #define dev_essential_TYPE_ACCESS_LIST_COMPATIBILITY 0
95 #elif defined(_MSC_VER) && (_MSC_VER >= 1920)
96 #define dev_essential_TYPE_ACCESS_LIST_COMPATIBILITY 0
98 #define dev_essential_TYPE_ACCESS_LIST_COMPATIBILITY 1
103 #ifndef dev_essential_TYPE_ACCESS_LIST_COMPATIBILITY
104 #define dev_essential_TYPE_ACCESS_LIST_COMPATIBILITY 0
108 #if dev_essential_TYPE_ACCESS_LIST_COMPATIBILITY
111 #ifdef HAS_STRING_VIEW
118 typedef typename container_type::iterator
iterator;
163 *
this = std::move(other);
173 _validation_info = std::move(other._validation_info);
174 _types = std::move(other._types);
176 for (
const auto& current: _types) {
184 _validator =
nullptr;
194 : _validator(nullptr), _validation_info(other._validation_info)
196 for (
auto current: other._types) {
197 auto new_type = std::make_shared<DDL_TYPE_TO_ACCESS>(*current);
201 _types.push_back(new_type);
213 _validation_info = other._validation_info;
214 for (
auto current: other._types) {
215 auto new_type = std::make_shared<DDL_TYPE_TO_ACCESS>(*current);
219 _types.push_back(new_type);
221 _validator =
nullptr;
232 std::shared_ptr<const DDL_TYPE_TO_ACCESS>
get(
const std::string& type_name)
const
235 const auto named_items = getNamedContainer();
236 const auto found = named_items->find(type_name);
237 if (found != named_items->end()) {
238 return found->second;
242 for (
auto& current: _types) {
243 if (current->getName() == type_name) {
244 std::shared_ptr<const DDL_TYPE_TO_ACCESS> const_return = current;
261 size_t current_pos = 0;
262 for (
auto& current: _types) {
263 if (current->getName() == type_name) {
282 const auto named_items = getNamedContainer();
283 const auto found = named_items->find(type_name);
284 if (found != named_items->end()) {
289 for (
auto& current: _types) {
290 if (current->getName() == type_name) {
305 bool contains(
const DDL_TYPE_TO_ACCESS& type_to_find)
const
307 const auto value_found =
get(type_to_find.getName());
309 return type_to_find == *value_found;
323 for (
const auto& other_content: other._types) {
337 void add(
const DDL_TYPE_TO_ACCESS& type_to_add)
339 if (checkExistenceAndEquality(type_to_add)) {
345 auto new_type_value = std::make_shared<DDL_TYPE_TO_ACCESS>(type_to_add);
349 _types.push_back(new_type_value);
351 const auto named_items = getNamedContainer();
352 (*named_items)[new_type_value->getName()] = new_type_value;
355 _validator->notifyChangedListContent(
356 TypeAccessListEventCode::list_item_added, *new_type_value, type_to_add.getName());
368 void insert(
const size_t pos_idx,
const DDL_TYPE_TO_ACCESS& type_to_add)
372 _validation_info +
"::insert", {type_to_add.getName()},
"given pos_idx is invalid");
374 else if (pos_idx ==
getSize()) {
378 if (checkExistenceAndEquality(type_to_add)) {
383 auto new_type_value = std::make_shared<DDL_TYPE_TO_ACCESS>(type_to_add);
389 std::advance(cit, pos_idx);
390 _types.insert(cit, new_type_value);
392 const auto named_items = getNamedContainer();
393 (*named_items)[new_type_value->getName()] = new_type_value;
396 _validator->notifyChangedListContent(TypeAccessListEventCode::list_item_inserted,
398 type_to_add.getName());
409 void emplace(DDL_TYPE_TO_ACCESS&& type_to_add)
411 if (checkExistenceAndEquality(type_to_add)) {
416 auto new_type_value = std::make_shared<DDL_TYPE_TO_ACCESS>(std::move(type_to_add));
420 _types.push_back(new_type_value);
422 const auto named_items = getNamedContainer();
423 (*named_items)[new_type_value->getName()] = new_type_value;
426 _validator->notifyChangedListContent(TypeAccessListEventCode::list_item_added,
428 new_type_value->getName());
437 void remove(
const std::string& type_name)
440 typename container_type::iterator current_it;
441 for (current_it = _types.begin(); current_it != _types.end(); ++current_it) {
442 if ((*current_it)->getName() == type_name) {
443 removed_value = (*current_it);
448 _types.erase(current_it);
450 const auto named_items = getNamedContainer();
451 named_items->erase(type_name);
456 _validator->notifyChangedListContent(
457 TypeAccessListEventCode::list_item_removed, *removed_value, type_name);
462 _validation_info +
"::remove", {type_name},
"value with the given name does not exist");
471 std::shared_ptr<DDL_TYPE_TO_ACCESS>
access(
const std::string& type_name)
474 const auto named_items = getNamedContainer();
475 auto value_found = named_items->find(type_name);
476 if (value_found != named_items->end()) {
477 return value_found->second;
481 for (
const auto& current: _types) {
482 if (current->getName() == type_name) {
497 return _types.size();
507 return _types.begin();
527 return _types.cbegin();
537 return _types.cend();
569 return std::equal(
begin(),
end(), other.
begin(), [](
const auto& local,
const auto& other) {
570 return *(local) == *(other);
592 for (
auto& current: _types) {
598 const auto named_items = getNamedContainer();
599 named_items->clear();
610 if (!_types.empty()) {
611 auto last_element = _types[_types.size() - 1];
617 const auto named_items = getNamedContainer();
618 named_items->erase(last_element->getName());
622 _validator->notifyChangedListContent(TypeAccessListEventCode::list_item_popped,
624 last_element->getName());
628 throw ddl::dd::Error(_validation_info +
"::popBack", {},
"list is empty");
642 const std::string& additional_info)
644 if (event_code == list_item_renamed) {
645 bool already_exists =
false;
647 already_exists = _validator->validateContains(subject_changed);
650 already_exists =
countContains(subject_changed.getName()) > 1;
652 if (already_exists) {
654 _validation_info +
"::modelChanged",
655 {subject_changed.getName()},
656 "Renaming not possible. Value with the given name already exists");
660 const auto named_items = getNamedContainer();
661 auto found_in_names = named_items->begin();
662 for (; found_in_names != named_items->end(); ++found_in_names) {
664 if (found_in_names->second->getName() == subject_changed.getName()) {
665 auto old_value = found_in_names->second;
666 named_items->erase(found_in_names);
667 (*named_items)[old_value->getName()] = old_value;
676 _validator->notifyChangedListContent(event_code, subject_changed, additional_info);
692 destination.setValidator(validator);
703 return std::count_if(_types.begin(), _types.end(), [type_name](
const value_type& value) {
704 return value->getName() == type_name;
708 bool checkExistenceAndEquality(
const DDL_TYPE_TO_ACCESS& type_to_add)
711 bool already_exists =
false;
713 already_exists = _validator->validateContains(type_to_add);
716 already_exists =
contains(type_to_add.getName());
718 if (already_exists) {
720 const auto type_found =
get(type_to_add.getName());
724 if (*type_found == type_to_add) {
729 {type_to_add.getName()},
730 "value with the given name already exists");
734 {type_to_add.getName()},
735 "value with the given name already exists");
743 _validator = validator;
745 auto named_items = getNamedContainer(); named_items->clear();
746 for (
auto& value: _types) {
747 (*named_items)[value->getName()] = value;
756 return _validator->getNamedItemViewList();
763 return _validator->getNamedItemViewList();
768 std::string _validation_info;
The ModelObserver utility template.
MODEL_SUBJECT_T subject_type
local definition of the subject code type
EVENT_CODE_T event_code_type
local definition of the event code type
Model Subject utility to define a Model Subject that notifies one or more observers.
Utility class for observable named items where the order is important.
void add(const DDL_TYPE_TO_ACCESS &type_to_add)
adds the given item
virtual ~TypeAccessList()
DTOR.
bool operator!=(const TypeAccessList &other) const
non equality operator.
container_type::const_iterator const_iterator
local definition of the container const_iterator
const_iterator begin() const
range based begin operator for const access
void insert(const size_t pos_idx, const DDL_TYPE_TO_ACCESS &type_to_add)
inserts the given item at the given pos
const_iterator cbegin() const
const begin iterator access
container_type::iterator iterator
local definition of the container iterator
void popBack()
removes the last element if exists.
void remove(const std::string &type_name)
item to remove
void emplace(DDL_TYPE_TO_ACCESS &&type_to_add)
emplace the given item
TypeAccessList & operator=(const TypeAccessList &other)
copies (deepcopy!) and overwrite the current content.
TypeAccessList(TYPE_VALIDATOR_CLASS *validator, const std::string &validation_info)
CTOR.
bool contains(const std::string &type_name) const
determines is the type name exists in this list
std::shared_ptr< DDL_TYPE_TO_ACCESS > value_type
local definition of the value type
size_t countContains(const std::string &type_name) const
determines the count of the type name exists in this list
parent_type::event_code_type event_code_type
local definition of the eventcode type
map_subject_type::observer_type observer_type
local definition of the observer type
void modelChanged(event_code_type event_code, subject_type &subject_changed, const std::string &additional_info)
overrides the observers utility function.
std::vector< value_type > container_type
local definition of the container type
OptionalSize getPosOf(const std::string &type_name) const
Get the Pos Of the item with the name type_name.
bool contains(const TypeAccessList &other) const
determines if the other is a subset of this list
const_iterator cend() const
const end iterator access
TypeAccessList(const TypeAccessList &other)
copies (deepcopy!) CTOR
size_t getSize() const
Get the Size.
TypeAccessList()=delete
no default CTOR!
bool operator==(const TypeAccessList &other) const
equality operator.
bool contains(const DDL_TYPE_TO_ACCESS &type_to_find) const
determines if the item is in this list.
DDL_TYPE_TO_ACCESS access_type
local definition of the access type
std::shared_ptr< const DDL_TYPE_TO_ACCESS > get(const std::string &type_name) const
get the item with the given name type_name
friend TYPE_VALIDATOR_CLASS
friend validator class
std::shared_ptr< DDL_TYPE_TO_ACCESS > access(const std::string &type_name)
change access to an item
TypeAccessList(TypeAccessList &&other)
move CTOR
TypeAccessListSubject< DDL_TYPE_TO_ACCESS > map_subject_type
local definition of the internal subject type
void deepCopy(TypeAccessList &destination, TYPE_VALIDATOR_CLASS *validator) const
copies the content of the of the list (by reseting the observer) and resets the validator to the give...
void clear()
clears the list and remove this as observer
std::unordered_map< std::string, value_type > container_named_compatibility_type
local definition of the container type for getNamedItemList, which was had incompatible changes withi...
iterator end()
the range based end iterator
const_iterator end() const
range based end operator for const access
TypeAccessListObserver< DDL_TYPE_TO_ACCESS > parent_type
local definition of the parent type to register observer
std::unordered_map< std::string, value_type > container_named_type
local definition of the container type for getNamedItemViewList
parent_type::subject_type subject_type
local definition of the subject type
iterator begin()
the range based begin iterator
TypeAccessList & operator=(TypeAccessList &&other)
move assignment operator
DataDefinition Model-Observer pattern utility.
OO DataDefinition Common Design.
OO DataDefinition Redesign.
Error
Enumerates possible error values for filesystem interaction.
TypeAccessListEventCode
Internal event code to inform the parent DD Object about the change of an item within the list.
@ validation_info
Validation Info.
definition of the ddl namespace
Checks for std::string_view and defines HAS_STRING_VIEW accordingly.