CYD-UI
A C++ library for building native graphic user interfaces
Loading...
Searching...
No Matches
component_holder.cppm
Go to the documentation of this file.
1// Copyright (c) 2024, Víctor Castillo Agüero.
2// SPDX-License-Identifier: GPL-3.0-or-later
3
5
6import std;
7export import :type;
8
9export
10{
12 struct component_builder_t {
14 component_builder_t() = default;
15
16 template<ComponentConcept C>
17 component_builder_t(C comp) {
18 components.emplace_back([=] {
19 return std::shared_ptr<component_base_t> {new C {comp}};
20 });
21 }
23 std::vector<std::shared_ptr<component_base_t>> get_components() const {
24 std::vector<std::shared_ptr<component_base_t>> _components { };
25 for (const auto &builder: components) {
26 _components.emplace_back(builder());
27 }
28 return _components;
29 }
31 const auto &get_component_constructors() const {
32 return components;
33 }
35 void append_component(const std::function<std::shared_ptr<component_base_t>()>& component) {
36 components.emplace_back(component);
37 }
39 void transform(auto&& fun) {
40 for (auto &builder: components) {
41 auto original_builder = builder;
42 builder = [=]() {
43 return fun(original_builder());
44 };
45 }
46 }
48 bool empty() const {
49 return components.empty();
50 }
52 std::size_t size() const {
53 return components.size();
54 }
55
56 private:
57 std::vector<std::function<std::shared_ptr<component_base_t>()>> components { };
58 };
60 struct component_holder_t {
61 //template<ComponentConcept C>
62 //inline component_holder_t(C &&comp) {
63 // components[""] = new C {comp};
64 // //components[""] = std::make_unique<C>(comp);
65 //}
66 //
67 //template<ComponentConcept C>
68 //inline component_holder_t(C &comp) {
69 // components[""] = new C {comp};
70 // //components[""] = std::make_unique<C>(comp);
71 //}
72 component_holder_t() = default;
73
74 template<ComponentConcept ...C>
75 component_holder_t(C... comps) noexcept {
76 (components.emplace_back(std::shared_ptr<component_base_t>{new C{comps}}), ...);
77 }
78
79 template<ComponentConcept C>
80 component_holder_t(std::vector<C> comps) noexcept {
81 for (auto &comp: comps) {
82 components.emplace_back(std::shared_ptr<component_base_t> {new C {comp}});
83 }
84 }
85
86 template<ComponentConcept C>
87 component_holder_t(C comp) {
88 components.emplace_back(std::shared_ptr<component_base_t> {new C {comp}});
89 }
92 for (const auto &item: builder.get_components()) {
93 components.emplace_back(item);
94 }
95 }
98 components.insert_range(components.end(), other.components);
99 }
101 component_holder_t(component_holder_t &&other) noexcept {
102 components.insert_range(components.end(), std::move(other.components));
103 }
106 components = other.components;
107 return *this;
108 }
111 components = std::move(other.components);
112 return *this;
113 }
116 components.insert_range(components.end(), other.components);
117 return *this;
118 }
121 const std::vector<std::shared_ptr<component_base_t>> &components_
122 ): components(components_) {
123 }
125 [[nodiscard]] auto &get_components() {
126 return components;
127 }
129 [[nodiscard]] const auto &get_components() const {
130 return components;
131 }
132
133 // Cannot work, since we would need to recursively copy any `component_holder_t` in the props
134 // of each component.
135 //component_holder_t clone() const {
136 //
137 //}
139 void append_component(std::shared_ptr<component_base_t> component) {
140 components.emplace_back(component);
141 }
143 auto begin() {
144 return components.begin();
145 }
147 auto begin() const {
148 return components.begin();
149 }
151 auto end() {
152 return components.end();
153 }
155 auto end() const {
156 return components.end();
157 }
158
159 private:
160 std::vector<std::shared_ptr<component_base_t>> components { };
161 };
162 }
163}
std::vector< std::shared_ptr< component_base_t > > get_components() const
void append_component(const std::function< std::shared_ptr< component_base_t >()> &component)
void append_component(std::shared_ptr< component_base_t > component)
component_holder_t & operator=(const component_holder_t &other)
component_holder_t & append(const component_holder_t &other)