1 #include "cpp_function_lib.h"
2 
add_one(double a,int b)3 double add_one(double a, int b)
4 {
5     return a + (double) b + 1.0;
6 }
7 
add_two(double a,int b)8 double add_two(double a, int b)
9 {
10     return a + (double) b + 2.0;
11 }
12 
13 
AddAnotherFunctor(double to_add)14 AddAnotherFunctor::AddAnotherFunctor(double to_add)
15     : to_add(to_add)
16 {
17 }
18 
operator ()(double a,int b) const19 double AddAnotherFunctor::operator()(double a, int b) const
20 {
21     return a + (double) b + this->to_add;
22 };
23 
24 
FunctionKeeper(std::function<double (double,int)> user_function)25 FunctionKeeper::FunctionKeeper(std::function<double(double, int)> user_function)
26     : my_function(user_function)
27 {
28 }
29 
~FunctionKeeper()30 FunctionKeeper::~FunctionKeeper()
31 {
32 }
33 
set_function(std::function<double (double,int)> user_function)34 void FunctionKeeper::set_function(std::function<double(double, int)> user_function)
35 {
36     this->my_function = user_function;
37 }
38 
get_function() const39 std::function<double(double, int)> FunctionKeeper::get_function() const
40 {
41     return this->my_function;
42 }
43 
call_function(double a,int b) const44 double FunctionKeeper::call_function(double a, int b) const
45 {
46     if (!this->my_function) {
47         throw std::runtime_error("Trying to call undefined function!");
48     }
49     return this->my_function(a, b);
50 };
51