Support free function pointers in glz::custom - #2687
Open
stephenberry wants to merge 1 commit into
Open
Conversation
Free functions can now be used with glz::custom, following the same conventions as lambdas: the object is the first argument, the decoded input is the second, and an optional glz::context& third argument enables custom error handling. A non-void return acts as a getter, reading into the returned reference. - invocable_args_t/invocable_result_t now dispatch through invocable_traits_for, which handles function objects via decltype(&T::operator()) and free function pointers directly. The primary template is empty so the aliases stay SFINAE-friendly for other types (e.g. std::function member pointers inspected inside std::enable_if_t). - is_invocable_concrete is unchanged, so registry, cli_menu, and constraint dispatch are unaffected; custom.hpp, custom_meta.hpp, and reflect.hpp opt into function pointers explicitly via is_function_ptr_invocable. - custom read dispatch routes non-void concrete callables to the reference-read path instead of static_asserting, so reference- returning free functions and concrete lambdas work as getters. - custom_read_input_type recognizes free function setters, enabling variant deduction and JSON schema generation for them. Closes #2639
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Resolves #2639
Free functions can now be used with
glz::custom, following the same conventions as lambdas: the object is the first argument, the decoded input is the second, and an optionalglz::context&third argument enables custom error handling. A free function with a non-void return acts as a getter, so reading decodes into the returned reference and writing serializes the returned value.Implementation notes
invocable_args_t/invocable_result_tnow dispatch through a newinvocable_traits_forselector that handles function objects viadecltype(&T::operator())and free function pointers directly. Its primary template is intentionally empty so the aliases remain SFINAE-friendly for other types (e.g.std::functionmember pointers inspected insidestd::enable_if_tconditions), matching the previous alias behavior.is_invocable_concreteis deliberately unchanged (it still means "callable with a concreteoperator()"), so the dispatch chains inregistry.hpp,cli_menu.hpp, andconstraint.hppare unaffected. The places that support free functions (custom.hpp,custom_meta.hpp,reflect.hpp) opt in explicitly withis_function_ptr_invocable.static_assert, so reference-returning free functions (and now concrete lambdas) work as read-side getters.custom_read_input_typerecognizes free function setters, enabling variant type deduction and JSON schema generation for them, on par with lambdas.Tests
New
free_function_custom_testssuite injson_testcovering: basic free read/write, theglz::context&read and write forms with custom error messages, mixing a free function with a member pointer, string inputs including escape round-tripping, and a reference-returning free function. Also verified compile-compatibility for registry member-function endpoints, registry function-pointer endpoints, whole-valuecustomoverstd::functionmembers inside variants and schema generation, andcli_menustructs with multi-argument function pointer members.