MongoDB C++ Driver 4.1.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
list.hpp
Go to the documentation of this file.
1// Copyright 2009-present MongoDB, Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#pragma once
16
17#include <sstream>
18
21
26
28
29namespace bsoncxx {
30namespace v_noabi {
31namespace builder {
32
36class list {
37 using initializer_list_t = std::initializer_list<list>;
38
39 public:
43 list() : list({}) {}
44
56 template <typename T>
57 list(T value) : val{value} {}
58
62 // 1. The initializer list's size is even; this implies a list of
63 // key-value pairs or an empty document if the size is zero.
64 // 2. Each 'key' is a string type. In a list of key-value pairs, the 'key' is every other
65 // element starting at the 0th element.
66 //
79 list(initializer_list_t init) : list(init, true, true) {}
80
88 return view();
89 }
90
98 return val.view();
99 }
100
101 private:
103
104 friend ::bsoncxx::v_noabi::builder::document;
105 friend ::bsoncxx::v_noabi::builder::array;
106
107 list(initializer_list_t init, bool type_deduction, bool is_array) : val{nullptr} {
108 std::stringstream err_msg{"cannot construct document"};
109 bool valid_document = false;
110 if (type_deduction || !is_array) {
111 valid_document = [&] {
112 if (init.size() % 2 != 0) {
113 err_msg << " : must be list of key-value pairs";
114 return false;
115 }
116 for (size_t i = 0; i < init.size(); i += 2) {
117 auto t = (begin(init) + i)->val.view().type();
118 if (t != type::k_string) {
119 err_msg << " : all keys must be string type. ";
120 err_msg << "Found type=" << to_string(t);
121 return false;
122 }
123 }
124 return true;
125 }();
126 }
127
128 if (valid_document) {
129 core _core{false};
130 for (size_t i = 0; i < init.size(); i += 2) {
131 _core.key_owned(std::string((begin(init) + i)->val.view().get_string().value));
132 _core.append((begin(init) + i + 1)->val);
133 }
134 val = types::bson_value::value(_core.extract_document());
135 } else if (type_deduction || is_array) {
136 core _core{true};
137 for (auto&& ele : init)
138 _core.append(ele.val);
139 val = types::bson_value::value(_core.extract_array());
140 } else {
141 throw bsoncxx::v_noabi::exception{error_code::k_unmatched_key_in_builder, err_msg.str()};
142 }
143 }
144};
145
149class document : public list {
150 using initializer_list_t = std::initializer_list<list>;
151
152 public:
156 document() : list({}, false, false) {}
157
168 document(initializer_list_t init) : list(init, false, false) {}
169};
170
174class array : public list {
175 using initializer_list_t = std::initializer_list<list>;
176
177 public:
181 array() : list({}, false, true) {}
182
193 array(initializer_list_t init) : list(init, false, true) {}
194};
195} // namespace builder
196} // namespace v_noabi
197} // namespace bsoncxx
198
200
Declares bsoncxx::v_noabi::builder::basic::array.
The bsoncxx v_noabi macro guard postlude header.
The bsoncxx v_noabi macro guard prelude header.
Provides bsoncxx::v_noabi::error_code.
Provides bsoncxx::v_noabi::exception.
array()
Creates an empty array.
Definition list.hpp:181
document()
Creates an empty document.
Definition list.hpp:156
list()
Creates an empty document.
Definition list.hpp:43
array(initializer_list_t init)
Creates a BSON array.
Definition list.hpp:193
array()
Creates an empty array.
Definition list.hpp:181
document(initializer_list_t init)
Creates a BSON document.
Definition list.hpp:168
document()
Creates an empty document.
Definition list.hpp:156
types::bson_value::view view()
Provides a view of the underlying BSON value.
Definition list.hpp:97
list(T value)
Creates a bsoncxx::v_noabi::builder::list from a value of type T. T must be a bsoncxx::v_noabi::types...
Definition list.hpp:57
operator types::bson_value::view()
Provides a view of the underlying BSON value.
Definition list.hpp:87
list()
Creates an empty document.
Definition list.hpp:43
list(initializer_list_t init)
Creates a BSON document, if possible. Otherwise, it will create a BSON array. A document is possible ...
Definition list.hpp:79
An owning variant type that represents any BSON type.
Definition value.hpp:48
bson_value::view view() const noexcept
Get a view over the bson_value owned by this object.
A non-owning variant that can contain any BSON type.
Definition view.hpp:55
bsoncxx::v_noabi::type type() const
Returns the type of the underlying BSON value stored in this object.
view(b_double v) noexcept
Construct a bson_value::view from the provided BSON type.
Provides bsoncxx::v_noabi::builder::core.
Provides entities for use with "list" BSON builder syntax.
Declares entities used to build BSON documents.
Declares entities whose ABI stability is NOT guaranteed.
@ k_unmatched_key_in_builder
Attempted to view or extract a document when a key was still awaiting a matching value.
Definition error_code.hpp:83
std::string to_string(type rhs)
Returns a stringification of the given type.
@ k_string
UTF-8 string.
Definition types.hpp:45
The top-level namespace within which all bsoncxx library entities are declared.
Provides bsoncxx::v_noabi::types::bson_value::value.