@@ -21,7 +21,9 @@ namespace bson {
21
21
22
22
// / Please refer to https://mongoc.org/libbson/current/api.html
23
23
class Writer {
24
- using ParentType = std::variant<bson_t *, bson_array_builder_t *>;
24
+ struct IsRoot {};
25
+
26
+ using ParentType = std::variant<bson_t *, bson_array_builder_t *, IsRoot>;
25
27
26
28
public:
27
29
struct BSONOutputArray {
@@ -44,22 +46,32 @@ class Writer {
44
46
using OutputObjectType = BSONOutputObject;
45
47
using OutputVarType = BSONOutputVar;
46
48
47
- Writer () : { bson_init (&doc_); }
49
+ Writer () : {}
48
50
49
51
~Writer () = default ;
50
52
51
- // TODO
52
- OutputArrayType array_as_root (const size_t _size) const noexcept {}
53
+ OutputArrayType array_as_root (const size_t _size) const noexcept {
54
+ bson_array_builder_t * val = bson_array_builder_new ();
55
+ return OutputObjectType (val, IsRoot{});
56
+ }
53
57
54
- // TODO
55
- OutputObjectType object_as_root (const size_t _size) const noexcept {}
58
+ OutputObjectType object_as_root (const size_t _size) const noexcept {
59
+ bson_t val;
60
+ bson_init (&val);
61
+ return OutputObjectType (val, IsRoot{});
62
+ }
56
63
57
- // TODO
58
- OutputVarType null_as_root () const noexcept {}
64
+ OutputVarType null_as_root () const noexcept {
65
+ // Appears to be unsupported by the BSON C API.
66
+ return OutputVarType{};
67
+ }
59
68
60
- // TODO
61
69
template <class T >
62
- OutputVarType value_as_root (const T& _var) const noexcept {}
70
+ OutputVarType value_as_root (const T& _var) const noexcept {
71
+ static_assert (rfl::always_false_v<T>,
72
+ " BSON only allows arrays or objects as its root." );
73
+ return OutputVarType{};
74
+ }
63
75
64
76
OutputArrayType add_array_to_array (const size_t _size,
65
77
OutputArrayType* _parent) const noexcept {
@@ -154,8 +166,12 @@ class Writer {
154
166
using Type = std::remove_cvref_t <decltype (_parent)>;
155
167
if constexpr (std::is_same<Type, bson_array_builder_t *>()) {
156
168
bson_array_builder_append_array_builder_end (_parent, _arr->val_ );
157
- } else {
169
+ } else if constexpr (std::is_same<Tupe, bson_t >()) {
158
170
bson_append_array_end (_parent, _arr->val_ );
171
+ } else if constexpr (std::is_same<Type, IsRoot>()) {
172
+ bson_array_builder_build (_arr->val_ , doc_);
173
+ } else {
174
+ static_assert (rfl::always_false_v<Type>, " Unsupported type." );
159
175
}
160
176
};
161
177
std::visit (handle, _arr->parent_ );
@@ -166,14 +182,18 @@ class Writer {
166
182
using Type = std::remove_cvref_t <decltype (_parent)>;
167
183
if constexpr (std::is_same<Type, bson_array_builder_t *>()) {
168
184
bson_array_builder_append_document_end (_parent, _arr->val_ );
169
- } else {
185
+ } else if constexpr (std::is_same<Tupe, bson_t >()) {
170
186
bson_append_document_end (_parent, _arr->val_ );
187
+ } else if constexpr (std::is_same<Type, IsRoot>()) {
188
+ doc_ = _obj->val_ ;
189
+ } else {
190
+ static_assert (rfl::always_false_v<Type>, " Unsupported type." );
171
191
}
172
192
};
173
193
std::visit (handle, _arr->parent_ );
174
194
}
175
195
176
- public :
196
+ private :
177
197
bson_t doc_;
178
198
};
179
199
0 commit comments