52 template <
typename... Args>
55 ,
item{T{std::forward<Args>(args)...}}
81 constexpr
static auto invalidId = std::numeric_limits<IdType>::max();
90 template <
typename WrappedIterator>
150 return !(lhs == rhs);
156 for (std::size_t i = 0; i < offset && tmp !=
endIterator_; ++i)
171 for (std::size_t i = 0; i < offset && *
this !=
endIterator_; ++i)
177 for (std::size_t i = 0; i < offset && *
this !=
beginIterator_; ++i)
192 template <
typename WrappedIterator>
207 throw std::runtime_error{
"Dereferencing end iterator"};
214 throw std::runtime_error{
"Dereferencing end iterator"};
219 template <
typename WrappedIterator>
230 throw std::runtime_error{
"Dereferencing end iterator"};
237 throw std::runtime_error{
"Dereferencing end iterator"};
261 items_.push_back(
ItemWithId{id_, std::optional<T>{element}});
278 items_.push_back(
ItemWithId{id_, std::optional<T>{std::move(element)}});
294 template <
typename... Args>
314 const auto p = findItem(
id);
315 if (p == std::end(items_))
321 auto result = condense();
322 return {result, items_.begin(), items_.end()};
333 const auto p = findItem(
id);
334 if (p == std::end(items_))
337 const auto result = std::move(p->item);
365 const auto iter = findItem(
id);
366 if (iter == std::end(items_))
367 return {
nullptr,
false,
false};
368 if (!iter->item.has_value())
369 return {
nullptr,
true,
true};
373 const auto selectedIter = selected_.insert(std::move(*iter)).first;
380 .alreadySelected =
false,
390 std::size_t
deselectAll(std::invocable<ItemWithId const&>
auto const& callback)
392 std::size_t result = 0;
393 for (
auto const& selected : selected_)
395 auto const id = selected.id;
396 if (callback(selected))
398 auto entry = findItem(
id);
399 if (entry != std::end(items_))
403 entry->item = std::move(
const_cast<ItemWithId&
>(selected).item);
422 auto const iter = selected_.find(
ItemWithId{id, std::nullopt});
423 if (iter == std::end(selected_))
428 auto entry = findItem(
id);
429 if (entry != std::end(items_))
436 selected_.erase(iter);
437 if (selected_.empty())
458 auto iter = findItem(
id);
459 if (iter == std::end(items_))
461 return IteratorType{iter, items_.begin(), items_.end()};
472 auto iter = findItem(
id);
473 if (iter == std::end(items_))
505 return {items_.begin(), items_.begin(), items_.end()};
513 return {items_.begin(), items_.begin(), items_.end()};
521 return {items_.cbegin(), items_.begin(), items_.end()};
529 return {items_.end(), items_.begin(), items_.end()};
537 return {items_.end(), items_.begin(), items_.end()};
545 return {items_.cend(), items_.begin(), items_.end()};
553 return itemCount_ == 0;
569 return items_.begin();
575 typename ItemContainerType::iterator
rawEnd()
583 typename ItemContainerType::const_iterator
rawBegin()
const
585 return items_.begin();
593 return items_.cbegin();
599 typename ItemContainerType::const_iterator
rawEnd()
const
609 return items_.cend();
612 template <
typename RegistryPtr>
616 typename ItemContainerType::iterator
begin()
const
620 typename ItemContainerType::iterator
end()
const
624 typename ItemContainerType::iterator
cbegin()
const
628 typename ItemContainerType::iterator
cend()
const
655 typename ItemContainerType::iterator findItem(
IdType id)
658 std::lower_bound(std::begin(items_), std::end(items_),
id, [](
auto const& lhs,
auto const& rhs) {
662 if (p == std::end(items_) || p->id !=
id)
663 return std::end(items_);
666 typename ItemContainerType::const_iterator findItem(
IdType id)
const
669 std::lower_bound(std::begin(items_), std::end(items_),
id, [](
auto const& lhs,
auto const& rhs) {
673 if (p == std::end(items_) || p->id !=
id)
674 return std::end(items_);
680 if (selected_.empty() && itemCount_ < (items_.size() / 2))
686 [](
auto const& item) {
691 return std::end(items_);
697 std::set<ItemWithId> selected_{};
Definition: selectables_registry.hpp:194
auto const * operator->() const
Definition: selectables_registry.hpp:211
auto const & operator*() const
Definition: selectables_registry.hpp:204
ConstIterator(typename ItemContainerType::iterator iter)
Definition: selectables_registry.hpp:200
Iterator that ignores items that are selected.
Definition: selectables_registry.hpp:92
IteratorBase & operator=(IteratorBase const &)=default
bool isEnd() const
Definition: selectables_registry.hpp:181
WrappedIterator beginIterator_
Definition: selectables_registry.hpp:188
IteratorBase & operator++()
Definition: selectables_registry.hpp:111
IteratorBase(IteratorBase const &)=default
IteratorBase operator+(std::size_t offset) const
Definition: selectables_registry.hpp:153
friend bool operator!=(const IteratorBase &lhs, const IteratorBase &rhs)
Definition: selectables_registry.hpp:148
IteratorBase & operator-=(std::size_t offset)
Definition: selectables_registry.hpp:175
IteratorBase & operator--()
Definition: selectables_registry.hpp:126
IteratorBase(WrappedIterator wrapped, WrappedIterator begin, WrappedIterator end)
Definition: selectables_registry.hpp:94
friend bool operator==(const IteratorBase &lhs, const IteratorBase &rhs)
Definition: selectables_registry.hpp:143
WrappedIterator endIterator_
Definition: selectables_registry.hpp:189
IteratorBase & operator+=(std::size_t offset)
Definition: selectables_registry.hpp:169
IteratorBase operator-(std::size_t offset) const
Definition: selectables_registry.hpp:161
WrappedIterator wrappedIterator_
Definition: selectables_registry.hpp:187
IteratorBase(IteratorBase &&)=default
IteratorBase operator--(int)
Definition: selectables_registry.hpp:136
IteratorBase & operator=(IteratorBase &&)=default
IteratorBase operator++(int)
Definition: selectables_registry.hpp:119
Definition: selectables_registry.hpp:221
auto * operator->()
Definition: selectables_registry.hpp:234
auto & operator*()
Definition: selectables_registry.hpp:227
This container associates items with an id and allows for individual items to be "selected",...
Definition: selectables_registry.hpp:32
ConstIteratorType cend() const
Returns end iterator.
Definition: selectables_registry.hpp:543
ItemContainerType::const_iterator rawConstEnd() const
Returns a const iterator to the underlying container.
Definition: selectables_registry.hpp:607
constexpr static auto invalidId
Invalid id value.
Definition: selectables_registry.hpp:81
ItemContainerType::const_iterator rawBegin() const
Returns a const iterator to the underlying container.
Definition: selectables_registry.hpp:583
IteratorType begin()
Returns iterator to first unselected item or end.
Definition: selectables_registry.hpp:503
IteratorType erase(IdType id)
Erase/Remove an item from the container.
Definition: selectables_registry.hpp:312
auto const & operator[](IdType id) const
Returns item by id.
Definition: selectables_registry.hpp:484
ConstIteratorType begin() const
Returns iterator to first unselected item or end.
Definition: selectables_registry.hpp:511
IteratorType end()
Returns end iterator.
Definition: selectables_registry.hpp:527
ConstIteratorType end() const
Returns end iterator.
Definition: selectables_registry.hpp:535
SelectablesRegistry(SelectablesRegistry &&)=default
ItemContainerType::const_iterator rawConstBegin() const
Returns a const iterator to the underlying container.
Definition: selectables_registry.hpp:591
IdType emplace(Args &&... args)
Emplace an item to the container.
Definition: selectables_registry.hpp:295
SelectablesRegistry & operator=(const SelectablesRegistry &)=default
std::size_t IdType
Id type used to identify items.
Definition: selectables_registry.hpp:35
std::size_t deselectAll(std::invocable< ItemWithId const & > auto const &callback)
Deselects all items.
Definition: selectables_registry.hpp:390
ItemContainerType::iterator rawBegin()
Returns an iterator to the underlying container.
Definition: selectables_registry.hpp:567
ConstIteratorType cbegin() const
Returns iterator to first unselected item or end.
Definition: selectables_registry.hpp:519
std::optional< T > pop(IdType id)
Erase/Remove an item from the container and return it.
Definition: selectables_registry.hpp:331
bool deselect(IdType id, std::invocable< ItemWithId const & > auto const &callback)
Deselects item with id.
Definition: selectables_registry.hpp:420
SelectionResult select(IdType id)
Select an item.
Definition: selectables_registry.hpp:363
SelectablesRegistry(const SelectablesRegistry &)=default
ItemContainerType::iterator rawEnd()
Returns an iterator to the underlying container.
Definition: selectables_registry.hpp:575
IdType append(T &&element)
Append an item to the container.
Definition: selectables_registry.hpp:276
SelectablesRegistry()=default
std::vector< ItemWithId > ItemContainerType
Type of the container that stores the items.
Definition: selectables_registry.hpp:78
SelectablesRegistry & operator=(SelectablesRegistry &&)=default
RawRangeWrap< SelectablesRegistry< T > const * > rawRange() const
Helper for range based for loops.
Definition: selectables_registry.hpp:649
bool empty() const
Returns whether the container is empty.
Definition: selectables_registry.hpp:551
IteratorType get(IdType id)
Get iterator to item with id.
Definition: selectables_registry.hpp:456
RawRangeWrap< SelectablesRegistry< T > * > rawRange()
Helper for range based for loops.
Definition: selectables_registry.hpp:639
ItemContainerType::const_iterator rawEnd() const
Returns a const iterator to the underlying container.
Definition: selectables_registry.hpp:599
~SelectablesRegistry()=default
ConstIteratorType get(IdType id) const
Get iterator to item with id.
Definition: selectables_registry.hpp:470
IdType append(T const &element)
Append an item to the container.
Definition: selectables_registry.hpp:259
auto & operator[](IdType id)
Returns item by id.
Definition: selectables_registry.hpp:495
void clear()
Definition: selectables_registry.hpp:442
std::size_t size() const
Returns the amount of items in the container.
Definition: selectables_registry.hpp:559
Definition: file_dialog.hpp:6
Definition: selectables_registry.hpp:19
Wrapper around items that associates them with an id.
Definition: selectables_registry.hpp:41
ItemWithId(IdType id, std::optional< T > item)
Definition: selectables_registry.hpp:61
ItemWithId & operator=(ItemWithId &&)=default
std::optional< T > item
The item.
Definition: selectables_registry.hpp:50
ItemWithId(IdType id, T item)
Definition: selectables_registry.hpp:57
bool operator<(ItemWithId const &other) const
Compares the id of the item.
Definition: selectables_registry.hpp:71
ItemWithId(IdType id, Detail::InplaceTag, Args &&... args)
Definition: selectables_registry.hpp:53
ItemWithId(ItemWithId &&)=default
ItemWithId & operator=(ItemWithId const &)=default
IdType id
Id of the item.
Definition: selectables_registry.hpp:43
ItemWithId(ItemWithId const &)=default
Definition: selectables_registry.hpp:614
RegistryPtr registry
Definition: selectables_registry.hpp:615
ItemContainerType::iterator end() const
Definition: selectables_registry.hpp:620
ItemContainerType::iterator cend() const
Definition: selectables_registry.hpp:628
ItemContainerType::iterator begin() const
Definition: selectables_registry.hpp:616
ItemContainerType::iterator cbegin() const
Definition: selectables_registry.hpp:624
Definition: selectables_registry.hpp:346
std::optional< T > * item
Pointer to the selected item (may be nullptr).
Definition: selectables_registry.hpp:348
bool found
Whether the item was found.
Definition: selectables_registry.hpp:351
bool alreadySelected
Whether the item was already selected.
Definition: selectables_registry.hpp:354