77#include < string_view>
88#include < type_traits>
99#include < vector>
10+ #include < yas/binary_oarchive.hpp>
11+ #include < yas/mem_streams.hpp>
1012
1113#include " ../Bytestring.hpp"
1214#include " ../Result.hpp"
1315#include " ../Vectorstring.hpp"
1416#include " ../always_false.hpp"
1517#include " ../internal/is_literal.hpp"
18+ #include " ../parsing/schemaful/IsSchemafulWriter.hpp"
1619
1720namespace rfl ::yas {
1821
19- template <class OArchive >
2022struct Writer {
23+ using OArchive = ::yas::binary_oarchive<::yas::mem_ostream>;
24+
2125 struct OutputArrayType {
2226 OArchive* ar;
2327 };
@@ -57,15 +61,20 @@ struct Writer {
5761 return OutputObjectType{ar_};
5862 }
5963
60- OutputUnionType union_as_root () const noexcept { return OutputUnionType{ar_}; }
64+ OutputUnionType union_as_root () const noexcept {
65+ return OutputUnionType{ar_};
66+ }
67+
68+ OutputVarType null_as_root () const noexcept { return OutputVarType{ar_}; }
6169
6270 template <class T >
63- void value_as_root (const T& _var) const noexcept {
71+ OutputVarType value_as_root (const T& _var) const noexcept {
6472 if constexpr (internal::is_literal_v<T>) {
6573 (*ar_) & _var.to_string ();
6674 } else {
6775 (*ar_) & _var;
6876 }
77+ return OutputVarType{ar_};
6978 }
7079
7180 OutputArrayType add_array_to_array (const size_t _size,
@@ -74,9 +83,24 @@ struct Writer {
7483 return OutputArrayType{_parent->ar };
7584 }
7685
77- OutputArrayType add_array_to_object (const std::string_view& _name,
78- const size_t _size,
79- OutputObjectType* _parent) const noexcept {
86+ OutputArrayType add_array_to_map (const std::string_view& _name,
87+ const size_t _size,
88+ OutputMapType* _parent) const noexcept {
89+ (*_parent->ar ) & std::string (_name);
90+ (*_parent->ar ) & _size;
91+ return OutputArrayType{_parent->ar };
92+ }
93+
94+ OutputArrayType add_array_to_object (
95+ const std::string_view& _name, const size_t _size,
96+ OutputObjectType* _parent) const noexcept {
97+ (*_parent->ar ) & _size;
98+ return OutputArrayType{_parent->ar };
99+ }
100+
101+ OutputArrayType add_array_to_union (const size_t _index, const size_t _size,
102+ OutputUnionType* _parent) const noexcept {
103+ (*_parent->ar ) & _index;
80104 (*_parent->ar ) & _size;
81105 return OutputArrayType{_parent->ar };
82106 }
@@ -94,76 +118,140 @@ struct Writer {
94118 return OutputMapType{_parent->ar };
95119 }
96120
121+ OutputMapType add_map_to_map (const std::string_view& _name,
122+ const size_t _size,
123+ OutputMapType* _parent) const noexcept {
124+ (*_parent->ar ) & std::string (_name);
125+ (*_parent->ar ) & _size;
126+ return OutputMapType{_parent->ar };
127+ }
128+
129+ OutputMapType add_map_to_union (const size_t _index, const size_t _size,
130+ OutputUnionType* _parent) const noexcept {
131+ (*_parent->ar ) & _index;
132+ (*_parent->ar ) & _size;
133+ return OutputMapType{_parent->ar };
134+ }
135+
97136 OutputObjectType add_object_to_array (
98137 const size_t _size, OutputArrayType* _parent) const noexcept {
99138 return OutputObjectType{_parent->ar };
100139 }
101140
141+ OutputObjectType add_object_to_map (const std::string_view& _name,
142+ const size_t _size,
143+ OutputMapType* _parent) const noexcept {
144+ (*_parent->ar ) & std::string (_name);
145+ (*_parent->ar ) & _size;
146+ return OutputObjectType{_parent->ar };
147+ }
148+
102149 OutputObjectType add_object_to_object (
103150 const std::string_view& _name, const size_t _size,
104151 OutputObjectType* _parent) const noexcept {
105152 return OutputObjectType{_parent->ar };
106153 }
107154
155+ OutputObjectType add_object_to_union (
156+ const size_t _index, const size_t _size,
157+ OutputUnionType* _parent) const noexcept {
158+ (*_parent->ar ) & _index;
159+ return OutputObjectType{_parent->ar };
160+ }
161+
108162 OutputUnionType add_union_to_array (OutputArrayType* _parent) const noexcept {
109163 return OutputUnionType{_parent->ar };
110164 }
111165
112- OutputUnionType add_union_to_object (const std::string_view& _name,
113- OutputObjectType* _parent) const noexcept {
166+ OutputUnionType add_union_to_map (const std::string_view& _name,
167+ OutputMapType* _parent) const noexcept {
168+ (*_parent->ar ) & std::string (_name);
169+ return OutputUnionType{_parent->ar };
170+ }
171+
172+ OutputUnionType add_union_to_object (
173+ const std::string_view& _name, OutputObjectType* _parent) const noexcept {
174+ return OutputUnionType{_parent->ar };
175+ }
176+
177+ OutputUnionType add_union_to_union (const size_t _index,
178+ OutputUnionType* _parent) const noexcept {
179+ (*_parent->ar ) & _index;
114180 return OutputUnionType{_parent->ar };
115181 }
116182
117183 template <class T >
118- void add_value_to_array (const T& _var,
119- OutputArrayType* _parent) const noexcept {
120- if constexpr (internal::is_literal_v<T>) {
121- (*_parent->ar ) & _var.to_string ();
122- } else {
123- (*_parent->ar ) & _var;
124- }
184+ OutputVarType add_value_to_array (const T& _var, OutputArrayType*) const {
185+ add_value (_var);
186+ return OutputVarType{};
125187 }
126188
127189 template <class T >
128- void add_value_to_object (const std::string_view& _name, const T& _var,
129- OutputObjectType* _parent) const noexcept {
130- if constexpr (internal::is_literal_v<T>) {
131- (*_parent->ar ) & _var.to_string ();
132- } else {
133- (*_parent->ar ) & _var;
134- }
190+ OutputVarType add_value_to_map (const std::string_view& _name, const T& _var,
191+ OutputMapType* _parent) const {
192+ add_string_view (_name);
193+ add_value (_var);
194+ return OutputVarType{};
135195 }
136196
137197 template <class T >
138- void add_value_to_map (const std::string_view& _name, const T& _var,
139- OutputMapType* _parent) const noexcept {
140- (*_parent->ar ) & std::string (_name);
141- if constexpr (internal::is_literal_v<T>) {
142- (*_parent->ar ) & _var.to_string ();
143- } else {
144- (*_parent->ar ) & _var;
145- }
198+ OutputVarType add_value_to_object (const std::string_view& _name,
199+ const T& _var, OutputObjectType*) const {
200+ add_value (_var);
201+ return OutputVarType{};
146202 }
147203
148204 template <class T >
149- void write_union (const size_t _index, const T& _var,
150- OutputUnionType* _parent) const noexcept {
151- (*_parent->ar ) & _index;
152- _var.write (*this , _parent);
205+ OutputVarType add_value_to_union (const size_t _index, const T& _var,
206+ OutputUnionType* _parent) const {
207+ (*ar_)(_index);
208+ add_value (_var);
209+ return OutputVarType{};
210+ }
211+
212+ OutputVarType add_null_to_array (OutputArrayType* _parent) const {
213+ return OutputVarType{};
214+ }
215+
216+ OutputVarType add_null_to_map (const std::string_view& _name,
217+ OutputMapType* _parent) const {
218+ return OutputVarType{};
219+ }
220+
221+ OutputVarType add_null_to_object (const std::string_view& _name,
222+ OutputObjectType* _parent) const {
223+ return OutputVarType{};
224+ }
225+
226+ OutputVarType add_null_to_union (const size_t _index,
227+ OutputUnionType* _parent) const {
228+ return OutputVarType{};
229+ }
230+
231+ void end_array (OutputArrayType* _arr) const noexcept {}
232+
233+ void end_map (OutputMapType* _map) const noexcept {}
234+
235+ void end_object (OutputObjectType* _obj) const noexcept {}
236+
237+ void add_string_view (const std::string_view& _str) const {
238+ (*ar_) & std::string (_str);
153239 }
154240
155241 template <class T >
156- void write_value (const T& _var, OutputUnionType* _parent ) const noexcept {
242+ void add_value (const T& _var) const {
157243 if constexpr (internal::is_literal_v<T>) {
158- (*_parent-> ar ) & _var.to_string ();
244+ (*ar_ ) & _var.to_string ();
159245 } else {
160- (*_parent-> ar ) & _var;
246+ (*ar_ ) & _var;
161247 }
162248 }
163249
164250 OArchive* ar_;
165251};
166252
253+ static_assert (parsing::schemaful::IsSchemafulWriter<Writer>);
254+
167255} // namespace rfl::yas
168256
169257#endif
0 commit comments