MongoDB C++ Driver  mongocxx-3.7.0
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 
26 namespace bsoncxx {
27 BSONCXX_INLINE_NAMESPACE_BEGIN
28 namespace builder {
29 using namespace bsoncxx::types;
30 
34 class 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 
142 class 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 
166 class 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 BSONCXX_INLINE_NAMESPACE_END
188 } // namespace bsoncxx
bsoncxx
Top level namespace for MongoDB C++ BSON functionality.
Definition: element.hpp:24
bsoncxx::exception
Class representing any exceptions emitted from the bsoncxx library or its underlying implementation.
Definition: exception.hpp:28
bsoncxx::builder::list::list
list()
Creates an empty document.
Definition: list.hpp:41
bsoncxx::builder::document::document
document()
Creates an empty document.
Definition: list.hpp:149
bsoncxx::builder::list::list
list(T value)
Creates a bsoncxx::builder::list from a value of type T.
Definition: list.hpp:54
bsoncxx::types::bson_value::view::view
view() noexcept
Default constructs a bson_value::view.
bsoncxx::types::bson_value::value::view
bson_value::view view() const noexcept
Get a view over the bson_value owned by this object.
bsoncxx::builder::list::list
list(initializer_list_t init)
Creates a BSON document, if possible.
Definition: list.hpp:74
bsoncxx::builder::array::array
array(initializer_list_t init)
Creates a BSON array.
Definition: list.hpp:184
bsoncxx::builder::document::document
document(initializer_list_t init)
Creates a BSON document.
Definition: list.hpp:160
bsoncxx::types::bson_value::view::get_string
const b_string & get_string() const
bsoncxx::builder::document
A JSON-like builder for creating documents.
Definition: list.hpp:142
bsoncxx::types::bson_value::view::type
bsoncxx::type type() const
bsoncxx::builder::list::view
bson_value::view view()
Provides a view of the underlying BSON value.
Definition: list.hpp:90
bsoncxx::to_string
std::string to_string(type rhs)
Returns a stringification of the given type.
bsoncxx::builder::list
A JSON-like builder for creating documents and arrays.
Definition: list.hpp:34
bsoncxx::builder::array
A JSON-like builder for creating arrays.
Definition: list.hpp:166
bsoncxx::types::bson_value::value
A variant owning type that represents any BSON type.
Definition: value.hpp:44
bsoncxx::builder::array::array
array()
Creates an empty array.
Definition: list.hpp:173
bsoncxx::types::bson_value::view
A view-only variant that can contain any BSON type.
Definition: view.hpp:44