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 size_t nb_args() const
104 {
105 return _args.size();
106 }
107
113 const std::shared_ptr<E>& expr() const
114 {
115 return _y;
116 }
117
127 template<typename... X>
128 std::shared_ptr<E> operator()(const X&... x) const
129 {
130 auto expr_copy = expr()->copy();
131 size_t i = 0;
132 (expr_copy->replace_arg(_args[i++]->unique_id(), this->__get_copy(x)), ...);
133 assert_release(i == this->args().size() &&
134 "Invalid arguments: wrong number of input arguments");
135 return std::dynamic_pointer_cast<E>(expr_copy);
136 }
137
149 std::shared_ptr<E> operator()(const std::vector<std::shared_ptr<ExprBase>>& x) const
150 {
151 assert_release(x.size() == this->args().size() &&
152 "Invalid arguments: wrong number of input arguments");
153 auto expr_copy = expr()->copy();
154 for(size_t i = 0 ; i < x.size() ; i++)
155 expr_copy->replace_arg(_args[i]->unique_id(), x[i]->copy());
156 return std::dynamic_pointer_cast<E>(expr_copy);
157 }
158
165 Index input_size() const
166 {
167 return this->_args.total_size();
168 }
169
170 protected:
171
179 template<typename X>
180 std::shared_ptr<X> __get_copy(std::shared_ptr<X> x) const
181 {
182 return std::dynamic_pointer_cast<X>(x->copy());
183 }
184
192 template<typename X>
193 std::shared_ptr<ExprBase> __get_copy(const AnalyticExprWrapper<X>& x) const
194 {
195 return x->copy();
196 }
197
205 template<typename A>
206 auto __get_copy(const A& x) const
207 {
208 if constexpr(std::is_base_of_v<VarBase,A>)
209 return std::dynamic_pointer_cast<A>(x.copy());
210 else
211 {
212 // todo: make this generic (analytic / set):
213 return const_value(x);
214 }
215 }
216
217 const std::shared_ptr<E> _y;
219 };
220}
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:206
std::shared_ptr< ExprBase > __get_copy(const AnalyticExprWrapper< X > &x) const
Helper function to get a copy of an AnalyticExprWrapper.
Definition codac2_FunctionBase.h:193
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:128
const FunctionArgsList _args
arguments of the function
Definition codac2_FunctionBase.h:218
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:180
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:165
const std::shared_ptr< E > _y
expression that defines the function
Definition codac2_FunctionBase.h:217
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:149
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:113
size_t nb_args() const
Returns the number of arguments of the function.
Definition codac2_FunctionBase.h:103