codac 2.0.0
Loading...
Searching...
No Matches
codac2_ExprBase.h
Go to the documentation of this file.
1
9
10#pragma once
11
12#include <map>
13#include <memory>
14#include <cassert>
15#include <utility>
16#include "codac2_Domain.h"
17#include "codac2_Index.h"
18
19namespace codac2
20{
21 class ExprBase;
22 class VarBase;
23
32 class ExprID
33 {
34 public:
35
43
49 Index id() const;
50
57 bool operator==(const ExprID& i) const;
58
68 bool operator<(const ExprID& i) const;
69
70 protected:
71
72 const Index _id;
73 static Index _id_counter;
74 };
75
84 class ExprBase : public std::enable_shared_from_this<ExprBase>
85 {
86 public:
87
94
103 virtual std::shared_ptr<ExprBase> copy() const = 0;
104
115 virtual void replace_arg(const ExprID& old_arg_id, const std::shared_ptr<ExprBase>& new_expr) = 0;
116
122 const ExprID& unique_id() const;
123
133 bool operator==(const ExprBase& e) const;
134
141 virtual ~ExprBase() = default;
142
143 protected:
144
146 };
147
162 template<typename... X>
164 {
165 public:
166
176 OperationExprBase(std::shared_ptr<X>... x)
177 : _x(std::make_tuple((x)...))
178 { }
179
191 : _x(e._x)
192 {
193 std::apply(
194 [](auto &&... x)
195 {
196 ((x = __copy(x)), ...);
197 }, _x);
198 }
199
209 void replace_arg(const ExprID& old_arg_id, const std::shared_ptr<ExprBase>& new_expr)
210 {
211 std::apply(
212 [old_arg_id,new_expr](auto &&... x)
213 {
214 (__replace_arg(x,old_arg_id,new_expr), ...);
215 }, _x);
216 }
217
218 protected:
219
229 template<typename X_>
230 static std::shared_ptr<X_> __copy(const std::shared_ptr<X_>& x)
231 {
232 return std::dynamic_pointer_cast<X_>(x->copy());
233 }
234
246 template<typename D>
247 static void __replace_arg(std::shared_ptr<D>& x, const ExprID& old_arg_id, const std::shared_ptr<ExprBase>& new_expr)
248 {
249 if(x->unique_id() == old_arg_id)
250 {
251 assert(std::dynamic_pointer_cast<VarBase>(x) && "this subexpr should be some variable");
252 x = std::dynamic_pointer_cast<D>(new_expr);
253 }
254 else
255 x->replace_arg(old_arg_id, new_expr);
256 }
257
258 std::tuple<std::shared_ptr<X>...> _x;
259 };
260}
Abstract base class for representing an expression.
Definition codac2_ExprBase.h:85
const ExprID & unique_id() const
Returns the unique identifier of the expression.
virtual ~ExprBase()=default
Virtual destructor.
const ExprID _unique_id
unique identifier for this expression
Definition codac2_ExprBase.h:145
virtual void replace_arg(const ExprID &old_arg_id, const std::shared_ptr< ExprBase > &new_expr)=0
Replaces a variable by a new expression.
virtual std::shared_ptr< ExprBase > copy() const =0
Creates a copy of the current expression.
ExprBase()
Default constructor.
bool operator==(const ExprBase &e) const
Equality operator for comparing two expressions.
A class representing a unique identifier for expressions.
Definition codac2_ExprBase.h:33
ExprID()
Default constructor.
bool operator<(const ExprID &i) const
Comparison operator.
Index id() const
Retrieves the unique identifier of the expression.
static Index _id_counter
static counter used to generate unique IDs for each ExprID object
Definition codac2_ExprBase.h:73
const Index _id
unique identifier, cannot be modified after initialization
Definition codac2_ExprBase.h:72
bool operator==(const ExprID &i) const
Equality operator.
OperationExprBase(const OperationExprBase< X... > &e)
Copy constructor.
Definition codac2_ExprBase.h:190
void replace_arg(const ExprID &old_arg_id, const std::shared_ptr< ExprBase > &new_expr)
Replaces a variable by a new expression.
Definition codac2_ExprBase.h:209
OperationExprBase(std::shared_ptr< X >... x)
Constructs an OperationExprBase with operand expressions.
Definition codac2_ExprBase.h:176
static void __replace_arg(std::shared_ptr< D > &x, const ExprID &old_arg_id, const std::shared_ptr< ExprBase > &new_expr)
Helper function to replace a variable by a new expression.
Definition codac2_ExprBase.h:247
std::tuple< std::shared_ptr< X >... > _x
tuple storing the operand expressions
Definition codac2_ExprBase.h:258
static std::shared_ptr< X_ > __copy(const std::shared_ptr< X_ > &x)
Helper function to copy a single operand expression.
Definition codac2_ExprBase.h:230
Abstract base class for representing variables in analytic or set functions.
Definition codac2_VarBase.h:24