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
25namespace daw::json {
26 inline namespace DAW_JSON_VER {
27
28#if defined( DAW_JSON_CNTTP_JSON_NAME )
29 // C++ 20 Non-Type Class Template Arguments
30
35 template<std::size_t N>
36 struct json_name {
37 static_assert( N > 0 );
38 char const m_data[N]{ };
39
40 template<std::size_t... Is>
41 DAW_ATTRIB_NONNULL( )
42 DAW_ATTRIB_INLINE DAW_CONSTEVAL
43 json_name( char const *ptr, std::index_sequence<Is...> ) noexcept
44 : m_data{ ptr[Is]... } {}
45
46 DAW_ATTRIB_INLINE DAW_CONSTEVAL
47 json_name( char const ( &ptr )[N] ) noexcept
48 : json_name( ptr, std::make_index_sequence<N>{ } ) {}
49
50 [[nodiscard]] DAW_ATTRIB_INLINE constexpr
51 operator daw::string_view( ) const noexcept {
52 return { m_data, N - 1 };
53 }
54
55 // Needed for copy_to_iterator
56 [[nodiscard]] DAW_ATTRIB_RET_NONNULL
57 DAW_ATTRIB_INLINE constexpr char const *
58 begin( ) const noexcept {
59 return m_data;
60 }
61
62 // Needed for copy_to_iterator
63 [[nodiscard]] DAW_ATTRIB_RET_NONNULL
64 DAW_ATTRIB_INLINE constexpr char const *
65 end( ) const noexcept {
66 return m_data + static_cast<ptrdiff_t>( size( ) );
67 }
68
69 [[nodiscard]] static DAW_CONSTEVAL std::size_t size( ) {
70 return N - 1;
71 }
72
73 [[nodiscard]] static DAW_CONSTEVAL bool empty( ) {
74 return size( ) == 0;
75 }
76
77 template<std::size_t M>
78 [[nodiscard]] constexpr bool
79 operator==( json_name<M> const &rhs ) const noexcept {
80 if( N != M ) {
81 return false;
82 }
83 for( std::size_t n = 0; n < N; ++n ) {
84 if( m_data[n] != rhs.m_data[n] ) {
85 return false;
86 }
87 }
88 return true;
89 }
90
91 [[nodiscard]] DAW_ATTRIB_INLINE constexpr bool
92 operator==( daw::string_view rhs ) const noexcept {
93 return daw::string_view( m_data, N - 1 ) == rhs;
94 }
95
96 [[nodiscard]] DAW_ATTRIB_INLINE constexpr bool
97 operator==( std::string_view rhs ) const noexcept {
98 return std::string_view( m_data, N - 1 ) == rhs;
99 }
100
101 [[nodiscard]] DAW_ATTRIB_INLINE constexpr
102 operator std::string_view( ) const noexcept {
103 return std::string_view( m_data, N - 1 );
104 }
105 };
106 template<typename... Chars>
107 json_name( Chars... ) -> json_name<sizeof...( Chars )>;
108
109 template<std::size_t N>
110 json_name( char const ( & )[N] ) -> json_name<N>;
111
112#define JSONNAMETYPE ::daw::json::json_name
113 inline constexpr JSONNAMETYPE default_key_name{ "key" };
114 inline constexpr JSONNAMETYPE default_value_name{ "value" };
115
116#else
117#define JSONNAMETYPE char const *
118 inline constexpr char const default_key_name[] = "key";
119 inline constexpr char const default_value_name[] = "value";
120#endif
121 namespace json_details {
122 DAW_JSON_MAKE_REQ_TRAIT( has_name_v, T::name );
123
124 template<typename... Ts>
125 DAW_CPP20_CONCEPT all_have_name_v = ( has_name_v<Ts> and ... );
126
127 template<typename T>
128 DAW_CPP20_CONCEPT is_no_name_v = not has_name_v<T>;
129
130 template<typename... Ts>
131 DAW_CPP20_CONCEPT are_no_name_v = ( is_no_name_v<Ts> and ... );
132 } // namespace json_details
133 } // namespace DAW_JSON_VER
134} // 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