DAW JSON Link
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 
11 #include "version.h"
12 
13 #include "daw_json_req_helper.h"
14 
15 #include <daw/daw_consteval.h>
16 #include <daw/daw_cpp_feature_check.h>
17 #include <daw/daw_likely.h>
18 #include <daw/daw_string_view.h>
19 
20 #include <cstddef>
21 #include <string_view>
22 #include <utility>
23 
24 namespace daw::json {
25  inline namespace DAW_JSON_VER {
26 
27 #if defined( DAW_JSON_CNTTP_JSON_NAME )
28  // C++ 20 Non-Type Class Template Arguments
29 
34  template<std::size_t N>
35  struct json_name {
36  static_assert( N > 0 );
37  char const m_data[N]{ };
38 
39  public:
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]] DAW_ATTRIB_INLINE constexpr std::size_t size( ) noexcept {
70  return N - 1;
71  }
72 
73  template<std::size_t M>
74  [[nodiscard]] constexpr bool
75  operator==( json_name<M> const &rhs ) const noexcept {
76  if( N != M ) {
77  return false;
78  }
79  for( std::size_t n = 0; n < N; ++n ) {
80  if( m_data[n] != rhs.m_data[n] ) {
81  return false;
82  }
83  }
84  return true;
85  }
86 
87  [[nodiscard]] DAW_ATTRIB_INLINE constexpr bool
88  operator==( daw::string_view rhs ) const noexcept {
89  return daw::string_view( m_data, N - 1 ) == rhs;
90  }
91 
92  [[nodiscard]] DAW_ATTRIB_INLINE constexpr bool
93  operator==( std::string_view rhs ) const noexcept {
94  return std::string_view( m_data, N - 1 ) == rhs;
95  }
96 
97  [[nodiscard]] DAW_ATTRIB_INLINE constexpr
98  operator std::string_view( ) const noexcept {
99  return std::string_view( m_data, N - 1 );
100  }
101  };
102  template<typename... Chars>
103  json_name( Chars... ) -> json_name<sizeof...( Chars )>;
104 
105  template<std::size_t N>
106  json_name( char const ( & )[N] ) -> json_name<N>;
107 
108 #define JSONNAMETYPE ::daw::json::json_name
109  inline constexpr JSONNAMETYPE default_key_name{ "key" };
110  inline constexpr JSONNAMETYPE default_value_name{ "value" };
111 
112 #else
113 #define JSONNAMETYPE char const *
114  inline constexpr char const default_key_name[] = "key";
115  inline constexpr char const default_value_name[] = "value";
116 #endif
117  namespace json_details {
118  DAW_JSON_MAKE_REQ_TRAIT( has_name_v, T::name );
119 
120  template<typename... Ts>
121  inline constexpr bool all_have_name_v = ( has_name_v<Ts> and ... );
122 
123  template<typename T>
124  inline constexpr bool is_no_name_v = not has_name_v<T>;
125 
126  template<typename... Ts>
127  inline constexpr bool are_no_name_v = ( is_no_name_v<Ts> and ... );
128  } // namespace json_details
129  } // namespace DAW_JSON_VER
130 } // namespace daw::json
#define JSONNAMETYPE
#define DAW_JSON_MAKE_REQ_TRAIT(Name,...)
Disable concepts on gcc < 13.3. See https://github.com/beached/daw_json_link/issues/454.
Customization point traits.
#define DAW_JSON_VER
The version string used in namespace definitions. Must be a valid namespace name.
Definition: version.h:25