19 #include <bsoncxx/builder/basic/array-fwd.hpp>
20 #include <bsoncxx/builder/list-fwd.hpp>
22 #include <bsoncxx/builder/core.hpp>
23 #include <bsoncxx/exception/error_code.hpp>
24 #include <bsoncxx/exception/exception.hpp>
25 #include <bsoncxx/types/bson_value/value.hpp>
27 #include <bsoncxx/config/prelude.hpp>
33 using namespace ::bsoncxx::v_noabi::types;
47 using initializer_list_t = std::initializer_list<list>;
66 list(T value) : val{value} {}
87 list(initializer_list_t init) :
list(init, true, true) {}
94 operator bson_value::view() {
108 bson_value::value val;
110 friend ::bsoncxx::v_noabi::builder::document;
111 friend ::bsoncxx::v_noabi::builder::array;
113 list(initializer_list_t init,
bool type_deduction,
bool is_array) : val{nullptr} {
114 std::stringstream err_msg{
"cannot construct document"};
115 bool valid_document =
false;
116 if (type_deduction || !is_array) {
117 valid_document = [&] {
118 if (init.size() % 2 != 0) {
119 err_msg <<
" : must be list of key-value pairs";
122 for (
size_t i = 0; i < init.size(); i += 2) {
123 auto t = (begin(init) + i)->val.view().type();
124 if (t != type::k_utf8) {
125 err_msg <<
" : all keys must be string type. ";
126 err_msg <<
"Found type=" <<
to_string(t);
134 if (valid_document) {
136 for (
size_t i = 0; i < init.size(); i += 2) {
137 _core.key_owned(std::string((begin(init) + i)->val.view().get_string().value));
138 _core.append((begin(init) + i + 1)->val);
140 val = bson_value::value(_core.extract_document());
141 }
else if (type_deduction || is_array) {
143 for (
auto&& ele : init)
144 _core.append(ele.val);
145 val = bson_value::value(_core.extract_array());
157 using initializer_list_t = std::initializer_list<list>;
181 using initializer_list_t = std::initializer_list<list>;
198 array(initializer_list_t init) :
list(init, false, true) {}
207 using namespace ::bsoncxx::v_noabi::types;
213 #if defined(BSONCXX_TEST_MACRO_GUARDS_FIX_MISSING_POSTLUDE)
214 #include <bsoncxx/config/postlude.hpp>
A JSON-like builder for creating arrays.
Definition: list.hpp:180
array(initializer_list_t init)
Creates a BSON array.
Definition: list.hpp:198
array()
Creates an empty array.
Definition: list.hpp:187
A JSON-like builder for creating documents.
Definition: list.hpp:156
document(initializer_list_t init)
Creates a BSON document.
Definition: list.hpp:174
document()
Creates an empty document.
Definition: list.hpp:163
A JSON-like builder for creating documents and arrays.
Definition: list.hpp:46
list(T value)
Creates a bsoncxx::v_noabi::builder::list from a value of type T.
Definition: list.hpp:66
list()
Creates an empty document.
Definition: list.hpp:53
bson_value::view view()
Provides a view of the underlying BSON value.
Definition: list.hpp:103
list(initializer_list_t init)
Creates a BSON document, if possible.
Definition: list.hpp:87
Class representing any exceptions emitted from the bsoncxx library or its underlying implementation.
Definition: exception.hpp:30
std::string to_string(type rhs)
Returns a stringification of the given type.
The top-level namespace for bsoncxx library entities.
Definition: element-fwd.hpp:19