CYD-UI
A C++ library for building native graphic user interfaces
Loading...
Searching...
No Matches
expression.cppm
Go to the documentation of this file.
1
5
7
8import std;
9
10import fabric.logging;
11
12export import :types;
13
14export template <typename Type>
16public:
19 class node_t {
20 public:
21 using sptr = std::shared_ptr<node_t>;
22
32
33 explicit node_t(operation op_)
34 : op(op_) {
35 switch (op) {
36 case DIMENSION:
37 dimension = nullptr;
38 break;
39 case ADDITION:
40 case SUBTRACTION:
41 case MULTIPLICATION:
42 case DIVISION:
43 children = {};
44 break;
45 case CONSTANT:
46 case PARAMETER:
47 default:
48 break;
49 }
50 }
51
53 switch (op) {
54 case DIMENSION:
55 dimension.reset();
56 break;
57 case ADDITION:
58 case SUBTRACTION:
59 case MULTIPLICATION:
60 case DIVISION:
61 children.clear();
62 break;
63 case CONSTANT:
64 case PARAMETER:
65 default:
66 break;
67 }
68 }
69
70 static sptr make(operation op_) {
71 return std::make_shared<node_t>(op_);
72 }
73
74 bool operator==(const node_t& other) const {
75 if (op != other.op)
76 return false;
77
78 switch (op) {
79 case CONSTANT:
80 return (const_value == other.const_value);
81 case DIMENSION:
82 return (dimension == other.dimension);
83 case PARAMETER:
84 return (parameter == other.parameter);
85 case ADDITION:
86 case SUBTRACTION:
87 case MULTIPLICATION:
88 case DIVISION:
89 if (children.size() != other.children.size())
90 return false;
91 for (auto it1 = children.begin(), it2 = other.children.begin(); it1 != children.end();
92 ++it1, ++it2) {
93 if ((*it1 != *it2) or (*(*it1).get() != *(*it2).get()))
94 return false;
95 }
96 return true;
97 }
98 }
99
100 std::string to_string() const {
101 std::stringstream sts{};
102 switch (op) {
103 case CONSTANT:
104 sts << const_value;
105 break;
106 case DIMENSION:
107 sts << std::format("[0x{:X}](", (unsigned long)dimension.get());
108 sts << dimension->value_ << ")";
109 break;
110 case PARAMETER:
111 sts << std::format("[{}]", parameter.name);
112 break;
113 case ADDITION:
114 case SUBTRACTION:
115 case MULTIPLICATION:
116 case DIVISION:
117 if (children.empty()) {
118 sts << "()";
119 } else {
120 sts << "(";
121 auto it = children.begin();
122 sts << it->get()->to_string();
123 ++it;
124 for (; it != children.end(); ++it) {
125 switch (op) {
126 case ADDITION:
127 sts << " + ";
128 break;
129 case SUBTRACTION:
130 sts << " - ";
131 break;
132 case MULTIPLICATION:
133 sts << " * ";
134 break;
135 case DIVISION:
136 sts << " / ";
137 break;
138 default:
139 break;
140 }
141 sts << it->get()->to_string();
142 }
143 sts << ")";
144 }
145 break;
146 }
147 return sts.str();
148 }
149
151 std::shared_ptr<dimension_impl<Type>> dimension{nullptr};
152 std::list<sptr> children{};
154 };
155
156 expression() = default;
157 expression(const Type& value) {
158 auto node = make_node(node_t::CONSTANT);
159 node->const_value = value;
160 tree_ = node;
161 }
162
163 void add_dependency(std::shared_ptr<dimension_impl<Type>> dependency) {
164 dependencies_.insert(dependency);
165 }
166
167 static typename node_t::sptr make_node(node_t::operation op) { return node_t::make(op); }
168
169 bool operator==(const expression& other) const {
170 if (other.dependencies_ != dependencies_)
171 return false;
172
173 if (tree_ == other.tree_) {
174 return true;
175 } else if (tree_ == nullptr || other.tree_ == nullptr) {
176 return false;
177 } else {
178 return (*tree_.get() == *other.tree_.get());
179 }
180 }
181
182 std::string to_string() const {
183 if (tree_ == nullptr) {
184 return "default";
185 } else {
186 return tree_->to_string();
187 }
188 }
189
190 bool empty() const {
191 return tree_.get() == nullptr;
192 }
193
194 void clear() {
195 tree_.reset();
196 tree_ = {nullptr};
197 dependencies_.clear();
198 }
199
200 auto& tree() {
201 return tree_;
202 }
203
204 const auto& tree() const {
205 return tree_;
206 }
207
208 auto& dependencies() {
209 return dependencies_;
210 }
211
212 const auto& dependencies() const {
213 return dependencies_;
214 }
215
216 auto& parameters() {
217 return parameters_;
218 }
219
220 const auto& parameters() const {
221 return parameters_;
222 }
223
226
227private:
228 typename node_t::sptr tree_ = nullptr;
229 std::unordered_set<std::shared_ptr<dimension_impl<Type>>> dependencies_{};
230 std::unordered_set<parameter> parameters_{};
231};
std::shared_ptr< dimension_impl< Type > > dimension
std::shared_ptr< node_t > sptr
bool operator==(const node_t &other) const
enum cydui::dimensions::expression::node_t::operation op
static sptr make(operation op_)
void add_dependency(std::shared_ptr< dimension_impl< Type > > dependency)
const auto & tree() const
static node_t::sptr make_node(node_t::operation op)
const auto & dependencies() const
bool operator==(const expression &other) const
std::string to_string() const
const auto & parameters() const
expression(const Type &value)