Skip to content

Commit

Permalink
Added functions for writing root
Browse files Browse the repository at this point in the history
  • Loading branch information
liuzicheng1987 committed Jan 13, 2024
1 parent ee3981d commit 1d0c37f
Showing 1 changed file with 33 additions and 13 deletions.
46 changes: 33 additions & 13 deletions include/rfl/bson/Writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ namespace bson {

/// Please refer to https://mongoc.org/libbson/current/api.html
class Writer {
using ParentType = std::variant<bson_t*, bson_array_builder_t*>;
struct IsRoot {};

using ParentType = std::variant<bson_t*, bson_array_builder_t*, IsRoot>;

public:
struct BSONOutputArray {
Expand All @@ -44,22 +46,32 @@ class Writer {
using OutputObjectType = BSONOutputObject;
using OutputVarType = BSONOutputVar;

Writer() : { bson_init(&doc_); }
Writer() : {}

~Writer() = default;

// TODO
OutputArrayType array_as_root(const size_t _size) const noexcept {}
OutputArrayType array_as_root(const size_t _size) const noexcept {
bson_array_builder_t* val = bson_array_builder_new();
return OutputObjectType(val, IsRoot{});
}

// TODO
OutputObjectType object_as_root(const size_t _size) const noexcept {}
OutputObjectType object_as_root(const size_t _size) const noexcept {
bson_t val;
bson_init(&val);
return OutputObjectType(val, IsRoot{});
}

// TODO
OutputVarType null_as_root() const noexcept {}
OutputVarType null_as_root() const noexcept {
// Appears to be unsupported by the BSON C API.
return OutputVarType{};
}

// TODO
template <class T>
OutputVarType value_as_root(const T& _var) const noexcept {}
OutputVarType value_as_root(const T& _var) const noexcept {
static_assert(rfl::always_false_v<T>,
"BSON only allows arrays or objects as its root.");
return OutputVarType{};
}

OutputArrayType add_array_to_array(const size_t _size,
OutputArrayType* _parent) const noexcept {
Expand Down Expand Up @@ -154,8 +166,12 @@ class Writer {
using Type = std::remove_cvref_t<decltype(_parent)>;
if constexpr (std::is_same<Type, bson_array_builder_t*>()) {
bson_array_builder_append_array_builder_end(_parent, _arr->val_);
} else {
} else if constexpr (std::is_same<Tupe, bson_t>()) {
bson_append_array_end(_parent, _arr->val_);
} else if constexpr (std::is_same<Type, IsRoot>()) {
bson_array_builder_build(_arr->val_, doc_);
} else {
static_assert(rfl::always_false_v<Type>, "Unsupported type.");
}
};
std::visit(handle, _arr->parent_);
Expand All @@ -166,14 +182,18 @@ class Writer {
using Type = std::remove_cvref_t<decltype(_parent)>;
if constexpr (std::is_same<Type, bson_array_builder_t*>()) {
bson_array_builder_append_document_end(_parent, _arr->val_);
} else {
} else if constexpr (std::is_same<Tupe, bson_t>()) {
bson_append_document_end(_parent, _arr->val_);
} else if constexpr (std::is_same<Type, IsRoot>()) {
doc_ = _obj->val_;
} else {
static_assert(rfl::always_false_v<Type>, "Unsupported type.");
}
};
std::visit(handle, _arr->parent_);
}

public:
private:
bson_t doc_;
};

Expand Down

0 comments on commit 1d0c37f

Please sign in to comment.