Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add apply*() functions (visitor pattern) #3450

Draft
wants to merge 11 commits into
base: develop
Choose a base branch
from

Conversation

falbrechtskirchinger
Copy link
Contributor

@falbrechtskirchinger falbrechtskirchinger commented Apr 21, 2022

This has been factored out of #3446 but builds upon that PR and needs to be rebased.

Originally, I was looking for a workaround to be able to use std::ranges::sort() with basic_json. std::ranges::sort(j) fails to compile because basic_json doesn't (and can't) satisfy the std:random_access_iterator concept. This PR is the product of this search and adds 3 variants of visitor-style apply*(Fn, Args...) functions to basic_json which invoke a given callable (Fn) with the stored JSON value and any additional arguments (Args...).

(Describe functions, etc.)

To Do:

  • Exceptions are placeholders. Throw appropriate type, maybe re-word messages, and pick (and document) an ID number.
  • Complete unit tests.
  • Update documentation.
  • Look at discussion Generic value from JSON to function #3779. Potential use case?

@coveralls
Copy link

coveralls commented Apr 21, 2022

Coverage Status

Coverage decreased (-0.02%) to 99.983% when pulling 04b4d79 on falbrechtskirchinger:apply into b205361 on nlohmann:develop.

Add 3 variants of apply*() functions that invoke a given callable with
the supplied arguments and the stored JSON value.

* void apply(Fn, Args...):
  ...

* void apply_cb(ResultCallback, Fn, Args...):
  ...

* R apply_r(Fn, Args...):
  ...
Clang <4 fails to compile std::forward_as_tuple() calls involving
basic_json values. As a workaround, the values are wrapped using
std::reference_wrapper.
Lambdas with certain noexcept specifiers are deemed non-invocable at
compile-time, failing at runtime.
The invoke implemenation does not compile on GCC 8.1.0.
apply_invoke overload resolution fails for unknown reason.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants