1#ifndef INCLUDE_INJA_FUNCTION_STORAGE_HPP_
2#define INCLUDE_INJA_FUNCTION_STORAGE_HPP_
6#include "string_view.hpp"
10using json = nlohmann::json;
12using Arguments = std::vector<const json *>;
13using CallbackFunction = std::function<json(Arguments &args)>;
14using VoidCallbackFunction = std::function<void(Arguments &args)>;
21 enum class Operation {
74 explicit FunctionData(
const Operation &op,
const CallbackFunction &cb = CallbackFunction{}) : operation(op), callback(cb) {}
75 const Operation operation;
76 const CallbackFunction callback;
80 const int VARIADIC {-1};
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 }},
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 });
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 });
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()) {
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()) {
136 return FunctionData { Operation::None };
Class for builtin functions and user-defined callbacks.
Definition: function_storage.hpp:19
Definition: function_storage.hpp:73