Skip to content

Commit

Permalink
add void callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
pantor committed Aug 10, 2020
1 parent 0b7d3d6 commit 5f9d429
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 6 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,12 @@ env.add_callback("double-greetings", 0, [greet](Arguments args) {
return greet + " " + greet + "!";
});
env.render("{{ double-greetings }}", data); // "Hello Hello!"
// You can also add a callback without return variable, e.g. for debugging:
env.add_void_callback("log", 1, [greet](Arguments args) {
std::cout << "logging: " << args[0] << std::endl;
});
env.render("{{ log(neighbour) }}", data); // Prints nothing to result, only to cout...
```

### Comments
Expand Down
16 changes: 15 additions & 1 deletion include/inja/environment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,14 @@ class Environment {
@brief Adds a variadic callback
*/
void add_callback(const std::string &name, const CallbackFunction &callback) {
function_storage.add_callback(name, -1, callback);
add_callback(name, -1, callback);
}

/*!
@brief Adds a variadic void callback
*/
void add_void_callback(const std::string &name, const VoidCallbackFunction &callback) {
add_void_callback(name, -1, callback);
}

/*!
Expand All @@ -183,6 +190,13 @@ class Environment {
function_storage.add_callback(name, num_args, callback);
}

/*!
@brief Adds a void callback with given number or arguments
*/
void add_void_callback(const std::string &name, int num_args, const VoidCallbackFunction &callback) {
function_storage.add_callback(name, num_args, [callback](Arguments& args) { callback(args); return json(); });
}

/** Includes a template with a given name into the environment.
* Then, a template can be rendered in another template using the
* include "<name>" syntax.
Expand Down
1 change: 1 addition & 0 deletions include/inja/function_storage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ using json = nlohmann::json;

using Arguments = std::vector<const json *>;
using CallbackFunction = std::function<json(Arguments &args)>;
using VoidCallbackFunction = std::function<void(Arguments &args)>;

/*!
* \brief Class for builtin functions and user-defined callbacks.
Expand Down
3 changes: 1 addition & 2 deletions include/inja/renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,9 @@ class Renderer : public NodeVisitor {
void print_json(const std::shared_ptr<json> value) {
if (value->is_string()) {
*output_stream << value->get_ref<const json::string_t&>();
} else if (value->is_number_float()) {
*output_stream << value->dump();
} else if (value->is_number_integer()) {
*output_stream << value->get<const json::number_integer_t>();
} else if (value->is_null()) {
} else {
*output_stream << value->dump();
}
Expand Down
20 changes: 17 additions & 3 deletions single_include/inja/inja.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1526,6 +1526,7 @@ using json = nlohmann::json;

using Arguments = std::vector<const json *>;
using CallbackFunction = std::function<json(Arguments &args)>;
using VoidCallbackFunction = std::function<void(Arguments &args)>;

/*!
* \brief Class for builtin functions and user-defined callbacks.
Expand Down Expand Up @@ -3348,10 +3349,9 @@ class Renderer : public NodeVisitor {
void print_json(const std::shared_ptr<json> value) {
if (value->is_string()) {
*output_stream << value->get_ref<const json::string_t&>();
} else if (value->is_number_float()) {
*output_stream << value->dump();
} else if (value->is_number_integer()) {
*output_stream << value->get<const json::number_integer_t>();
} else if (value->is_null()) {
} else {
*output_stream << value->dump();
}
Expand Down Expand Up @@ -4061,7 +4061,14 @@ class Environment {
@brief Adds a variadic callback
*/
void add_callback(const std::string &name, const CallbackFunction &callback) {
function_storage.add_callback(name, -1, callback);
add_callback(name, -1, callback);
}

/*!
@brief Adds a variadic void callback
*/
void add_void_callback(const std::string &name, const VoidCallbackFunction &callback) {
add_void_callback(name, -1, callback);
}

/*!
Expand All @@ -4071,6 +4078,13 @@ class Environment {
function_storage.add_callback(name, num_args, callback);
}

/*!
@brief Adds a void callback with given number or arguments
*/
void add_void_callback(const std::string &name, int num_args, const VoidCallbackFunction &callback) {
function_storage.add_callback(name, num_args, [callback](Arguments& args) { callback(args); return json(); });
}

/** Includes a template with a given name into the environment.
* Then, a template can be rendered in another template using the
* include "<name>" syntax.
Expand Down
5 changes: 5 additions & 0 deletions test/test-functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,15 @@ TEST_CASE("callbacks") {
return number1.length();
});

env.add_void_callback("log", 1, [](inja::Arguments args) {
int a = 2;
});

env.add_callback("multiply", 0, [](inja::Arguments args) { return 1.0; });

CHECK(env.render("{{ double(age) }}", data) == "56");
CHECK(env.render("{{ half(age) }}", data) == "14");
CHECK(env.render("{{ log(age) }}", data) == "");
CHECK(env.render("{{ double-greetings }}", data) == "Hello Hello!");
CHECK(env.render("{{ double-greetings() }}", data) == "Hello Hello!");
CHECK(env.render("{{ multiply(4, 5) }}", data) == "20.0");
Expand Down

0 comments on commit 5f9d429

Please sign in to comment.