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