Inja 3.3.0
A Template Engine for Modern C++
Loading...
Searching...
No Matches
function_storage.hpp
1#ifndef INCLUDE_INJA_FUNCTION_STORAGE_HPP_
2#define INCLUDE_INJA_FUNCTION_STORAGE_HPP_
3
4#include <vector>
5
6#include "string_view.hpp"
7
8namespace inja {
9
10using json = nlohmann::json;
11
12using Arguments = std::vector<const json *>;
13using CallbackFunction = std::function<json(Arguments &args)>;
14using VoidCallbackFunction = std::function<void(Arguments &args)>;
15
20public:
21 enum class Operation {
22 Not,
23 And,
24 Or,
25 In,
26 Equal,
27 NotEqual,
28 Greater,
29 GreaterEqual,
30 Less,
31 LessEqual,
32 Add,
33 Subtract,
34 Multiplication,
35 Division,
36 Power,
37 Modulo,
38 AtId,
39 At,
40 Default,
41 DivisibleBy,
42 Even,
43 Exists,
44 ExistsInObject,
45 First,
46 Float,
47 Int,
48 IsArray,
49 IsBoolean,
50 IsFloat,
51 IsInteger,
52 IsNumber,
53 IsObject,
54 IsString,
55 Last,
56 Length,
57 Lower,
58 Max,
59 Min,
60 Odd,
61 Range,
62 Round,
63 Sort,
64 Upper,
65 Super,
66 Join,
67 Callback,
68 ParenLeft,
69 ParenRight,
70 None,
71 };
72
73 struct FunctionData {
74 explicit FunctionData(const Operation &op, const CallbackFunction &cb = CallbackFunction{}) : operation(op), callback(cb) {}
75 const Operation operation;
76 const CallbackFunction callback;
77 };
78
79private:
80 const int VARIADIC {-1};
81
82 std::map<std::pair<std::string, int>, FunctionData> function_storage = {
83 {std::make_pair("at", 2), FunctionData { Operation::At }},
84 {std::make_pair("default", 2), FunctionData { Operation::Default }},
85 {std::make_pair("divisibleBy", 2), FunctionData { Operation::DivisibleBy }},
86 {std::make_pair("even", 1), FunctionData { Operation::Even }},
87 {std::make_pair("exists", 1), FunctionData { Operation::Exists }},
88 {std::make_pair("existsIn", 2), FunctionData { Operation::ExistsInObject }},
89 {std::make_pair("first", 1), FunctionData { Operation::First }},
90 {std::make_pair("float", 1), FunctionData { Operation::Float }},
91 {std::make_pair("int", 1), FunctionData { Operation::Int }},
92 {std::make_pair("isArray", 1), FunctionData { Operation::IsArray }},
93 {std::make_pair("isBoolean", 1), FunctionData { Operation::IsBoolean }},
94 {std::make_pair("isFloat", 1), FunctionData { Operation::IsFloat }},
95 {std::make_pair("isInteger", 1), FunctionData { Operation::IsInteger }},
96 {std::make_pair("isNumber", 1), FunctionData { Operation::IsNumber }},
97 {std::make_pair("isObject", 1), FunctionData { Operation::IsObject }},
98 {std::make_pair("isString", 1), FunctionData { Operation::IsString }},
99 {std::make_pair("last", 1), FunctionData { Operation::Last }},
100 {std::make_pair("length", 1), FunctionData { Operation::Length }},
101 {std::make_pair("lower", 1), FunctionData { Operation::Lower }},
102 {std::make_pair("max", 1), FunctionData { Operation::Max }},
103 {std::make_pair("min", 1), FunctionData { Operation::Min }},
104 {std::make_pair("odd", 1), FunctionData { Operation::Odd }},
105 {std::make_pair("range", 1), FunctionData { Operation::Range }},
106 {std::make_pair("round", 2), FunctionData { Operation::Round }},
107 {std::make_pair("sort", 1), FunctionData { Operation::Sort }},
108 {std::make_pair("upper", 1), FunctionData { Operation::Upper }},
109 {std::make_pair("super", 0), FunctionData { Operation::Super }},
110 {std::make_pair("super", 1), FunctionData { Operation::Super }},
111 {std::make_pair("join", 2), FunctionData { Operation::Join }},
112 };
113
114public:
115 void add_builtin(nonstd::string_view name, int num_args, Operation op) {
116 function_storage.emplace(std::make_pair(static_cast<std::string>(name), num_args), FunctionData { op });
117 }
118
119 void add_callback(nonstd::string_view name, int num_args, const CallbackFunction &callback) {
120 function_storage.emplace(std::make_pair(static_cast<std::string>(name), num_args), FunctionData { Operation::Callback, callback });
121 }
122
123 FunctionData find_function(nonstd::string_view name, int num_args) const {
124 auto it = function_storage.find(std::make_pair(static_cast<std::string>(name), num_args));
125 if (it != function_storage.end()) {
126 return it->second;
127
128 // Find variadic function
129 } else if (num_args > 0) {
130 it = function_storage.find(std::make_pair(static_cast<std::string>(name), VARIADIC));
131 if (it != function_storage.end()) {
132 return it->second;
133 }
134 }
135
136 return FunctionData { Operation::None };
137 }
138};
139
140} // namespace inja
141
142#endif // INCLUDE_INJA_FUNCTION_STORAGE_HPP_
Class for builtin functions and user-defined callbacks.
Definition: function_storage.hpp:19
Definition: function_storage.hpp:73