Skip to content

Commit 21ca4a5

Browse files
committed
Rename basic_function_result -> function_result
1 parent 5594077 commit 21ca4a5

File tree

5 files changed

+98
-99
lines changed

5 files changed

+98
-99
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,12 +227,12 @@ Supported types for the C++ function parameters:
227227
- `jsonexpr::null_t = std::nullptr_t`, for null.
228228
- `jsonexpr::json = nlohmann::json`, for "any of the above" (handle type checks yourself).
229229

230-
The return value must be (convertible to) `jsonexpr::json`, or `jsonexpr::basic_function_result` if handling errors (see below for more information on error handling).
230+
The return value must be (convertible to) `jsonexpr::json`, or `jsonexpr::function_result` if handling errors (see below for more information on error handling).
231231

232232

233233
#### Error handling
234234

235-
The library will automatically take care of validating the type of each argument passed to this function, and report appropriate errors in case of a mismatch. However, if the function has error states based on the *values* of the parameters (e.g., here: the length of the array, or the types of the elements within it), these errors need to be handled and reported explicitly. This is done by returning a `jsonexpr::basic_function_result`, which is an alias for `jsonexpr::expected<jsonexpr::json, std::string>` (either a JSON value, or an error message).
235+
The library will automatically take care of validating the type of each argument passed to this function, and report appropriate errors in case of a mismatch. However, if the function has error states based on the *values* of the parameters (e.g., here: the length of the array, or the types of the elements within it), these errors need to be handled and reported explicitly. This is done by returning a `jsonexpr::function_result`, which is an alias for `jsonexpr::expected<jsonexpr::json, std::string>` (either a JSON value, or an error message).
236236

237237
In the example below, we check that each element in the array is a string:
238238

@@ -244,7 +244,7 @@ int main() {
244244
jsonexpr::register_function(
245245
funcs, "join",
246246
[](const std::vector<jsonexpr::json>& array,
247-
const std::string& separator) -> jsonexpr::basic_function_result {
247+
const std::string& separator) -> jsonexpr::function_result {
248248
for (std::size_t i = 0; i < array.size(); ++i) {
249249
if (!array[i].is_string()) {
250250
return jsonexpr::unexpected(

libjsonexpr/include/jsonexpr/base.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,21 +70,21 @@ std::string_view get_type_name(const T&) noexcept {
7070
return get_type_name<T>();
7171
}
7272

73-
using function_result = expected<json, error>;
74-
using basic_function_result = expected<json, std::string>;
73+
using ast_function_result = expected<json, error>;
74+
using function_result = expected<json, std::string>;
7575

7676
template<typename... Args>
77-
using basic_function_ptr = basic_function_result (*)(const Args&...);
77+
using basic_function_ptr = function_result (*)(const Args&...);
7878

7979
struct function;
8080
using function_registry = std::unordered_map<std::string, function>;
8181

8282
using variable_registry = std::unordered_map<std::string, json>;
8383

8484
struct function {
85-
using ast_function_t = std::function<function_result(
85+
using ast_function_t = std::function<ast_function_result(
8686
std::span<const ast::node>, const variable_registry&, const function_registry&)>;
87-
using basic_function_t = std::function<basic_function_result(std::span<const json>)>;
87+
using basic_function_t = std::function<function_result(std::span<const json>)>;
8888

8989
using overload_t = std::unordered_map<std::string, basic_function_t>;
9090

libjsonexpr/include/jsonexpr/functions.hpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ template<typename... Args>
2525
struct type_list {};
2626

2727
template<typename Function, typename... Args, std::size_t... Indices>
28-
basic_function_result call(
28+
function_result call(
2929
Function func,
3030
std::span<const json> args,
3131
type_list<Args...>,
@@ -45,17 +45,15 @@ concept stateless_lambda = (!function_ptr<T>) && requires(const T& func) {
4545
} // namespace impl
4646

4747
template<typename R, typename... Args>
48-
requires std::is_convertible_v<R, basic_function_result>
48+
requires std::is_convertible_v<R, function_result>
4949
void register_function(function_registry& funcs, std::string_view name, R (*func)(Args...)) {
5050
std::string key;
5151
(impl::add_type(key, get_type_name<std::decay_t<Args>>()), ...);
5252

53-
funcs[std::string{name}].add_overload(
54-
key, [=](std::span<const json> args) -> basic_function_result {
55-
return impl::call(
56-
func, args, impl::type_list<Args...>{},
57-
std::make_index_sequence<sizeof...(Args)>{});
58-
});
53+
funcs[std::string{name}].add_overload(key, [=](std::span<const json> args) -> function_result {
54+
return impl::call(
55+
func, args, impl::type_list<Args...>{}, std::make_index_sequence<sizeof...(Args)>{});
56+
});
5957
}
6058

6159
template<impl::stateless_lambda Func>
@@ -66,7 +64,7 @@ void register_function(function_registry& funcs, std::string_view name, const Fu
6664
JSONEXPR_EXPORT void register_ast_function(
6765
function_registry& funcs,
6866
std::string_view name,
69-
std::function<function_result(
67+
std::function<ast_function_result(
7068
std::span<const ast::node>, const variable_registry&, const function_registry&)> func);
7169

7270
JSONEXPR_EXPORT function_registry default_functions();

libjsonexpr/src/eval.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ expected<json, error> eval(
6868
try {
6969
const auto args = std::span<const ast::node>(f.args.begin(), f.args.end());
7070

71-
auto result = [&]() -> function_result {
71+
auto result = [&]() -> ast_function_result {
7272
if (std::holds_alternative<function::ast_function_t>(func.overloads)) {
7373
return std::get<function::ast_function_t>(func.overloads)(args, vreg, freg);
7474
} else {

0 commit comments

Comments
 (0)