MongoDB C++ Driver current
Loading...
Searching...
No Matches
list.hpp
1// Copyright 2020 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
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>
23
24#include <bsoncxx/config/prelude.hpp>
25
26namespace bsoncxx {
27inline namespace v_noabi {
28namespace builder {
29using namespace bsoncxx::types;
30
34class list {
35 using initializer_list_t = std::initializer_list<list>;
36
37 public:
41 list() : list({}) {}
42
53 template <typename T>
54 list(T value) : val{value} {}
55
59 // 1. The initializer list's size is even; this implies a list of
60 // key-value pairs or an empty document if the size is zero.
61 // 2. Each 'key' is a string type. In a list of key-value pairs, the 'key' is every other
62 // element starting at the 0th element.
63 //
74 list(initializer_list_t init) : list(init, true, true) {}
75
81 operator bson_value::view() {
82 return view();
83 }
84
91 return val.view();
92 }
93
94 private:
96
97 friend class document;
98 friend class array;
99
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";
107 return false;
108 }
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);
114 return false;
115 }
116 }
117 return true;
118 }();
119 }
120
121 if (valid_document) {
122 core _core{false};
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);
126 }
127 val = bson_value::value(_core.extract_document());
128 } else if (type_deduction || is_array) {
129 core _core{true};
130 for (auto&& ele : init)
131 _core.append(ele.val);
132 val = bson_value::value(_core.extract_array());
133 } else {
134 throw bsoncxx::exception{error_code::k_unmatched_key_in_builder, err_msg.str()};
135 }
136 }
137};
138
142class document : public list {
143 using initializer_list_t = std::initializer_list<list>;
144
145 public:
149 document() : list({}, false, false){};
150
160 document(initializer_list_t init) : list(init, false, false) {}
161};
162
166class array : public list {
167 using initializer_list_t = std::initializer_list<list>;
168
169 public:
173 array() : list({}, false, true){};
174
184 array(initializer_list_t init) : list(init, false, true) {}
185};
186} // namespace builder
187} // namespace v_noabi
188} // namespace bsoncxx
189
190// CXX-2770: missing include of postlude header.
191#if defined(BSONCXX_TEST_MACRO_GUARDS_FIX_MISSING_POSTLUDE)
192#include <bsoncxx/config/postlude.hpp>
193#endif
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
operator bson_value::view()
Provides a view of the underlying BSON value.
Definition list.hpp:81
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
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