MongoDB C++ Driver mongocxx-3.11.0
Loading...
Searching...
No Matches
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
33using namespace ::bsoncxx::v_noabi::types; // Deprecated. Deliberately undocumented.
34
35} // namespace builder
36} // namespace v_noabi
37} // namespace bsoncxx
38
39namespace bsoncxx {
40namespace v_noabi {
41namespace builder {
42
46class list {
47 using initializer_list_t = std::initializer_list<list>;
48
49 public:
53 list() : list({}) {}
54
66 template <typename T>
67 list(T value) : val{value} {}
68
72 // 1. The initializer list's size is even; this implies a list of
73 // key-value pairs or an empty document if the size is zero.
74 // 2. Each 'key' is a string type. In a list of key-value pairs, the 'key' is every other
75 // element starting at the 0th element.
76 //
89 list(initializer_list_t init) : list(init, true, true) {}
90
97 operator bson_value::view() {
98 return view();
99 }
100
107 bson_value::view view() {
108 return val.view();
109 }
110
111 private:
112 bson_value::value val;
113
114 friend ::bsoncxx::v_noabi::builder::document;
115 friend ::bsoncxx::v_noabi::builder::array;
116
117 list(initializer_list_t init, bool type_deduction, bool is_array) : val{nullptr} {
118 std::stringstream err_msg{"cannot construct document"};
119 bool valid_document = false;
120 if (type_deduction || !is_array) {
121 valid_document = [&] {
122 if (init.size() % 2 != 0) {
123 err_msg << " : must be list of key-value pairs";
124 return false;
125 }
126 for (size_t i = 0; i < init.size(); i += 2) {
127 auto t = (begin(init) + i)->val.view().type();
128 if (t != type::k_utf8) {
129 err_msg << " : all keys must be string type. ";
130 err_msg << "Found type=" << to_string(t);
131 return false;
132 }
133 }
134 return true;
135 }();
136 }
137
138 if (valid_document) {
139 core _core{false};
140 for (size_t i = 0; i < init.size(); i += 2) {
141 _core.key_owned(std::string((begin(init) + i)->val.view().get_string().value));
142 _core.append((begin(init) + i + 1)->val);
143 }
144 val = bson_value::value(_core.extract_document());
145 } else if (type_deduction || is_array) {
146 core _core{true};
147 for (auto&& ele : init)
148 _core.append(ele.val);
149 val = bson_value::value(_core.extract_array());
150 } else {
152 err_msg.str()};
153 }
154 }
155};
156
160class document : public list {
161 using initializer_list_t = std::initializer_list<list>;
162
163 public:
167 document() : list({}, false, false) {}
168
179 document(initializer_list_t init) : list(init, false, false) {}
180};
181
185class array : public list {
186 using initializer_list_t = std::initializer_list<list>;
187
188 public:
192 array() : list({}, false, true) {}
193
204 array(initializer_list_t init) : list(init, false, true) {}
205};
206} // namespace builder
207} // namespace v_noabi
208} // namespace bsoncxx
209
210namespace bsoncxx {
211namespace builder {
212
213using namespace ::bsoncxx::v_noabi::types; // Deprecated. Deliberately undocumented.
214
215} // namespace builder
216} // namespace bsoncxx
217
218// CXX-2770: missing include of postlude header.
219#if defined(BSONCXX_TEST_MACRO_GUARDS_FIX_MISSING_POSTLUDE)
221#endif
222
Declares bsoncxx::v_noabi::builder::basic::array.
The bsoncxx macro guard postlude header.
The bsoncxx macro guard prelude header.
Provides bsoncxx::v_noabi::error_code.
Provides bsoncxx::v_noabi::exception.
A JSON-like builder for creating arrays.
Definition list.hpp:185
array(initializer_list_t init)
Creates a BSON array.
Definition list.hpp:204
array()
Creates an empty array.
Definition list.hpp:192
A JSON-like builder for creating documents.
Definition list.hpp:160
document(initializer_list_t init)
Creates a BSON document.
Definition list.hpp:179
document()
Creates an empty document.
Definition list.hpp:167
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. T must be a bsoncxx::v_noabi::types...
Definition list.hpp:67
list()
Creates an empty document.
Definition list.hpp:53
bson_value::view view()
Provides a view of the underlying BSON value.
Definition list.hpp:107
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:89
operator bson_value::view()
Provides a view of the underlying BSON value.
Definition list.hpp:97
Class representing any exceptions emitted from the bsoncxx library or its underlying implementation.
Definition exception.hpp:34
Provides bsoncxx::v_noabi::builder::core.
Provides entities for use with "list" BSON builder syntax.
Declares entities representing BSON value types.
@ k_unmatched_key_in_builder
Attempted to view or extract a document when a key was still awaiting a matching value.
std::string to_string(type rhs)
Returns a stringification of the given type.
@ k_utf8
Equivalent to k_string.
The top-level namespace within which all bsoncxx library entities are declared.
Provides bsoncxx::v_noabi::types::bson_value::value.