Repository navigation
Expand file tree
/
Copy pathWriter.cpp
More file actions
144 lines (124 loc) · 4.72 KB
/
Copy pathWriter.cpp
File metadata and controls
144 lines (124 loc) · 4.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "rfl/bson/Writer.hpp"
namespace rfl::bson {
Writer::Writer(bson_t* _doc) : doc_(_doc) {}
Writer::~Writer() = default;
Writer::OutputArrayType Writer::array_as_root(
const size_t /*_size*/) const noexcept {
bson_array_builder_t* val = bson_array_builder_new();
return OutputArrayType(val, IsRoot{});
}
Writer::OutputObjectType Writer::object_as_root(
const size_t /*_size*/) const noexcept {
return OutputObjectType(doc_, IsRoot{});
}
Writer::OutputVarType Writer::null_as_root() const noexcept {
// Appears to be unsupported by the BSON C API.
return OutputVarType{};
}
Writer::OutputArrayType Writer::add_array_to_array(
const size_t /*_size*/, OutputArrayType* _parent) const {
bson_array_builder_t* val;
const bool ok =
bson_array_builder_append_array_builder_begin(_parent->val_, &val);
if (!ok) {
throw std::runtime_error("Could not array to array.");
}
return OutputArrayType(val, IsArray{_parent->val_});
}
Writer::OutputArrayType Writer::add_array_to_object(
const std::string_view& _name, const size_t /*_size*/,
OutputObjectType* _parent) const {
bson_array_builder_t* val;
const bool ok = bson_append_array_builder_begin(
_parent->val_, _name.data(), static_cast<int>(_name.size()), &val);
if (!ok) {
throw std::runtime_error("Could not array to array.");
}
return OutputArrayType(val, IsObject{_parent->val_});
}
Writer::OutputObjectType Writer::add_object_to_array(
const size_t /*_size*/, OutputArrayType* _parent) const {
subdocs_->emplace_back(rfl::Box<BSONType>());
const bool ok = bson_array_builder_append_document_begin(
_parent->val_, &(subdocs_->back()->val_));
if (!ok) {
throw std::runtime_error("Could not object to array.");
}
return OutputObjectType(&subdocs_->back()->val_, IsArray{_parent->val_});
}
Writer::OutputObjectType Writer::add_object_to_object(
const std::string_view& _name, const size_t /*_size*/,
OutputObjectType* _parent) const {
subdocs_->emplace_back(rfl::Box<BSONType>());
const bool ok = bson_append_document_begin(_parent->val_, _name.data(),
static_cast<int>(_name.size()),
&(subdocs_->back()->val_));
if (!ok) {
throw std::runtime_error("Could not add object to object.");
}
return OutputObjectType(&subdocs_->back()->val_, IsObject{_parent->val_});
}
Writer::OutputVarType Writer::add_null_to_array(
OutputArrayType* _parent) const {
const bool ok = bson_array_builder_append_null(_parent->val_);
if (!ok) {
throw std::runtime_error("Could not add null to array.");
}
return OutputVarType{};
}
Writer::OutputVarType Writer::add_null_to_object(
const std::string_view& _name, OutputObjectType* _parent) const {
const bool ok = bson_append_null(_parent->val_, _name.data(),
static_cast<int>(_name.size()));
if (!ok) {
throw std::runtime_error("Could not add null to object.");
}
return OutputVarType{};
}
void Writer::end_array(OutputArrayType* _arr) const {
const auto handle = [&](const auto _parent) {
using Type = std::remove_cvref_t<decltype(_parent)>;
if constexpr (std::is_same<Type, IsArray>()) {
const bool ok =
bson_array_builder_append_array_builder_end(_parent.ptr_, _arr->val_);
if (!ok) {
throw std::runtime_error("Could not end array.");
}
} else if constexpr (std::is_same<Type, IsObject>()) {
const bool ok = bson_append_array_builder_end(_parent.ptr_, _arr->val_);
if (!ok) {
throw std::runtime_error("Could not end array.");
}
} else if constexpr (std::is_same<Type, IsRoot>()) {
const bool ok = bson_array_builder_build(_arr->val_, doc_);
if (!ok) {
throw std::runtime_error("Could not end array.");
}
} else {
static_assert(rfl::always_false_v<Type>, "Unsupported type.");
}
};
std::visit(handle, _arr->parent_);
}
void Writer::end_object(OutputObjectType* _obj) const {
const auto handle = [&](const auto _parent) {
using Type = std::remove_cvref_t<decltype(_parent)>;
if constexpr (std::is_same<Type, IsArray>()) {
const bool ok =
bson_array_builder_append_document_end(_parent.ptr_, _obj->val_);
if (!ok) {
throw std::runtime_error("Could not end object.");
}
} else if constexpr (std::is_same<Type, IsObject>()) {
const bool ok = bson_append_document_end(_parent.ptr_, _obj->val_);
if (!ok) {
throw std::runtime_error("Could not end object.");
}
} else if constexpr (std::is_same<Type, IsRoot>()) {
} else {
static_assert(rfl::always_false_v<Type>, "Unsupported type.");
}
};
std::visit(handle, _obj->parent_);
}
} // namespace rfl::bson