#pragma once #include #include #include template class SelectionManager { public: SelectionManager() : _multiselection_enabled(false) {} void discard() { _selected_things.clear(); } void fetch(T * const thing) { bool already_there = std::any_of(_selected_things.begin(), _selected_things.end(), [&thing](const auto& selected_thing) { return thing == selected_thing; }); if (!already_there) _selected_things.emplace_back(thing); } void remove(T * const thing) { for (std::size_t i = 0; i < _selected_things.size(); ++i) { if (thing == _selected_things.at(i)) { _selected_things[i] = std::move(_selected_things.back()); _selected_things.pop_back(); break; } } } bool isSelected(const T * const thing) const { for (const auto& selected_thing : thing) if (thing == selected_thing) return true; return false; } void enableMultiselection(bool enable = true) { _multiselection_enabled = enable; } bool isMultiselectionEnabled() const { return _multiselection_enabled; } void apply(const std::function& function) { for (auto& thing : _selected_things) function(thing); } private: std::vector _selected_things; bool _multiselection_enabled; };