19 #include <bsoncxx/builder/core.hpp>
20 #include <bsoncxx/exception/error_code.hpp>
21 #include <bsoncxx/exception/exception.hpp>
22 #include <bsoncxx/types/bson_value/value.hpp>
24 #include <bsoncxx/config/prelude.hpp>
27 inline namespace v_noabi {
29 using namespace bsoncxx::types;
35 using initializer_list_t = std::initializer_list<list>;
54 list(T value) : val{value} {}
74 list(initializer_list_t init) :
list(init, true, true) {}
100 list(initializer_list_t init,
bool type_deduction,
bool is_array) : val{nullptr} {
101 std::stringstream err_msg{
"cannot construct document"};
102 bool valid_document =
false;
103 if (type_deduction || !is_array) {
104 valid_document = [&] {
105 if (init.size() % 2 != 0) {
106 err_msg <<
" : must be list of key-value pairs";
109 for (
size_t i = 0; i < init.size(); i += 2) {
110 auto t = (begin(init) + i)->val.
view().
type();
111 if (t != type::k_utf8) {
112 err_msg <<
" : all keys must be string type. ";
113 err_msg <<
"Found type=" <<
to_string(t);
121 if (valid_document) {
123 for (
size_t i = 0; i < init.size(); i += 2) {
124 _core.key_owned(std::string((begin(init) + i)->val.
view().
get_string().value));
125 _core.append((begin(init) + i + 1)->val);
128 }
else if (type_deduction || is_array) {
130 for (
auto&& ele : init)
131 _core.append(ele.val);
143 using initializer_list_t = std::initializer_list<list>;
167 using initializer_list_t = std::initializer_list<list>;
184 array(initializer_list_t init) :
list(init, false, true) {}
191 #if defined(BSONCXX_TEST_MACRO_GUARDS_FIX_MISSING_POSTLUDE)
192 #include <bsoncxx/config/postlude.hpp>
A JSON-like builder for creating arrays.
Definition: list.hpp:166
array(initializer_list_t init)
Creates a BSON array.
Definition: list.hpp:184
array()
Creates an empty array.
Definition: list.hpp:173
A JSON-like builder for creating documents.
Definition: list.hpp:142
document(initializer_list_t init)
Creates a BSON document.
Definition: list.hpp:160
document()
Creates an empty document.
Definition: list.hpp:149
A JSON-like builder for creating documents and arrays.
Definition: list.hpp:34
list(T value)
Creates a bsoncxx::builder::list from a value of type T.
Definition: list.hpp:54
list()
Creates an empty document.
Definition: list.hpp:41
bson_value::view view()
Provides a view of the underlying BSON value.
Definition: list.hpp:90
list(initializer_list_t init)
Creates a BSON document, if possible.
Definition: list.hpp:74
Class representing any exceptions emitted from the bsoncxx library or its underlying implementation.
Definition: exception.hpp:27
A variant owning type that represents any BSON type.
Definition: value.hpp:43
bson_value::view view() const noexcept
Get a view over the bson_value owned by this object.
A view-only variant that can contain any BSON type.
Definition: view.hpp:44
bsoncxx::type type() const
const b_string & get_string() const
view() noexcept
Default constructs a bson_value::view.
std::string to_string(type rhs)
Returns a stringification of the given type.
The top-level namespace for bsoncxx library entities.
Definition: element.hpp:24