-
Notifications
You must be signed in to change notification settings - Fork 0
/
FunctionNamespace.h
146 lines (119 loc) · 2.77 KB
/
FunctionNamespace.h
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#pragma once
#include "Functions/IFunction.h"
#include "FunctionArguments.h"
#include <vector>
#include <string>
#include <string_view>
#include <memory>
#include <map>
class FunctionCaller final
{
public:
FunctionCaller() = default;
FunctionCaller(cfunc_ptr _func, FunctionArguments&& _args)
{
SetFunction(_func, std::move(_args));
}
void SetFunction(cfunc_ptr _func, FunctionArguments&& _args)
{
func = std::move(_func);
args = std::move(_args);
}
bool can_call(size_t size) const
{
return args.can_call(size);
}
type call(std::vector<type>&& arguments) const
{
auto x = args.superimpose(std::move(arguments));
return func->calculate(std::move(x.first), std::move(x.second));
}
cfunc_ptr get() const
{
return func;
}
operator bool() const
{
return func != nullptr;
}
private:
FunctionArguments args;
cfunc_ptr func = nullptr;
};
class FunctionByName final
{
public:
FunctionByName() = default;
void AddFunction(cfunc_ptr func, FunctionArguments&& args)
{
if (args.is_template())
{
if(temp)
throw std::exception("Function with template already exists");
temp = FunctionCaller(func, std::move(args));
return;
}
size_t cnt = args.size();
if (funcs.find(cnt) != funcs.end())
throw std::exception("This function alredy exists");
funcs[cnt] = FunctionCaller(func, std::move(args));
}
type call(std::vector<type>&& arguments) const
{
auto it = funcs.find(arguments.size());
if (it == funcs.end())
{
if (temp)
return temp.call(std::move(arguments));
throw std::exception("No function with this argument count");
}
return it->second.call(std::move(arguments));
}
const FunctionCaller* get(size_t size) const
{
auto it = funcs.find(size);
if (it != funcs.end())
return &it->second;
if (temp && temp.can_call(size))
return &temp;
return nullptr;
}
const FunctionCaller& get_or_create(size_t size)
{
return funcs[size];
}
private:
std::map<size_t, FunctionCaller> funcs;
FunctionCaller temp;
uint64_t temp_cnt = 0;
};
class FunctionNamespace final
{
public:
FunctionNamespace() = default;
void AddFunction(std::string&& name, cfunc_ptr func,
FunctionArguments&& args)
{
funcs[std::move(name)].AddFunction(func, std::move(args));
}
type call(std::string const& name, std::vector<type>&& arguments) const
{
auto it = funcs.find(name);
if (it == funcs.end())
throw std::exception("No function wtih this name");
return it->second.call(std::move(arguments));
}
const FunctionByName* get(std::string const& name) const
{
auto it = funcs.find(name);
if (it == funcs.end())
return nullptr;
return &it->second;
}
const FunctionByName& get_or_create(std::string const& name)
{
return funcs[name];
}
protected:
std::unordered_map<std::string, FunctionByName> funcs;
};