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
17#include <memory>
18#include <optional>
19#include <type_traits>
20
21#if defined( __cpp_aggregate_paren_init )
22#if __cpp_aggregate_paren_init >= 201902L
23#define DAW_HAS_AGG_PAREN_INIT
24#endif
25#endif
26
27namespace daw::json {
28 inline namespace DAW_JSON_VER {
29 namespace concepts {
30 template<typename T>
31 struct nullable_value_traits<std::optional<T>> {
32 using value_type = T;
33 using nullable_type = std::optional<T>;
34 static constexpr bool is_nullable = true;
35
36 static constexpr value_type const &read( nullable_type const &val ) {
37 assert( has_value( val ) );
38 return *val;
39 }
40
41 DAW_JSON_CPP23_STATIC_CALL_OP constexpr nullable_type
42 operator( )( construct_nullable_with_value_t, nullable_type const &opt )
44 noexcept( std::is_nothrow_copy_constructible_v<nullable_type> ) {
45 return opt;
46 }
47
48 DAW_JSON_CPP23_STATIC_CALL_OP constexpr nullable_type
49 operator( )( construct_nullable_with_value_t,
51 noexcept( std::is_nothrow_move_constructible_v<nullable_type> ) {
52 return opt;
53 }
54
55 template<typename... Args DAW_JSON_ENABLEIF(
56 nullable_impl::is_nullable_value_type_constructible_v<value_type,
57 Args...> )>
59 nullable_impl::is_nullable_value_type_constructible_v<value_type,
60 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_JSON_ENABLEIF(
110 nullable_impl::is_nullable_value_type_constructible_v<value_type,
111 Args...> )>
113 nullable_impl::is_nullable_value_type_constructible_v<value_type,
114 Args...> )
116 operator( )( construct_nullable_with_value_t,
118 noexcept( std::is_nothrow_constructible_v<value_type, Args...> ) {
119#if not defined( DAW_HAS_AGG_PAREN_INIT )
120 if constexpr( std::is_aggregate_v<value_type> and
121 nullable_impl::is_list_constructible_v<value_type,
122 Args...> ) {
123 return std::make_unique<value_type>(
124 value_type{ DAW_FWD( args )... } );
125 } else {
126#endif
127 return std::make_unique<value_type>( DAW_FWD( args )... );
128#if not defined( DAW_HAS_AGG_PAREN_INIT )
129 }
130#endif
131 }
132
134 construct_nullable_with_pointer_t,
136 return nullable_type( ptr );
137 }
138
139 constexpr nullable_type DAW_JSON_CPP23_STATIC_CALL_OP
140 operator( )( construct_nullable_with_empty_t )
142 return nullable_type( );
143 }
144
145 static constexpr bool has_value( nullable_type const &val ) {
146 return static_cast<bool>( val );
147 }
148 };
149
150 template<typename T>
151 struct nullable_value_traits<std::shared_ptr<T>> {
152 using value_type = T;
153 using nullable_type = std::shared_ptr<T>;
154 static constexpr bool is_nullable = true;
155
156 static constexpr value_type const &read( nullable_type const &val ) {
157 assert( has_value( val ) );
158 return *val;
159 }
160
161 DAW_JSON_CPP23_STATIC_CALL_OP constexpr nullable_type
162 operator( )( construct_nullable_with_value_t, nullable_type const &opt )
164 noexcept( std::is_nothrow_copy_constructible_v<nullable_type> ) {
165 return opt;
166 }
167
168 DAW_JSON_CPP23_STATIC_CALL_OP constexpr nullable_type
169 operator( )( construct_nullable_with_value_t,
171 noexcept( std::is_nothrow_move_constructible_v<nullable_type> ) {
172 return opt;
173 }
174
175 template<typename... Args DAW_JSON_ENABLEIF(
176 nullable_impl::is_nullable_value_type_constructible_v<value_type,
177 Args...> )>
179 nullable_impl::is_nullable_value_type_constructible_v<value_type,
180 Args...> )
182 operator( )( construct_nullable_with_value_t,
184 noexcept( std::is_nothrow_constructible_v<value_type, Args...> ) {
185#if not defined( DAW_HAS_AGG_PAREN_INIT )
186 if constexpr( std::is_aggregate_v<value_type> and
187 nullable_impl::is_list_constructible_v<value_type,
188 Args...> ) {
189 return std::make_shared<value_type>(
190 value_type{ DAW_FWD( args )... } );
191 } else {
192#endif
193 return std::make_shared<value_type>( DAW_FWD( args )... );
194#if not defined( DAW_HAS_AGG_PAREN_INIT )
195 }
196#endif
197 }
198
200 construct_nullable_with_pointer_t,
202 return nullable_type( ptr );
203 }
204
205 constexpr nullable_type DAW_JSON_CPP23_STATIC_CALL_OP
206 operator( )( construct_nullable_with_empty_t )
208 return nullable_type( );
209 }
210
211 static constexpr bool has_value( nullable_type const &val ) {
212 return static_cast<bool>( val );
213 }
214 };
215
216 template<typename T>
217 struct nullable_value_traits<T *> {
218 using value_type = T;
219 using nullable_type = T *;
220 static constexpr bool is_nullable = true;
221
222 static constexpr value_type const &read( nullable_type const &val ) {
223 assert( has_value( val ) );
224 return *val;
225 }
226
228 construct_nullable_with_value_t,
230 return ptr;
231 }
232
233 template<typename... Args DAW_JSON_ENABLEIF(
234 nullable_impl::is_nullable_value_type_constructible_v<value_type,
235 Args...> )>
237 nullable_impl::is_nullable_value_type_constructible_v<value_type,
238 Args...> )
240 operator( )( construct_nullable_with_value_t,
242 noexcept( std::is_nothrow_constructible_v<value_type, Args...> ) {
243#if not defined( DAW_HAS_AGG_PAREN_INIT )
244 if constexpr( std::is_aggregate_v<T> and
245 nullable_impl::is_list_constructible_v<T, Args...> ) {
246 return new value_type{ DAW_FWD( args )... };
247 } else {
248#endif
249 return new value_type( DAW_FWD( args )... );
250#if not defined( DAW_HAS_AGG_PAREN_INIT )
251 }
252#endif
253 }
254
256 construct_nullable_with_pointer_t,
258 return ptr;
259 }
260
261 DAW_JSON_CPP23_STATIC_CALL_OP constexpr nullable_type
262 operator( )( construct_nullable_with_empty_t )
264 return nullptr;
265 }
266
267 static constexpr bool has_value( nullable_type const &val ) {
268 return static_cast<bool>( val );
269 }
270 };
271 } // namespace concepts
272 } // namespace DAW_JSON_VER
273} // namespace daw::json
274#if defined( DAW_HAS_AGG_PAREN_INIT )
275#undef DAW_HAS_AGG_PAREN_INIT
276#endif
#define DAW_JSON_CPP23_STATIC_CALL_OP_CONST
#define DAW_JSON_ENABLEIF(...)
#define DAW_JSON_CPP23_STATIC_CALL_OP
Customization point traits.
DAW_JSON_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_JSON_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_JSON_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_JSON_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