codac 2.0.0
Loading...
Searching...
No Matches
codac2_FunctionBase.h
Go to the documentation of this file.
1
9
10#pragma once
11
12#include <map>
13#include "codac2_ExprBase.h"
16
17// todo: remove this:
19
20namespace codac2
21{
39 template<typename E>
41 {
42 public:
43
52 FunctionBase(const std::vector<std::reference_wrapper<VarBase>>& args, const std::shared_ptr<E>& y)
54 { }
55
64 FunctionBase(const FunctionArgsList& args, const std::shared_ptr<E>& y)
65 : _y(std::dynamic_pointer_cast<E>(y->copy())), _args(args)
66 { }
67
76 : _y(std::dynamic_pointer_cast<E>(f.expr()->copy())), _args(f.args()) // todo: keep this dynamic_pointer_cast?
77 { }
78
85 virtual ~FunctionBase()
86 { }
87
93 const FunctionArgsList& args() const
94 {
95 return _args;
96 }
97
103 const std::shared_ptr<E>& expr() const
104 {
105 return _y;
106 }
107
117 template<typename... X>
118 std::shared_ptr<E> operator()(const X&... x) const
119 {
120 auto expr_copy = expr()->copy();
121 size_t i = 0;
122 (expr_copy->replace_arg(_args[i++]->unique_id(), this->__get_copy(x)), ...);
123 assert_release(i == this->args().size() &&
124 "Invalid arguments: wrong number of input arguments");
125 return std::dynamic_pointer_cast<E>(expr_copy);
126 }
127
139 std::shared_ptr<E> operator()(const std::vector<std::shared_ptr<ExprBase>>& x) const
140 {
141 assert_release(x.size() == this->args().size() &&
142 "Invalid arguments: wrong number of input arguments");
143 auto expr_copy = expr()->copy();
144 for(size_t i = 0 ; i < x.size() ; i++)
145 expr_copy->replace_arg(_args[i]->unique_id(), x[i]->copy());
146 return std::dynamic_pointer_cast<E>(expr_copy);
147 }
148
155 Index input_size() const
156 {
157 return this->_args.total_size();
158 }
159
160 protected:
161
169 template<typename X>
170 std::shared_ptr<X> __get_copy(std::shared_ptr<X> x) const
171 {
172 return std::dynamic_pointer_cast<X>(x->copy());
173 }
174
182 template<typename X>
183 std::shared_ptr<ExprBase> __get_copy(const AnalyticExprWrapper<X>& x) const
184 {
185 return x->copy();
186 }
187
195 template<typename A>
196 auto __get_copy(const A& x) const
197 {
198 if constexpr(std::is_base_of_v<VarBase,A>)
199 return std::dynamic_pointer_cast<A>(x.copy());
200 else
201 {
202 // todo: make this generic (analytic / set):
203 return const_value(x);
204 }
205 }
206
207 const std::shared_ptr<E> _y;
209 };
210}
A container class to manage a collection of function arguments.
Definition codac2_FunctionArgsList.h:25
auto __get_copy(const A &x) const
Helper function to get a copy of various expression types.
Definition codac2_FunctionBase.h:196
std::shared_ptr< ExprBase > __get_copy(const AnalyticExprWrapper< X > &x) const
Helper function to get a copy of an AnalyticExprWrapper.
Definition codac2_FunctionBase.h:183
const FunctionArgsList & args() const
Returns the arguments of the function.
Definition codac2_FunctionBase.h:93
virtual ~FunctionBase()
Virtual destructor.
Definition codac2_FunctionBase.h:85
std::shared_ptr< E > operator()(const X &... x) const
Applies this function to the arguments and returns the resulting expression.
Definition codac2_FunctionBase.h:118
const FunctionArgsList _args
arguments of the function
Definition codac2_FunctionBase.h:208
FunctionBase(const std::vector< std::reference_wrapper< VarBase > > &args, const std::shared_ptr< E > &y)
Constructs a FunctionBase with a list of arguments and a shared pointer to an output expression.
Definition codac2_FunctionBase.h:52
std::shared_ptr< X > __get_copy(std::shared_ptr< X > x) const
Helper function to get a copy of a an expression.
Definition codac2_FunctionBase.h:170
Index input_size() const
Calculates the total size of the function arguments, as the sum of the sizes of each argument.
Definition codac2_FunctionBase.h:155
const std::shared_ptr< E > _y
expression that defines the function
Definition codac2_FunctionBase.h:207
FunctionBase(const FunctionArgsList &args, const std::shared_ptr< E > &y)
Constructs a FunctionBase with a list of arguments and a shared pointer to an output expression.
Definition codac2_FunctionBase.h:64
std::shared_ptr< E > operator()(const std::vector< std::shared_ptr< ExprBase > > &x) const
Applies this function to the arguments and returns the resulting expression.
Definition codac2_FunctionBase.h:139
FunctionBase(const FunctionBase< E > &f)
Copy constructor.
Definition codac2_FunctionBase.h:75
const std::shared_ptr< E > & expr() const
Returns the expression associated with the function.
Definition codac2_FunctionBase.h:103