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