codac 2.0.0
Loading...
Searching...
No Matches
codac2::OperationExprBase< X > Class Template Reference

A base class for expressions representing operations with multiple operands. More...

#include <codac2_ExprBase.h>

Public Member Functions

 OperationExprBase (std::shared_ptr< X >... x)
 Constructs an OperationExprBase with operand expressions.
 
 OperationExprBase (const OperationExprBase< X... > &e)
 Copy constructor.
 
void replace_arg (const ExprID &old_arg_id, const std::shared_ptr< ExprBase > &new_expr)
 Replaces a variable by a new expression.
 

Static Protected Member Functions

template<typename X_>
static std::shared_ptr< X_ > __copy (const std::shared_ptr< X_ > &x)
 Helper function to copy a single operand expression.
 
template<typename D>
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.
 

Protected Attributes

std::tuple< std::shared_ptr< X >... > _x
 tuple storing the operand expressions
 

Detailed Description

template<typename... X>
class codac2::OperationExprBase< X >

A base class for expressions representing operations with multiple operands.

The OperationExprBase class is a templated class designed to represent operations (e.g., addition, multiplication, squared root) that involve one or multiple operand expressions. The class holds a tuple of operand expressions, supports copying of operands, and provides functionality to replace specific expressions within the operation. The class is designed to be inherited by concrete operation expression classes, where the operands will be specialized.

Template Parameters
XThe types of the operands in the operation. It supports an arbitrary number of operand types via variadic templates.

Constructor & Destructor Documentation

◆ OperationExprBase() [1/2]

template<typename... X>
codac2::OperationExprBase< X >::OperationExprBase ( std::shared_ptr< X >... x)
inline

Constructs an OperationExprBase with operand expressions.

This constructor initializes the operation expression with a set of operand expressions, which are passed as shared pointers. These operands are stored in a tuple for efficient access and manipulation.

Parameters
xA variadic list of shared pointers to the operand expressions.
177 : _x(std::make_tuple((x)...))
178 { }
A base class for expressions representing operations with multiple operands.
Definition codac2_ExprBase.h:164
std::tuple< std::shared_ptr< X >... > _x
tuple storing the operand expressions
Definition codac2_ExprBase.h:258

◆ OperationExprBase() [2/2]

template<typename... X>
codac2::OperationExprBase< X >::OperationExprBase ( const OperationExprBase< X... > & e)
inline

Copy constructor.

This constructor creates a new OperationExprBase by copying the operand expressions from another instance. Each operand is deep-copied using the copy() method of the underlying expression objects, ensuring that the new instance has independent copies of the operands.

Parameters
eThe OperationExprBase instance to copy from.
191 : _x(e._x)
192 {
194 [](auto &&... x)
195 {
196 ((x = __copy(x)), ...);
197 }, _x);
198 }
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

Member Function Documentation

◆ replace_arg()

template<typename... X>
void codac2::OperationExprBase< X >::replace_arg ( const ExprID & old_arg_id,
const std::shared_ptr< ExprBase > & new_expr )
inline

Replaces a variable by a new expression.

This method replaces, in the current expression, a specific variable (corresponding to the old_arg_id) with a new expression.

Parameters
old_arg_idThe ExprID of the variable to replace.
new_exprA shared pointer to the new expression to insert.
210 {
212 [old_arg_id,new_expr](auto &&... x)
213 {
215 }, _x);
216 }
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

◆ __copy()

template<typename... X>
template<typename X_>
static std::shared_ptr< X_ > codac2::OperationExprBase< X >::__copy ( const std::shared_ptr< X_ > & x)
inlinestaticprotected

Helper function to copy a single operand expression.

This function performs a deep copy of an operand expression.

Template Parameters
X_The type of the operand expression.
Parameters
xA shared pointer to the operand expression to copy.
Returns
A shared pointer to the new copied expression.
231 {
232 return std::dynamic_pointer_cast<X_>(x->copy());
233 }

◆ __replace_arg()

template<typename... X>
template<typename D>
static void codac2::OperationExprBase< X >::__replace_arg ( std::shared_ptr< D > & x,
const ExprID & old_arg_id,
const std::shared_ptr< ExprBase > & new_expr )
inlinestaticprotected

Helper function to replace a variable by a new expression.

This function replaces, in the expression pointed by x, a specific variable (an argument, corresponding to the old_arg_id) with a new expression.

Template Parameters
DThe type of the current expression.
Parameters
xA reference to the shared pointer of the current expression.
old_arg_idThe ExprID of the variable to replace.
new_exprA shared pointer to the new expression to insert.
248 {
249 if(x->unique_id() == old_arg_id)
250 {
251 assert(std::dynamic_pointer_cast<VarBase>(x) && "this subexpr should be some variable");
253 }
254 else
256 }
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

The documentation for this class was generated from the following file: