55
66#include < climits>
77#include < cstdint>
8+ #include < stdexcept>
89#include < string>
910#include < string_view>
1011#include < type_traits>
1112
12- #include " ../Bytestring.hpp"
13- #include " ../Timestamp.hpp"
14- #include " ../Vectorstring.hpp"
1513#include " ../always_false.hpp"
1614#include " ../common.hpp"
15+ #include " ../concepts.hpp"
1716#include " ../internal/is_literal.hpp"
18- #include " ../internal/is_validator.hpp"
19- #include " ../patterns.hpp"
2017
2118namespace rfl ::avro {
2219
@@ -52,15 +49,15 @@ class RFL_API Writer {
5249
5350 ~Writer ();
5451
55- OutputArrayType array_as_root (const size_t _size) const noexcept ;
52+ OutputArrayType array_as_root (const size_t _size) const ;
5653
57- OutputMapType map_as_root (const size_t _size) const noexcept ;
54+ OutputMapType map_as_root (const size_t _size) const ;
5855
59- OutputObjectType object_as_root (const size_t _size) const noexcept ;
56+ OutputObjectType object_as_root (const size_t _size) const ;
6057
6158 OutputVarType null_as_root () const ;
6259
63- OutputUnionType union_as_root () const noexcept ;
60+ OutputUnionType union_as_root () const ;
6461
6562 template <class T >
6663 OutputVarType value_as_root (const T& _var) const {
@@ -138,7 +135,7 @@ class RFL_API Writer {
138135 avro_value_t new_value;
139136 int result = avro_value_append (&_parent->val_ , &new_value, nullptr );
140137 if (result != 0 ) {
141- throw std::runtime_error (std::string (__FUNCTION__) + " error(" +
138+ throw std::runtime_error (" Error adding value to array: error(" +
142139 std::to_string (result) +
143140 " ): " + avro_strerror ());
144141 }
@@ -153,7 +150,7 @@ class RFL_API Writer {
153150 int result = avro_value_add (&_parent->val_ , _name.data (), &new_value,
154151 nullptr , nullptr );
155152 if (result != 0 ) {
156- throw std::runtime_error (std::string (__FUNCTION__) + " error(" +
153+ throw std::runtime_error (" Error adding value to map: error(" +
157154 std::to_string (result) +
158155 " ): " + avro_strerror ());
159156 }
@@ -169,7 +166,7 @@ class RFL_API Writer {
169166 int result = avro_value_get_by_name (&_parent->val_ , _name.data (),
170167 &new_value, nullptr );
171168 if (result != 0 ) {
172- throw std::runtime_error (std::string (__FUNCTION__) + " error(" +
169+ throw std::runtime_error (" Error adding value to object: error(" +
173170 std::to_string (result) +
174171 " ): " + avro_strerror ());
175172 }
@@ -181,13 +178,13 @@ class RFL_API Writer {
181178 OutputVarType add_value_to_union (const size_t _index, const T& _var,
182179 OutputUnionType* _parent) const {
183180 if (_index > static_cast <size_t >(INT_MAX)) {
184- throw std::runtime_error (std::string (__FUNCTION__) + " index error" );
181+ throw std::runtime_error (" Error adding value to unions: Index error" );
185182 }
186183 avro_value_t new_value;
187184 int result = avro_value_set_branch (&_parent->val_ , static_cast <int >(_index),
188185 &new_value);
189186 if (result != 0 ) {
190- throw std::runtime_error (std::string (__FUNCTION__) + " error(" +
187+ throw std::runtime_error (" Error adding value to union: error(" +
191188 std::to_string (result) +
192189 " ): " + avro_strerror ());
193190 }
@@ -204,67 +201,79 @@ class RFL_API Writer {
204201 private:
205202 template <class T >
206203 void set_value (const T& _var, avro_value_t * _val) const {
207- if constexpr (std::is_same<std::remove_cvref_t <T>, std::string>()) {
204+ using Type = std::remove_cvref_t <T>;
205+
206+ if constexpr (std::is_same_v<Type, std::string>) {
208207 int result =
209208 avro_value_set_string_len (_val, _var.c_str (), _var.size () + 1 );
210209 if (result != 0 ) {
211- throw std::runtime_error (std::string (__FUNCTION__) + " error( " +
212- std::to_string (result) +
213- " ): " + avro_strerror ());
210+ throw std::runtime_error (
211+ " Error setting string value: " + std::to_string (result) + " : " +
212+ avro_strerror ());
214213 }
215- } else if constexpr (std::is_same<std::remove_cvref_t <T>,
216- rfl::Bytestring>() ||
217- std::is_same<std::remove_cvref_t <T>,
218- rfl::Vectorstring>()) {
214+
215+ } else if constexpr (concepts::MutableContiguousByteContainer<Type>) {
219216 auto var = _var;
220217 if (!var.data ()) {
221218 return ;
222219 }
223220 int result = avro_value_set_bytes (_val, var.data (), var.size ());
224221 if (result != 0 ) {
225- throw std::runtime_error (std::string (__FUNCTION__) + " error( " +
226- std::to_string (result) +
227- " ): " + avro_strerror ());
222+ throw std::runtime_error (
223+ " Error setting bytestring value: " + std::to_string (result) + " : " +
224+ avro_strerror ());
228225 }
229- } else if constexpr (std::is_same<std::remove_cvref_t <T>, bool >()) {
226+
227+ } else if constexpr (std::is_same_v<Type, bool >) {
230228 int result = avro_value_set_boolean (_val, _var);
231229 if (result != 0 ) {
232- throw std::runtime_error (std::string (__FUNCTION__) + " error( " +
233- std::to_string (result) +
234- " ): " + avro_strerror ());
230+ throw std::runtime_error (
231+ " Error setting boolean value: " + std::to_string (result) + " : " +
232+ avro_strerror ());
235233 }
236- } else if constexpr (std::is_floating_point<std::remove_cvref_t <T>>()) {
237- /* int result = avro_value_set_double(_val, static_cast<double>(_var));
234+
235+ } else if constexpr (std::is_same_v<Type, float >) {
236+ int result = avro_value_set_float (_val, static_cast <float >(_var));
238237 if (result != 0 ) {
239- throw std::runtime_error(std::string(__FUNCTION__) + " error("+
240- std::to_string(result)+"): " + avro_strerror());
241- }*/
242- avro_value_set_double (_val, static_cast <double >(_var));
243- } else if constexpr (std::is_integral<std::remove_cvref_t <T>>()) {
244- /* int result = avro_value_set_long(_val, static_cast<std::int64_t>(_var));
238+ throw std::runtime_error (
239+ " Error setting float value: " + std::to_string (result) + " : " +
240+ avro_strerror ());
241+ }
242+
243+ } else if constexpr (std::is_floating_point_v<Type>) {
244+ int result = avro_value_set_double (_val, static_cast <double >(_var));
245245 if (result != 0 ) {
246- throw std::runtime_error(std::string(__FUNCTION__) + " error("+
247- std::to_string(result)+"): " + avro_strerror());
248- }*/
249- avro_value_set_long (_val, static_cast <std::int64_t >(_var));
246+ throw std::runtime_error (
247+ " Error setting double value: " + std::to_string (result) + " : " +
248+ avro_strerror ());
249+ }
250+
251+ } else if constexpr (std::is_same_v<Type, std::int64_t > ||
252+ std::is_same_v<Type, std::uint32_t > ||
253+ std::is_same_v<Type, std::uint64_t >) {
254+ int result = avro_value_set_long (_val, static_cast <std::int64_t >(_var));
255+ if (result != 0 ) {
256+ throw std::runtime_error (
257+ " Error setting long value: " + std::to_string (result) + " : " +
258+ avro_strerror ());
259+ }
260+
261+ } else if constexpr (std::is_integral_v<Type>) {
262+ int result = avro_value_set_int (_val, static_cast <std::int32_t >(_var));
263+ if (result != 0 ) {
264+ throw std::runtime_error (
265+ " Error setting int value: " + std::to_string (result) + " : " +
266+ avro_strerror ());
267+ }
268+
250269 } else if constexpr (internal::is_literal_v<T>) {
251270 int result = avro_value_set_enum (_val, static_cast <int >(_var.value ()));
252271 if (result != 0 ) {
253- throw std::runtime_error (std::string (__FUNCTION__) + " error( " +
254- std::to_string (result) +
255- " ): " + avro_strerror ());
272+ throw std::runtime_error (
273+ " Error setting literal value: " + std::to_string (result) + " : " +
274+ avro_strerror ());
256275 }
257- } else if constexpr (internal::is_validator_v<T>) {
258- using ValueType = std::remove_cvref_t <typename T::ReflectionType>;
259- const auto val = _var.value ();
260- set_value<ValueType>(val, _val);
261- } else if constexpr (std::is_same<std::remove_cvref_t <T>,
262- rfl::Timestamp<" %Y-%m-%d" >>()) {
263- const auto str = _var.to_string ();
264- set_value<std::string>(str, _val);
265- } else if constexpr (std::is_same<std::remove_cvref_t <T>, rfl::Email>()) {
266- const auto & str = static_cast <const std::string&>(_var);
267- set_value<std::string>(str, _val);
276+
268277 } else {
269278 static_assert (rfl::always_false_v<T>, " Unsupported type." );
270279 }
0 commit comments