summaryrefslogtreecommitdiff
path: root/tests/run/cpp_function_lib.cpp
blob: c5a432220ec2474af0fb6378138aa8efd1e7cf27 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "cpp_function_lib.h"

double add_one(double a, int b)
{
    return a + (double) b + 1.0;
}

double add_two(double a, int b)
{
    return a + (double) b + 2.0;
}


AddAnotherFunctor::AddAnotherFunctor(double to_add)
    : to_add(to_add)
{
}

double AddAnotherFunctor::operator()(double a, int b) const
{
    return a + (double) b + this->to_add;
};


FunctionKeeper::FunctionKeeper(std::function<double(double, int)> user_function)
    : my_function(user_function)
{
}

FunctionKeeper::~FunctionKeeper()
{
}

void FunctionKeeper::set_function(std::function<double(double, int)> user_function)
{
    this->my_function = user_function;
}

std::function<double(double, int)> FunctionKeeper::get_function() const
{
    return this->my_function;
}

double FunctionKeeper::call_function(double a, int b) const
{
    if (!this->my_function) {
        throw std::runtime_error("Trying to call undefined function!");
    }
    return this->my_function(a, b);
};