DAW JSON Link
Loading...
Searching...
No Matches
daw_json_apply.h
Go to the documentation of this file.
1// Copyright (c) Darrell Wright
2//
3// Distributed under the Boost Software License, Version 1.0. (See accompanying
4// file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)
5//
6// Official repository: https://github.com/beached/daw_json_link
7//
8
9#pragma once
10
12
13#include <daw/daw_cpp20_concept.h>
14#include <daw/daw_cpp_feature_check.h>
15#include <daw/daw_enable_requires.h>
16#include <daw/daw_function_traits.h>
17
18#include <string_view>
19#include <tuple>
20#include <type_traits>
21
22namespace daw::json {
23 inline namespace DAW_JSON_VER {
24 namespace json_details {
25#if defined( DAW_HAS_CPP20_CONCEPTS )
26 template<typename T>
27 concept has_call_operator = std::is_function_v<T> or requires {
28 &std::remove_reference_t<T>::operator( );
29 };
30#else
31 template<typename T, typename = void>
32 inline constexpr bool has_call_operator = std::is_function_v<T>;
33
34 template<typename T>
35 inline constexpr bool has_call_operator<
36 T, std::void_t<decltype( &std::remove_reference_t<T>::operator( ) )>> =
37 true;
38#endif
39 template<typename Signature, typename Callable,
40 typename... FromJsonParams>
41 DAW_ATTRIB_INLINE constexpr auto
42 json_apply_impl( Callable &&callable, FromJsonParams &&...params ) {
43 using func_t = typename std::conditional_t<
44 std::is_same_v<use_default, Signature>,
45 daw::traits::identity<daw::func::function_traits<Callable>>,
46 daw::traits::identity<daw::func::function_traits<Signature>>>::type;
47
48 if constexpr( func_t::arity == 1 ) {
49 return DAW_FWD( callable )(
51 typename func_t::template argument<0>::plain_type>(
52 DAW_FWD( params )... ) );
53 } else {
54 return std::apply(
55 DAW_FWD( callable ),
56 daw::json::from_json<typename func_t::plain_params_t>(
57 DAW_FWD( params )... ) );
58 }
59 }
60 } // namespace json_details
61
73 template<typename Signature = use_default, typename String,
74 typename Callable DAW_ENABLEIF(
75 json_details::has_call_operator<Callable> )>
76 DAW_REQUIRES( json_details::has_call_operator<Callable> )
77 constexpr auto json_apply( String &&json_doc, Callable &&callable ) {
78 return json_details::json_apply_impl<Signature>( DAW_FWD( callable ),
79 DAW_FWD( json_doc ) );
80 }
81
94 template<typename Signature = use_default, typename String,
95 typename Callable DAW_ENABLEIF(
96 json_details::has_call_operator<Callable> )>
97 DAW_REQUIRES( json_details::has_call_operator<Callable> )
98 constexpr auto json_apply( String &&json_doc, std::string_view json_path,
99 Callable &&callable ) {
100 return json_details::json_apply_impl<Signature>(
101 DAW_FWD( callable ), DAW_FWD( json_doc ), json_path );
102 }
103
118 template<typename Signature = use_default, typename String,
119 auto... PolicyFlags,
120 typename Callable DAW_ENABLEIF(
121 json_details::has_call_operator<Callable> )>
122 DAW_REQUIRES( json_details::has_call_operator<Callable> )
123 constexpr auto json_apply(
124 String &&json_doc, std::string_view json_path,
125 daw::json::options::parse_flags_t<PolicyFlags...> flags,
126 Callable &&callable ) {
127 return json_details::json_apply_impl<Signature>(
128 DAW_FWD( callable ), DAW_FWD( json_doc ), json_path, flags );
129 }
130
144 template<typename Signature = use_default, typename String,
145 auto... PolicyFlags,
146 typename Callable DAW_ENABLEIF(
147 json_details::has_call_operator<Callable> )>
148 DAW_REQUIRES( json_details::has_call_operator<Callable> )
149 constexpr auto json_apply(
150 String &&json_doc,
151 daw::json::options::parse_flags_t<PolicyFlags...> flags,
152 Callable &&callable ) {
153 return json_details::json_apply_impl<Signature>(
154 DAW_FWD( callable ), DAW_FWD( json_doc ), flags );
155 }
156 } // namespace DAW_JSON_VER
157} // namespace daw::json
DAW_REQUIRES(daw::json::json_details::is_container_opted_into_json_iostreams_v< Container >) std
An opt in ostream interface for containers of types that have JSON mappings.
constexpr auto from_json(String &&json_data, options::parse_flags_t< PolicyFlags... >)
Construct the JSONMember from the JSON document argument.
Customization point traits.
#define DAW_JSON_VER
The version string used in namespace definitions. Must be a valid namespace name.
Definition version.h:20