DAW JSON Link
Loading...
Searching...
No Matches
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
12
17
18#include <daw/daw_algorithm.h>
19#include <daw/daw_character_traits.h>
20#include <daw/daw_enable_requires.h>
21#include <daw/daw_string_view.h>
22
23#include <cstdio>
24
25namespace daw::json {
26 inline namespace DAW_JSON_VER {
27 namespace concepts {
29 template<typename T>
30 DAW_REQUIRES( writeable_output_details::is_char_sized_character_v<T> or
31 writeable_output_details::is_byte_type_v<T> )
32 struct writable_output_trait<
33 T * DAW_ENABLEIF_S(
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 DAW_CPP23_STATIC_LOCAL constexpr auto writer =
42 []( T *&p, auto sv ) DAW_JSON_CPP23_STATIC_CALL_OP {
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_REQUIRES( writeable_output_details::is_span_like_range_v<
62 T, typename T::value_type> )
63 struct writable_output_trait<
64 T DAW_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 DAW_CPP23_STATIC_LOCAL constexpr auto writer =
74 []( T &s, auto sv ) DAW_JSON_CPP23_STATIC_CALL_OP {
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>
95 DAW_REQUIRES( writeable_output_details::is_string_like_writable_output_v<
96 Container, typename Container::value_type> )
97 struct writable_output_trait<Container DAW_ENABLEIF_S(
98 writeable_output_details::is_string_like_writable_output_v<
99 Container, typename Container::value_type> )> : std::true_type {
100 using CharT = typename Container::value_type;
101
102 template<typename... StringViews>
103 static constexpr void write( Container &out,
104 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 DAW_CPP23_STATIC_LOCAL constexpr auto writer =
111 []( CharT *&p, auto sv ) DAW_JSON_CPP23_STATIC_CALL_OP {
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 constexpr void put( Container &out, char c ) {
123 out.push_back( static_cast<CharT>( c ) );
124 }
125 };
126
128 template<typename T>
129 DAW_REQUIRES( writeable_output_details::is_writable_output_iterator_v<T> )
130 struct writable_output_trait<T DAW_ENABLEIF_S(
131 writeable_output_details::is_writable_output_iterator_v<T> )>
132 : std::true_type {
133
134 template<typename... StringViews>
135 static constexpr void write( T &it, StringViews const &...svs ) {
136 static_assert( sizeof...( StringViews ) > 0 );
137
138 DAW_CPP23_STATIC_LOCAL constexpr auto writer =
139 []( T &i, auto sv ) DAW_JSON_CPP23_STATIC_CALL_OP {
140 for( char c : daw::string_view( sv ) ) {
141 *i = c;
142 ++i;
143 }
144 return 0;
145 };
146 ( (void)writer( it, svs ), ... );
147 }
148
149 static constexpr void put( T &it, char c ) {
150 *it = c;
151 ++it;
152 }
153 };
154 } // namespace concepts
155 } // namespace DAW_JSON_VER
156} // namespace daw::json
#define daw_json_ensure(Bool,...)
Ensure that Bool is true. If false pass rest of args to daw_json_error.
DAW_REQUIRES(daw::json::json_details::is_container_opted_into_json_iostreams_v< Container >) std
An opt in ostream interface for containers of types that have JSON mappings.
#define DAW_JSON_CPP23_STATIC_CALL_OP
Customization point traits.
#define DAW_JSON_VER
The version string used in namespace definitions. Must be a valid namespace name.
Definition version.h:20