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
11#include "daw_json_link.h"
12
13#include <daw/daw_function_traits.h>
14
15#include <string_view>
16#include <tuple>
17#include <type_traits>
18
19namespace daw::json {
20 inline namespace DAW_JSON_VER {
21 namespace json_details {
22 template<typename T, typename = void>
23 inline constexpr bool has_call_operator = std::is_function_v<T>;
24
25 template<typename T>
26 inline constexpr bool has_call_operator<
27 T, std::void_t<decltype( &std::remove_reference_t<T>::operator( ) )>> =
28 true;
29
30 template<typename Signature, typename Callable,
31 typename... FromJsonParams>
32 DAW_ATTRIB_INLINE constexpr auto
33 json_apply_impl( Callable &&callable, FromJsonParams &&...params ) {
34 using func_t = typename std::conditional_t<
35 std::is_same_v<use_default, Signature>,
36 daw::traits::identity<daw::func::function_traits<Callable>>,
37 daw::traits::identity<daw::func::function_traits<Signature>>>::type;
38
39 if constexpr( func_t::arity == 1 ) {
40 return DAW_FWD( callable )(
42 typename func_t::template argument<0>::plain_type>(
43 DAW_FWD( params )... ) );
44 } else {
45 return std::apply(
46 DAW_FWD( callable ),
47 daw::json::from_json<typename func_t::plain_params_t>(
48 DAW_FWD( params )... ) );
49 }
50 }
51 } // namespace json_details
52
64 template<typename Signature = use_default, typename String,
65 typename Callable DAW_JSON_ENABLEIF(
66 json_details::has_call_operator<Callable> )>
67 DAW_JSON_REQUIRES( json_details::has_call_operator<Callable> )
68 constexpr auto json_apply( String &&json_doc, Callable &&callable ) {
69 return json_details::json_apply_impl<Signature>( DAW_FWD( callable ),
70 DAW_FWD( json_doc ) );
71 }
72
85 template<typename Signature = use_default, typename String,
86 typename Callable DAW_JSON_ENABLEIF(
87 json_details::has_call_operator<Callable> )>
88 DAW_JSON_REQUIRES( json_details::has_call_operator<Callable> )
89 constexpr auto json_apply( String &&json_doc, std::string_view json_path,
90 Callable &&callable ) {
91 return json_details::json_apply_impl<Signature>(
92 DAW_FWD( callable ), DAW_FWD( json_doc ), json_path );
93 }
94
109 template<typename Signature = use_default, typename String,
110 auto... PolicyFlags,
111 typename Callable DAW_JSON_ENABLEIF(
112 json_details::has_call_operator<Callable> )>
113 DAW_JSON_REQUIRES( json_details::has_call_operator<Callable> )
114 constexpr auto json_apply(
115 String &&json_doc, std::string_view json_path,
116 daw::json::options::parse_flags_t<PolicyFlags...> flags,
117 Callable &&callable ) {
118 return json_details::json_apply_impl<Signature>(
119 DAW_FWD( callable ), DAW_FWD( json_doc ), json_path, flags );
120 }
121
135 template<typename Signature = use_default, typename String,
136 auto... PolicyFlags,
137 typename Callable DAW_JSON_ENABLEIF(
138 json_details::has_call_operator<Callable> )>
139 DAW_JSON_REQUIRES( json_details::has_call_operator<Callable> )
140 constexpr auto json_apply(
141 String &&json_doc,
142 daw::json::options::parse_flags_t<PolicyFlags...> flags,
143 Callable &&callable ) {
144 return json_details::json_apply_impl<Signature>(
145 DAW_FWD( callable ), DAW_FWD( json_doc ), flags );
146 }
147 } // namespace DAW_JSON_VER
148} // namespace daw::json
#define DAW_JSON_ENABLEIF(...)
#define DAW_JSON_REQUIRES(...)
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