25 explicit ToolSet(std::vector<std::unique_ptr<Tool>> tools) {
26 if (std::any_of(tools.begin(), tools.end(),
27 [](
const auto& tool) { return !tool; }))
28 throw std::invalid_argument(
"ToolSet cannot own a null Tool");
31 std::make_shared<const std::vector<std::unique_ptr<Tool>>>(
35 explicit ToolSet(std::vector<std::shared_ptr<Tool>> tools) {
36 if (std::any_of(tools.begin(), tools.end(),
37 [](
const auto& tool) { return !tool; }))
38 throw std::invalid_argument(
"ToolSet cannot own a null Tool");
41 std::make_shared<const std::vector<std::shared_ptr<Tool>>>(
45 std::vector<Tool*> view()
const {
46 if (selection_)
return *selection_;
47 std::vector<Tool*> tools;
48 tools.reserve(size());
50 for (
const auto& tool : *unique_tools_) tools.push_back(tool.get());
52 for (
const auto& tool : *shared_tools_) tools.push_back(tool.get());
58 for (
Tool* tool : tools) {
59 const auto owns = [tool](
const auto& owner) {
return owner.get() == tool; };
60 const bool found = selection_
61 ? std::find(selection_->begin(), selection_->end(), tool) != selection_->end()
63 std::any_of(unique_tools_->begin(), unique_tools_->end(), owns))
65 std::any_of(shared_tools_->begin(), shared_tools_->end(), owns));
67 throw std::invalid_argument(
"ToolSet selection contains an unowned Tool");
71 result.unique_tools_ = unique_tools_;
72 result.shared_tools_ = shared_tools_;
73 result.selection_ = std::make_shared<const std::vector<Tool*>>(std::move(tools));
78 std::size_t size() const noexcept {
79 return selection_ ? selection_->size()
80 : (unique_tools_ ? unique_tools_->size() : 0)
81 + (shared_tools_ ? shared_tools_->size() : 0);
83 bool empty() const noexcept {
return size() == 0; }
86 std::shared_ptr<const std::vector<std::unique_ptr<Tool>>> unique_tools_;
87 std::shared_ptr<const std::vector<std::shared_ptr<Tool>>> shared_tools_;
88 std::shared_ptr<const std::vector<Tool*>> selection_;