DAW JSON Link
Loading...
Searching...
No Matches
daw_json_name.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_consteval.h>
16#include <daw/daw_cpp20_concept.h>
17#include <daw/daw_cpp_feature_check.h>
18#include <daw/daw_likely.h>
19#include <daw/daw_string_view.h>
20
21#include <cstddef>
22#include <string_view>
23#include <utility>
24#if defined( DAW_JSON_HAS_REFLECTION )
25#include <meta>
26#endif
27
28namespace daw::json {
29 inline namespace DAW_JSON_VER {
30
31#if defined( DAW_JSON_CNTTP_JSON_NAME )
32 // C++ 20 Non-Type Class Template Arguments
33
38#if defined( DAW_JSON_HAS_REFLECTION )
39 struct json_name {
40 char const *m_data{ };
41 std::size_t m_size{ };
42
43 template<std::size_t N>
44 consteval json_name( char const ( &ptr )[N] ) noexcept
45 : json_name( daw::string_view( ptr, N - 1 ) ) {}
46
47 consteval json_name( daw::string_view sv ) noexcept
48 : m_data( std::define_static_string( sv ) )
49 , m_size( sv.size( ) ) {}
50
51 [[nodiscard]] DAW_ATTRIB_INLINE constexpr
52 operator daw::string_view( ) const noexcept {
53 return { m_data, m_size };
54 }
55
56 // Needed for copy_to_iterator
57 [[nodiscard]] DAW_ATTRIB_RET_NONNULL
58 DAW_ATTRIB_INLINE constexpr char const *
59 begin( ) const noexcept {
60 return m_data;
61 }
62
63 // Needed for copy_to_iterator
64 [[nodiscard]] DAW_ATTRIB_RET_NONNULL
65 DAW_ATTRIB_INLINE constexpr char const *
66 end( ) const noexcept {
67 return m_data + m_size;
68 }
69
70 [[nodiscard]] constexpr std::size_t size( ) const {
71 return m_size;
72 }
73
74 [[nodiscard]] constexpr bool empty( ) const {
75 return m_size == 0;
76 }
77
78 [[nodiscard]] constexpr bool
79 operator==( json_name const &rhs ) const noexcept {
80 return daw::string_view( m_data, m_size ) ==
81 daw::string_view( rhs.m_data, rhs.m_size );
82 }
83
84 [[nodiscard]] DAW_ATTRIB_INLINE constexpr bool
85 operator==( daw::string_view rhs ) const noexcept {
86 return daw::string_view( m_data, m_size ) == rhs;
87 }
88
89 [[nodiscard]] DAW_ATTRIB_INLINE constexpr
90 operator std::string_view( ) const noexcept {
91 return std::string_view( m_data, m_size );
92 }
93 };
94#else
95 template<std::size_t N>
96 struct json_name {
97 static_assert( N > 0 );
98 char const m_data[N]{ };
99
100 template<std::size_t... Is>
101 DAW_ATTRIB_INLINE DAW_CONSTEVAL json_name(
102 daw::not_null<char const *> ptr, std::index_sequence<Is...> ) noexcept
103 : m_data{ ptr[Is]... } {}
104
105 DAW_ATTRIB_INLINE DAW_CONSTEVAL
106 json_name( char const ( &ptr )[N] ) noexcept
107 : json_name( ptr, std::make_index_sequence<N>{ } ) {}
108
109 [[nodiscard]] DAW_ATTRIB_INLINE constexpr
110 operator daw::string_view( ) const noexcept {
111 return { m_data, N - 1 };
112 }
113
114 // Needed for copy_to_iterator
115 [[nodiscard]] DAW_ATTRIB_RET_NONNULL
116 DAW_ATTRIB_INLINE constexpr char const *
117 begin( ) const noexcept {
118 return m_data;
119 }
120
121 // Needed for copy_to_iterator
122 [[nodiscard]] DAW_ATTRIB_RET_NONNULL
123 DAW_ATTRIB_INLINE constexpr char const *
124 end( ) const noexcept {
125 return m_data + static_cast<ptrdiff_t>( size( ) );
126 }
127
128 [[nodiscard]] static DAW_CONSTEVAL std::size_t size( ) {
129 return N - 1;
130 }
131
132 [[nodiscard]] static DAW_CONSTEVAL bool empty( ) {
133 return size( ) == 0;
134 }
135
136 template<std::size_t M>
137 [[nodiscard]] constexpr bool
138 operator==( json_name<M> const &rhs ) const noexcept {
139 if( N != M ) {
140 return false;
141 }
142 for( std::size_t n = 0; n < N; ++n ) {
143 if( m_data[n] != rhs.m_data[n] ) {
144 return false;
145 }
146 }
147 return true;
148 }
149
150 [[nodiscard]] DAW_ATTRIB_INLINE constexpr bool
151 operator==( daw::string_view rhs ) const noexcept {
152 return daw::string_view( m_data, N - 1 ) == rhs;
153 }
154
155 [[nodiscard]] DAW_ATTRIB_INLINE constexpr bool
156 operator==( std::string_view rhs ) const noexcept {
157 return std::string_view( m_data, N - 1 ) == rhs;
158 }
159
160 [[nodiscard]] DAW_ATTRIB_INLINE constexpr
161 operator std::string_view( ) const noexcept {
162 return std::string_view( m_data, N - 1 );
163 }
164 };
165 template<typename... Chars>
166 json_name( Chars... ) -> json_name<sizeof...( Chars )>;
167
168 template<std::size_t N>
169 json_name( char const ( & )[N] ) -> json_name<N>;
170#endif
171#define JSONNAMETYPE ::daw::json::json_name
172 inline constexpr JSONNAMETYPE default_key_name{ "key" };
173 inline constexpr JSONNAMETYPE default_value_name{ "value" };
174
175#else
176#define JSONNAMETYPE char const *
177 inline constexpr char const default_key_name[] = "key";
178 inline constexpr char const default_value_name[] = "value";
179#endif
180 namespace json_details {
181 DAW_JSON_MAKE_REQ_TRAIT( has_name_v, T::name );
182
183 template<typename... Ts>
184 DAW_CPP20_CONCEPT all_have_name_v = ( has_name_v<Ts> and ... );
185
186 template<typename T>
187 DAW_CPP20_CONCEPT is_no_name_v = not has_name_v<T>;
188
189 template<typename... Ts>
190 DAW_CPP20_CONCEPT are_no_name_v = ( is_no_name_v<Ts> and ... );
191 } // namespace json_details
192 } // namespace DAW_JSON_VER
193} // namespace daw::json
#define JSONNAMETYPE
#define DAW_JSON_MAKE_REQ_TRAIT(Name,...)
Customization point traits.
#define DAW_JSON_VER
The version string used in namespace definitions. Must be a valid namespace name.
Definition version.h:20