MongoDB C++ Driver  mongocxx-3.10.2
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/basic/array-fwd.hpp>
20 #include <bsoncxx/builder/list-fwd.hpp>
21 
22 #include <bsoncxx/builder/core.hpp>
23 #include <bsoncxx/exception/error_code.hpp>
24 #include <bsoncxx/exception/exception.hpp>
25 #include <bsoncxx/types/bson_value/value.hpp>
26 
27 #include <bsoncxx/config/prelude.hpp>
28 
29 namespace bsoncxx {
30 namespace v_noabi {
31 namespace builder {
32 
33 using namespace ::bsoncxx::v_noabi::types; // Deprecated.
34 
35 } // namespace builder
36 } // namespace v_noabi
37 } // namespace bsoncxx
38 
39 namespace bsoncxx {
40 namespace v_noabi {
41 namespace builder {
42 
46 class list {
47  using initializer_list_t = std::initializer_list<list>;
48 
49  public:
53  list() : list({}) {}
54 
65  template <typename T>
66  list(T value) : val{value} {}
67 
71  // 1. The initializer list's size is even; this implies a list of
72  // key-value pairs or an empty document if the size is zero.
73  // 2. Each 'key' is a string type. In a list of key-value pairs, the 'key' is every other
74  // element starting at the 0th element.
75  //
87  list(initializer_list_t init) : list(init, true, true) {}
88 
94  operator bson_value::view() {
95  return view();
96  }
97 
103  bson_value::view view() {
104  return val.view();
105  }
106 
107  private:
108  bson_value::value val;
109 
110  friend ::bsoncxx::v_noabi::builder::document;
111  friend ::bsoncxx::v_noabi::builder::array;
112 
113  list(initializer_list_t init, bool type_deduction, bool is_array) : val{nullptr} {
114  std::stringstream err_msg{"cannot construct document"};
115  bool valid_document = false;
116  if (type_deduction || !is_array) {
117  valid_document = [&] {
118  if (init.size() % 2 != 0) {
119  err_msg << " : must be list of key-value pairs";
120  return false;
121  }
122  for (size_t i = 0; i < init.size(); i += 2) {
123  auto t = (begin(init) + i)->val.view().type();
124  if (t != type::k_utf8) {
125  err_msg << " : all keys must be string type. ";
126  err_msg << "Found type=" << to_string(t);
127  return false;
128  }
129  }
130  return true;
131  }();
132  }
133 
134  if (valid_document) {
135  core _core{false};
136  for (size_t i = 0; i < init.size(); i += 2) {
137  _core.key_owned(std::string((begin(init) + i)->val.view().get_string().value));
138  _core.append((begin(init) + i + 1)->val);
139  }
140  val = bson_value::value(_core.extract_document());
141  } else if (type_deduction || is_array) {
142  core _core{true};
143  for (auto&& ele : init)
144  _core.append(ele.val);
145  val = bson_value::value(_core.extract_array());
146  } else {
147  throw bsoncxx::v_noabi::exception{error_code::k_unmatched_key_in_builder,
148  err_msg.str()};
149  }
150  }
151 };
152 
156 class document : public list {
157  using initializer_list_t = std::initializer_list<list>;
158 
159  public:
163  document() : list({}, false, false){};
164 
174  document(initializer_list_t init) : list(init, false, false) {}
175 };
176 
180 class array : public list {
181  using initializer_list_t = std::initializer_list<list>;
182 
183  public:
187  array() : list({}, false, true){};
188 
198  array(initializer_list_t init) : list(init, false, true) {}
199 };
200 } // namespace builder
201 } // namespace v_noabi
202 } // namespace bsoncxx
203 
204 namespace bsoncxx {
205 namespace builder {
206 
207 using namespace ::bsoncxx::v_noabi::types; // Deprecated.
208 
209 } // namespace builder
210 } // namespace bsoncxx
211 
212 // CXX-2770: missing include of postlude header.
213 #if defined(BSONCXX_TEST_MACRO_GUARDS_FIX_MISSING_POSTLUDE)
214 #include <bsoncxx/config/postlude.hpp>
215 #endif
A JSON-like builder for creating arrays.
Definition: list.hpp:180
array(initializer_list_t init)
Creates a BSON array.
Definition: list.hpp:198
array()
Creates an empty array.
Definition: list.hpp:187
A JSON-like builder for creating documents.
Definition: list.hpp:156
document(initializer_list_t init)
Creates a BSON document.
Definition: list.hpp:174
document()
Creates an empty document.
Definition: list.hpp:163
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.
Definition: list.hpp:66
list()
Creates an empty document.
Definition: list.hpp:53
bson_value::view view()
Provides a view of the underlying BSON value.
Definition: list.hpp:103
list(initializer_list_t init)
Creates a BSON document, if possible.
Definition: list.hpp:87
Class representing any exceptions emitted from the bsoncxx library or its underlying implementation.
Definition: exception.hpp:30
std::string to_string(type rhs)
Returns a stringification of the given type.
The top-level namespace for bsoncxx library entities.
Definition: element-fwd.hpp:19