DAW JSON Link
daw_writable_output_basics.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 <daw/json/impl/version.h>
12 
17 
18 #include <daw/daw_algorithm.h>
19 #include <daw/daw_character_traits.h>
20 #include <daw/daw_string_view.h>
21 
22 #include <cstdio>
23 
24 namespace daw::json {
25  inline namespace DAW_JSON_VER {
26  namespace concepts {
28  template<typename T>
30  writeable_output_details::is_char_sized_character_v<T> or
31  writeable_output_details::is_byte_type_v<T> )
32  struct writable_output_trait<
34  writeable_output_details::is_char_sized_character_v<T> or
35  writeable_output_details::is_byte_type_v<T> )> : std::true_type {
36 
37  template<typename... StringViews>
38  static constexpr void write( T *&ptr, StringViews const &...svs ) {
39  static_assert( sizeof...( StringViews ) > 0 );
40  daw_json_ensure( ptr, daw::json::ErrorReason::OutputError );
41  constexpr auto writer = []( T *&p,
43  if( sv.empty( ) ) {
44  return 0;
45  }
46  p = writeable_output_details::copy_to_buffer( p, sv );
47  return 1;
48  };
49  ( (void)writer( ptr, svs ), ... );
50  }
51 
52  static constexpr void put( T *&ptr, char c ) {
53  daw_json_ensure( ptr, daw::json::ErrorReason::OutputError );
54  *ptr = static_cast<T>( c );
55  ++ptr;
56  }
57  };
58 
60  template<typename T>
61  DAW_JSON_REQUIRES( writeable_output_details::is_span_like_range_v<
62  T, typename T::value_type> )
63  struct writable_output_trait<
64  T DAW_JSON_ENABLEIF_S( writeable_output_details::is_span_like_range_v<
65  T, typename T::value_type> )> : std::true_type {
66  using CharT = typename T::value_type;
67 
68  template<typename... StringViews>
69  static constexpr void write( T &out, StringViews const &...svs ) {
70  static_assert( sizeof...( StringViews ) > 0 );
71  daw_json_ensure( out.size( ) >= ( std::size( svs ) + ... ),
72  daw::json::ErrorReason::OutputError );
73  constexpr auto writer = []( T &s,
75  if( sv.empty( ) ) {
76  return 0;
77  }
78  (void)writeable_output_details::copy_to_buffer( s.data( ), sv );
79  s = s.subspan( sv.size( ) );
80  return 1;
81  };
82  ( (void)writer( out, svs ), ... );
83  }
84 
85  static constexpr void put( T &out, char c ) {
86  daw_json_ensure( not out.empty( ),
87  daw::json::ErrorReason::OutputError );
88  *out.data( ) = static_cast<CharT>( c );
89  out.remove_prefix( 1 );
90  }
91  };
92 
94  template<typename Container>
96  writeable_output_details::is_string_like_writable_output_v<
97  Container, typename Container::value_type> )
98  struct writable_output_trait<Container DAW_JSON_ENABLEIF_S(
99  writeable_output_details::is_string_like_writable_output_v<
100  Container, typename Container::value_type> )> : std::true_type {
101  using CharT = typename Container::value_type;
102 
103  template<typename... StringViews>
104  static inline void write( Container &out, StringViews const &...svs ) {
105  static_assert( sizeof...( StringViews ) > 0 );
106  auto const start_pos = out.size( );
107  auto const total_size = ( std::size( svs ) + ... );
108  out.resize( start_pos + total_size );
109 
110  constexpr auto writer = []( CharT *&p,
112  if( sv.empty( ) ) {
113  return 0;
114  }
115  p = writeable_output_details::copy_to_buffer( p, sv );
116  return 1;
117  };
118  auto *ptr = out.data( ) + start_pos;
119  ( (void)writer( ptr, svs ), ... );
120  }
121 
122  static inline void put( Container &out, char c ) {
123  out.push_back( static_cast<CharT>( c ) );
124  }
125  };
126 
128  template<typename T>
130  writeable_output_details::is_writable_output_iterator_v<T> )
131  struct writable_output_trait<T DAW_JSON_ENABLEIF_S(
132  writeable_output_details::is_writable_output_iterator_v<T> )>
133  : std::true_type {
134 
135  template<typename... StringViews>
136  static constexpr void write( T &it, StringViews const &...svs ) {
137  static_assert( sizeof...( StringViews ) > 0 );
138 
139  constexpr auto writer = []( T &i, auto sv )
141  for( char c : daw::string_view( sv ) ) {
142  *i = c;
143  ++i;
144  }
145  return 0;
146  };
147  ( (void)writer( it, svs ), ... );
148  }
149 
150  static constexpr void put( T &it, char c ) {
151  *it = c;
152  ++it;
153  }
154  };
155  } // namespace concepts
156  } // namespace DAW_JSON_VER
157 } // namespace daw::json
#define daw_json_ensure(Bool,...)
Ensure that Bool is true. If false pass rest of args to daw_json_error.
#define DAW_JSON_ENABLEIF_S(...)
#define DAW_JSON_CPP23_STATIC_CALL_OP
This is in addition to the parse policy. Always do a full name match instead of sometimes relying on ...
Customization point traits.
DAW_JSON_REQUIRES(boost::describe::has_describe_members< T >::value and use_boost_describe_v< T >) struct json_data_contract< T >
#define DAW_JSON_VER
The version string used in namespace definitions. Must be a valid namespace name.
Definition: version.h:25