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/daw_cpp_feature_check.h>
14 #include <daw/daw_likely.h>
15 
16 #include <cstddef>
17 #include <string_view>
18 #include <utility>
19 
20 namespace daw::json {
21  inline namespace DAW_JSON_VER {
22  // If the compiler supports CNTTP types allow for strings in json data
23  // contracts. Both support passing local char const[], but the type is
24  // different. To keep old behaviour when using C++20, define
25  // DAW_USE_CPP17_ABI
26 #if defined( __cpp_nontype_template_parameter_class ) and \
27  not defined( DAW_USE_CPP17_ABI )
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  private:
40  template<std::size_t... Is>
41  constexpr json_name( char const ( &ptr )[N], std::index_sequence<Is...> )
42  : m_data{ ptr[Is]... } {}
43 
44  public:
45  constexpr json_name( char const ( &ptr )[N] )
46  : json_name( ptr, std::make_index_sequence<N>{ } ) {}
47 
48  constexpr operator daw::string_view( ) const {
49  return { m_data, N - 1 };
50  }
51 
52  // Needed for copy_to_iterator
53  [[nodiscard]] constexpr char const *begin( ) const {
54  return m_data;
55  }
56 
57  // Needed for copy_to_iterator
58  [[nodiscard]] constexpr char const *end( ) const {
59  return m_data + static_cast<ptrdiff_t>( size( ) );
60  }
61 
62  [[nodiscard]] static constexpr std::size_t size( ) {
63  return N - 1;
64  }
65 
66  template<std::size_t M>
67  constexpr bool operator==( json_name<M> const &rhs ) const {
68  if( N != M ) {
69  return false;
70  }
71  for( std::size_t n = 0; n < N; ++n ) {
72  if( m_data[n] != rhs.m_data[n] ) {
73  return false;
74  }
75  }
76  return true;
77  }
78 
79  constexpr bool operator==( daw::string_view rhs ) const {
80  return daw::string_view( m_data, N - 1 ) == rhs;
81  }
82 
83  constexpr bool operator==( std::string_view rhs ) const {
84  return std::string_view( m_data, N - 1 ) == rhs;
85  }
86 
87  constexpr operator std::string_view( ) const {
88  return std::string_view( m_data, N - 1 );
89  }
90  };
91  template<typename... Chars>
92  json_name( Chars... ) -> json_name<sizeof...( Chars )>;
93 
94  template<std::size_t N>
95  json_name( char const ( & )[N] ) -> json_name<N>;
96 
97 #define JSONNAMETYPE ::daw::json::json_name
98  // Convenience for array members that are required to be unnamed
99  inline constexpr JSONNAMETYPE no_name{ "\a" };
100 
101  namespace json_details {
102  inline constexpr JSONNAMETYPE default_key_name{ "key" };
103  inline constexpr JSONNAMETYPE default_value_name{ "value" };
104  } // namespace json_details
105 
106 #else
107 #define JSONNAMETYPE char const *
108  // Convenience for array members that are required to be unnamed
109  inline constexpr char const no_name[] = "\a";
110  namespace json_details {
111 
112  inline constexpr char const default_key_name[] = "key";
113  inline constexpr char const default_value_name[] = "value";
114 
115  } // namespace json_details
116 #endif
117  namespace json_details {
118  template<typename JsonMember>
119  using is_no_name = std::bool_constant<JsonMember::name == no_name>;
120 
121  template<typename JsonMember>
123  } // namespace json_details
124  } // namespace DAW_JSON_VER
125 } // namespace daw::json
#define JSONNAMETYPE
Definition: daw_json_name.h:107
constexpr char const default_value_name[]
Definition: daw_json_name.h:113
std::bool_constant< JsonMember::name==no_name > is_no_name
Definition: daw_json_name.h:119
constexpr char const default_key_name[]
Definition: daw_json_name.h:112
constexpr bool is_no_name_v
Definition: daw_json_name.h:122
constexpr char const no_name[]
Definition: daw_json_name.h:109
Definition: daw_from_json.h:22
#define DAW_JSON_VER
Definition: version.h:11