mirror of
https://github.com/pantor/inja.git
synced 2026-04-15 12:38:53 +00:00
Another optimization of passing arguments as reference (#23)
* Fixed passing callback arguments as reference * Another passing as const reference instead of new instance * Implemented variable number of parameters - extended function "function_regex" to pass minimum and maximum parameters. if minimum=maximum, all params are required, if minimum = 0, all params are optional..... - for single parameter there is different regex (created by the same function_regex function) to allo read sort([1,2,3]) as one parameter - extended Match to test if group is valid, so only valid params are filled to params vector - new function arguments_count to test number of arguments passed to callback * unit tests for updated callbacks with variable number of params * merge with last version * Optimized passing arguments as const reference
This commit is contained in:
@@ -359,7 +359,7 @@ public:
|
||||
|
||||
class Renderer {
|
||||
public:
|
||||
std::map<Parsed::CallbackSignature, std::function<json(Parsed::Arguments, const json&)>> map_callbacks;
|
||||
std::map<Parsed::CallbackSignature, std::function<json(const Parsed::Arguments&, const json&)>> map_callbacks;
|
||||
|
||||
template<bool>
|
||||
bool eval_expression(const Parsed::ElementExpression& element, const json &data) {
|
||||
@@ -968,14 +968,14 @@ public:
|
||||
return j;
|
||||
}
|
||||
|
||||
void add_callback(std::string name, int number_arguments, std::function<json(Parsed::Arguments, const json&)> callback) {
|
||||
void add_callback(std::string name, int number_arguments, std::function<json(const Parsed::Arguments&, const json&)> callback) {
|
||||
Parsed::CallbackSignature signature = std::make_pair(name, number_arguments);
|
||||
parser.regex_map_callbacks[signature] = Parser::function_regex(name, number_arguments);
|
||||
renderer.map_callbacks[signature] = callback;
|
||||
}
|
||||
|
||||
template<typename T = json>
|
||||
T get_argument(Parsed::Arguments args, int index, const json& data) {
|
||||
T get_argument(const Parsed::Arguments& args, int index, const json& data) {
|
||||
return renderer.eval_expression<T>(args[index], data);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user