MongoDB C++ Driver mongocxx-4.0.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
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 {
142 err_msg.str()};
143 }
144 }
145};
146
150class document : public list {
151 using initializer_list_t = std::initializer_list<list>;
152
153 public:
157 document() : list({}, false, false) {}
158
169 document(initializer_list_t init) : list(init, false, false) {}
170};
171
175class array : public list {
176 using initializer_list_t = std::initializer_list<list>;
177
178 public:
182 array() : list({}, false, true) {}
183
194 array(initializer_list_t init) : list(init, false, true) {}
195};
196} // namespace builder
197} // namespace v_noabi
198} // namespace bsoncxx
199
201
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:175
array(initializer_list_t init)
Creates a BSON array.
Definition list.hpp:194
array()
Creates an empty array.
Definition list.hpp:182
A JSON-like builder for creating documents.
Definition list.hpp:150
document(initializer_list_t init)
Creates a BSON document.
Definition list.hpp:169
document()
Creates an empty document.
Definition list.hpp:157
A JSON-like builder for creating documents and arrays.
Definition list.hpp:36
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
Class representing any exceptions emitted from the bsoncxx library or its underlying implementation.
Definition exception.hpp:34
A variant owning type that represents any BSON type. Owns its underlying buffer. When a bson_value::v...
Definition value.hpp:47
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:54
bsoncxx::v_noabi::type type() const
Returns the type of the underlying BSON value stored in this object.
const b_string & get_string() const
Returns the underlying BSON UTF-8 string value.
Provides bsoncxx::v_noabi::builder::core.
Provides entities for use with "list" BSON builder syntax.
@ 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_string
UTF-8 string.
The top-level namespace within which all bsoncxx library entities are declared.
Provides bsoncxx::v_noabi::types::bson_value::value.