1111#include < vector>
1212
1313#include " ../Box.hpp"
14- #include " ../Bytestring.hpp"
1514#include " ../Ref.hpp"
16- // #include "../Result.hpp"
17- #include " ../Vectorstring.hpp"
1815#include " ../always_false.hpp"
19- #include " ../internal/ptr_cast.hpp"
2016#include " ../common.hpp"
17+ #include " ../concepts.hpp"
18+ #include " ../internal/ptr_cast.hpp"
2119
2220namespace rfl {
2321namespace bson {
@@ -79,43 +77,65 @@ class RFL_API Writer {
7977 }
8078
8179 OutputArrayType add_array_to_array (const size_t _size,
82- OutputArrayType* _parent) const noexcept ;
80+ OutputArrayType* _parent) const ;
8381
8482 OutputArrayType add_array_to_object (const std::string_view& _name,
8583 const size_t _size,
86- OutputObjectType* _parent) const noexcept ;
84+ OutputObjectType* _parent) const ;
8785
8886 OutputObjectType add_object_to_array (const size_t _size,
89- OutputArrayType* _parent) const noexcept ;
87+ OutputArrayType* _parent) const ;
9088
91- OutputObjectType add_object_to_object (
92- const std::string_view& _name, const size_t _size,
93- OutputObjectType* _parent) const noexcept ;
89+ OutputObjectType add_object_to_object (const std::string_view& _name,
90+ const size_t _size,
91+ OutputObjectType* _parent) const ;
9492
9593 template <class T >
9694 OutputVarType add_value_to_array (const T& _var,
97- OutputArrayType* _parent) const noexcept {
95+ OutputArrayType* _parent) const {
9896 if constexpr (std::is_same<std::remove_cvref_t <T>, std::string>()) {
99- bson_array_builder_append_utf8 (_parent->val_ , _var.c_str (),
100- static_cast <int >(_var.size ()));
101- } else if constexpr (std::is_same<std::remove_cvref_t <T>,
102- rfl::Bytestring>() ||
103- std::is_same<std::remove_cvref_t <T>,
104- rfl::Vectorstring>()) {
105- bson_array_builder_append_binary (
97+ const bool ok = bson_array_builder_append_utf8 (
98+ _parent->val_ , _var.c_str (), static_cast <int >(_var.size ()));
99+ if (!ok) {
100+ throw std::runtime_error (" Could not append utf-8 to array." );
101+ }
102+
103+ } else if constexpr (concepts::MutableContiguousByteContainer<
104+ std::remove_cvref_t <T>>) {
105+ const bool ok = bson_array_builder_append_binary (
106106 _parent->val_ , BSON_SUBTYPE_BINARY,
107107 internal::ptr_cast<const uint8_t *>(_var.data ()),
108108 static_cast <uint32_t >(_var.size ()));
109+ if (!ok) {
110+ throw std::runtime_error (" Could not append binary to array." );
111+ }
112+
109113 } else if constexpr (std::is_same<std::remove_cvref_t <T>, bool >()) {
110- bson_array_builder_append_bool (_parent->val_ , _var);
114+ const bool ok = bson_array_builder_append_bool (_parent->val_ , _var);
115+ if (!ok) {
116+ throw std::runtime_error (" Could not append bool to array." );
117+ }
118+
111119 } else if constexpr (std::is_floating_point<std::remove_cvref_t <T>>()) {
112- bson_array_builder_append_double (_parent->val_ ,
113- static_cast <double >(_var));
120+ const bool ok = bson_array_builder_append_double (
121+ _parent->val_ , static_cast <double >(_var));
122+ if (!ok) {
123+ throw std::runtime_error (" Could not append float to array." );
124+ }
125+
114126 } else if constexpr (std::is_integral<std::remove_cvref_t <T>>()) {
115- bson_array_builder_append_int64 (_parent->val_ ,
116- static_cast <std::int64_t >(_var));
127+ const bool ok = bson_array_builder_append_int64 (
128+ _parent->val_ , static_cast <std::int64_t >(_var));
129+ if (!ok) {
130+ throw std::runtime_error (" Could not append integer to array." );
131+ }
132+
117133 } else if constexpr (std::is_same<std::remove_cvref_t <T>, bson_oid_t >()) {
118- bson_array_builder_append_oid (_parent->val_ , &_var);
134+ const bool ok = bson_array_builder_append_oid (_parent->val_ , &_var);
135+ if (!ok) {
136+ throw std::runtime_error (" Could not append OID to array." );
137+ }
138+
119139 } else {
120140 static_assert (rfl::always_false_v<T>, " Unsupported type." );
121141 }
@@ -125,47 +145,75 @@ class RFL_API Writer {
125145 template <class T >
126146 OutputVarType add_value_to_object (const std::string_view& _name,
127147 const T& _var,
128- OutputObjectType* _parent) const noexcept {
148+ OutputObjectType* _parent) const {
129149 if constexpr (std::is_same<std::remove_cvref_t <T>, std::string>()) {
130- bson_append_utf8 (_parent->val_ , _name.data (),
131- static_cast <int >(_name.size ()), _var.c_str (),
132- static_cast <int >(_var.size ()));
133- } else if constexpr (std::is_same<std::remove_cvref_t <T>,
134- rfl::Bytestring>() ||
135- std::is_same<std::remove_cvref_t <T>,
136- rfl::Vectorstring>()) {
137- bson_append_binary (_parent->val_ , _name.data (),
138- static_cast <int >(_name.size ()), BSON_SUBTYPE_BINARY,
139- internal::ptr_cast<const uint8_t *>(_var.data ()),
140- static_cast <uint32_t >(_var.size ()));
150+ const bool ok = bson_append_utf8 (
151+ _parent->val_ , _name.data (), static_cast <int >(_name.size ()),
152+ _var.c_str (), static_cast <int >(_var.size ()));
153+ if (!ok) {
154+ throw std::runtime_error (" Could not utf-8 field '" +
155+ std::string (_name) + " ' to object." );
156+ }
157+
158+ } else if constexpr (concepts::MutableContiguousByteContainer<
159+ std::remove_cvref_t <T>>) {
160+ const bool ok = bson_append_binary (
161+ _parent->val_ , _name.data (), static_cast <int >(_name.size ()),
162+ BSON_SUBTYPE_BINARY, internal::ptr_cast<const uint8_t *>(_var.data ()),
163+ static_cast <uint32_t >(_var.size ()));
164+ if (!ok) {
165+ throw std::runtime_error (" Could not binary field '" +
166+ std::string (_name) + " ' to object." );
167+ }
168+
141169 } else if constexpr (std::is_same<std::remove_cvref_t <T>, bool >()) {
142- bson_append_bool (_parent->val_ , _name.data (),
143- static_cast <int >(_name.size ()), _var);
170+ const bool ok = bson_append_bool (_parent->val_ , _name.data (),
171+ static_cast <int >(_name.size ()), _var);
172+ if (!ok) {
173+ throw std::runtime_error (" Could not boolean field '" +
174+ std::string (_name) + " ' to object." );
175+ }
176+
144177 } else if constexpr (std::is_floating_point<std::remove_cvref_t <T>>()) {
145- bson_append_double (_parent->val_ , _name.data (),
146- static_cast <int >(_name.size ()),
147- static_cast <double >(_var));
178+ const bool ok = bson_append_double (_parent->val_ , _name.data (),
179+ static_cast <int >(_name.size ()),
180+ static_cast <double >(_var));
181+ if (!ok) {
182+ throw std::runtime_error (" Could not floating point field '" +
183+ std::string (_name) + " ' to object." );
184+ }
185+
148186 } else if constexpr (std::is_integral<std::remove_cvref_t <T>>()) {
149- bson_append_int64 (_parent->val_ , _name.data (),
150- static_cast <int >(_name.size ()),
151- static_cast <std::int64_t >(_var));
187+ const bool ok = bson_append_int64 (_parent->val_ , _name.data (),
188+ static_cast <int >(_name.size ()),
189+ static_cast <std::int64_t >(_var));
190+ if (!ok) {
191+ throw std::runtime_error (" Could not int field '" + std::string (_name) +
192+ " ' to object." );
193+ }
194+
152195 } else if constexpr (std::is_same<std::remove_cvref_t <T>, bson_oid_t >()) {
153- bson_append_oid (_parent->val_ , _name.data (),
154- static_cast <int >(_name.size ()), &_var);
196+ const bool ok = bson_append_oid (_parent->val_ , _name.data (),
197+ static_cast <int >(_name.size ()), &_var);
198+ if (!ok) {
199+ throw std::runtime_error (" Could not oid field '" + std::string (_name) +
200+ " ' to object." );
201+ }
202+
155203 } else {
156204 static_assert (rfl::always_false_v<T>, " Unsupported type." );
157205 }
158206 return OutputVarType{};
159207 }
160208
161- OutputVarType add_null_to_array (OutputArrayType* _parent) const noexcept ;
209+ OutputVarType add_null_to_array (OutputArrayType* _parent) const ;
162210
163211 OutputVarType add_null_to_object (const std::string_view& _name,
164- OutputObjectType* _parent) const noexcept ;
212+ OutputObjectType* _parent) const ;
165213
166- void end_array (OutputArrayType* _arr) const noexcept ;
214+ void end_array (OutputArrayType* _arr) const ;
167215
168- void end_object (OutputObjectType* _obj) const noexcept ;
216+ void end_object (OutputObjectType* _obj) const ;
169217
170218 private:
171219 // / Pointer to the main document. In BSON, documents are what are usually
0 commit comments