DAW JSON Link
Loading...
Searching...
No Matches
daw_nullable_value.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
14
15#include <daw/daw_cpp_feature_check.h>
16#include <daw/daw_enable_requires.h>
17
18#include <memory>
19#include <optional>
20#include <type_traits>
21
22#if defined( __cpp_aggregate_paren_init )
23#if __cpp_aggregate_paren_init >= 201902L
24#define DAW_HAS_AGG_PAREN_INIT
25#endif
26#endif
27
28namespace daw::json {
29 inline namespace DAW_JSON_VER {
30 namespace concepts {
31 template<typename T>
32 struct nullable_value_traits<std::optional<T>> {
33 using value_type = T;
34 using nullable_type = std::optional<T>;
35 static constexpr bool is_nullable = true;
36
37 static constexpr value_type const &read( nullable_type const &val ) {
38 assert( has_value( val ) );
39 return *val;
40 }
41
42 DAW_JSON_CPP23_STATIC_CALL_OP constexpr nullable_type
43 operator( )( construct_nullable_with_value_t, nullable_type const &opt )
45 noexcept( std::is_nothrow_copy_constructible_v<nullable_type> ) {
46 return opt;
47 }
48
49 DAW_JSON_CPP23_STATIC_CALL_OP constexpr nullable_type
50 operator( )( construct_nullable_with_value_t,
52 noexcept( std::is_nothrow_move_constructible_v<nullable_type> ) {
53 return opt;
54 }
55
56 template<typename... Args DAW_ENABLEIF(
57 nullable_impl::is_nullable_value_type_constructible_v<value_type,
58 Args...> )>
59 DAW_REQUIRES( nullable_impl::is_nullable_value_type_constructible_v<
60 value_type, Args...> )
62 operator( )( construct_nullable_with_value_t,
64 noexcept( std::is_nothrow_constructible_v<value_type, Args...> ) {
65#if not defined( DAW_HAS_AGG_PAREN_INIT )
66 if constexpr( std::is_aggregate_v<value_type> and
67 nullable_impl::is_list_constructible_v<value_type,
68 Args...> ) {
69 return std::optional<value_type>(
70 value_type{ DAW_FWD( args )... } );
71 } else {
72#endif
73 return std::optional<value_type>( std::in_place,
74 DAW_FWD( args )... );
75#if not defined( DAW_HAS_AGG_PAREN_INIT )
76 }
77#endif
78 }
79
80 constexpr nullable_type DAW_JSON_CPP23_STATIC_CALL_OP
81 operator( )( construct_nullable_with_empty_t )
83 return nullable_type( );
84 }
85
86 static constexpr bool has_value( nullable_type const &val ) {
87 return val.has_value( );
88 }
89 };
90
91 template<typename T>
92 struct nullable_value_traits<std::unique_ptr<T>> {
93 using value_type = T;
94 using nullable_type = std::unique_ptr<T>;
95 static constexpr bool is_nullable = true;
96
97 static constexpr value_type const &read( nullable_type const &val ) {
98 assert( has_value( val ) );
99 return *val;
100 }
101
102 DAW_JSON_CPP23_STATIC_CALL_OP constexpr nullable_type
103 operator( )( construct_nullable_with_value_t,
105 noexcept( std::is_nothrow_move_constructible_v<nullable_type> ) {
106 return opt;
107 }
108
109 template<typename... Args DAW_ENABLEIF(
110 nullable_impl::is_nullable_value_type_constructible_v<value_type,
111 Args...> )>
112 DAW_REQUIRES( nullable_impl::is_nullable_value_type_constructible_v<
113 value_type, Args...> )
115 operator( )( construct_nullable_with_value_t,
117 noexcept( std::is_nothrow_constructible_v<value_type, Args...> ) {
118#if not defined( DAW_HAS_AGG_PAREN_INIT )
119 if constexpr( std::is_aggregate_v<value_type> and
120 nullable_impl::is_list_constructible_v<value_type,
121 Args...> ) {
122 return std::make_unique<value_type>(
123 value_type{ DAW_FWD( args )... } );
124 } else {
125#endif
126 return std::make_unique<value_type>( DAW_FWD( args )... );
127#if not defined( DAW_HAS_AGG_PAREN_INIT )
128 }
129#endif
130 }
131
133 construct_nullable_with_pointer_t,
135 return nullable_type( ptr );
136 }
137
138 constexpr nullable_type DAW_JSON_CPP23_STATIC_CALL_OP
139 operator( )( construct_nullable_with_empty_t )
141 return nullable_type( );
142 }
143
144 static constexpr bool has_value( nullable_type const &val ) {
145 return static_cast<bool>( val );
146 }
147 };
148
149 template<typename T>
150 struct nullable_value_traits<std::shared_ptr<T>> {
151 using value_type = T;
152 using nullable_type = std::shared_ptr<T>;
153 static constexpr bool is_nullable = true;
154
155 static constexpr value_type const &read( nullable_type const &val ) {
156 assert( has_value( val ) );
157 return *val;
158 }
159
160 DAW_JSON_CPP23_STATIC_CALL_OP constexpr nullable_type
161 operator( )( construct_nullable_with_value_t, nullable_type const &opt )
163 noexcept( std::is_nothrow_copy_constructible_v<nullable_type> ) {
164 return opt;
165 }
166
167 DAW_JSON_CPP23_STATIC_CALL_OP constexpr nullable_type
168 operator( )( construct_nullable_with_value_t,
170 noexcept( std::is_nothrow_move_constructible_v<nullable_type> ) {
171 return opt;
172 }
173
174 template<typename... Args DAW_ENABLEIF(
175 nullable_impl::is_nullable_value_type_constructible_v<value_type,
176 Args...> )>
177 DAW_REQUIRES( nullable_impl::is_nullable_value_type_constructible_v<
178 value_type, Args...> )
180 operator( )( construct_nullable_with_value_t,
182 noexcept( std::is_nothrow_constructible_v<value_type, Args...> ) {
183#if not defined( DAW_HAS_AGG_PAREN_INIT )
184 if constexpr( std::is_aggregate_v<value_type> and
185 nullable_impl::is_list_constructible_v<value_type,
186 Args...> ) {
187 return std::make_shared<value_type>(
188 value_type{ DAW_FWD( args )... } );
189 } else {
190#endif
191 return std::make_shared<value_type>( DAW_FWD( args )... );
192#if not defined( DAW_HAS_AGG_PAREN_INIT )
193 }
194#endif
195 }
196
198 construct_nullable_with_pointer_t,
200 return nullable_type( ptr );
201 }
202
203 constexpr nullable_type DAW_JSON_CPP23_STATIC_CALL_OP
204 operator( )( construct_nullable_with_empty_t )
206 return nullable_type( );
207 }
208
209 static constexpr bool has_value( nullable_type const &val ) {
210 return static_cast<bool>( val );
211 }
212 };
213
214 template<typename T>
215 struct nullable_value_traits<T *> {
216 using value_type = T;
217 using nullable_type = T *;
218 static constexpr bool is_nullable = true;
219
220 static constexpr value_type const &read( nullable_type const &val ) {
221 assert( has_value( val ) );
222 return *val;
223 }
224
226 construct_nullable_with_value_t,
228 return ptr;
229 }
230
231 template<typename... Args DAW_ENABLEIF(
232 nullable_impl::is_nullable_value_type_constructible_v<value_type,
233 Args...> )>
234 DAW_REQUIRES( nullable_impl::is_nullable_value_type_constructible_v<
235 value_type, Args...> )
237 operator( )( construct_nullable_with_value_t,
239 noexcept( std::is_nothrow_constructible_v<value_type, Args...> ) {
240#if not defined( DAW_HAS_AGG_PAREN_INIT )
241 if constexpr( std::is_aggregate_v<T> and
242 nullable_impl::is_list_constructible_v<T, Args...> ) {
243 return new value_type{ DAW_FWD( args )... };
244 } else {
245#endif
246 return new value_type( DAW_FWD( args )... );
247#if not defined( DAW_HAS_AGG_PAREN_INIT )
248 }
249#endif
250 }
251
253 construct_nullable_with_pointer_t,
255 return ptr;
256 }
257
258 DAW_JSON_CPP23_STATIC_CALL_OP constexpr nullable_type
259 operator( )( construct_nullable_with_empty_t )
261 return nullptr;
262 }
263
264 static constexpr bool has_value( nullable_type const &val ) {
265 return static_cast<bool>( val );
266 }
267 };
268 } // namespace concepts
269 } // namespace DAW_JSON_VER
270} // namespace daw::json
271#if defined( DAW_HAS_AGG_PAREN_INIT )
272#undef DAW_HAS_AGG_PAREN_INIT
273#endif
#define DAW_JSON_CPP23_STATIC_CALL_OP_CONST
#define DAW_JSON_CPP23_STATIC_CALL_OP
Customization point traits.
DAW_REQUIRES(nullable_impl::is_nullable_value_type_constructible_v< value_type, Args... >) DAW_JSON_CPP23_STATIC_CALL_OP const expr nullable_type operator()(const ruct_nullable_with_value_t
DAW_REQUIRES(nullable_impl::is_nullable_value_type_constructible_v< value_type, Args... >) DAW_JSON_CPP23_STATIC_CALL_OP const expr nullable_type operator()(const ruct_nullable_with_value_t
DAW_REQUIRES(nullable_impl::is_nullable_value_type_constructible_v< value_type, Args... >) DAW_JSON_CPP23_STATIC_CALL_OP const expr nullable_type operator()(const ruct_nullable_with_value_t
DAW_REQUIRES(nullable_impl::is_nullable_value_type_constructible_v< value_type, Args... >) DAW_JSON_CPP23_STATIC_CALL_OP const expr nullable_type operator()(const ruct_nullable_with_value_t
#define DAW_JSON_VER
The version string used in namespace definitions. Must be a valid namespace name.
Definition version.h:20