Loading...
Searching...
No Matches
list.hpp
Go to the documentation of this file.
// Copyright 2009-present MongoDB, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include <sstream>
#include <bsoncxx/builder/basic/array-fwd.hpp> // IWYU pragma: keep: backward compatibility, to be removed.
#include <bsoncxx/builder/list-fwd.hpp> // IWYU pragma: export
#include <bsoncxx/builder/core.hpp>
#include <bsoncxx/exception/error_code.hpp>
#include <bsoncxx/exception/exception.hpp>
#include <bsoncxx/types/bson_value/value.hpp>
#include <bsoncxx/config/prelude.hpp>
namespace bsoncxx {
namespace v_noabi {
namespace builder {
class list {
using initializer_list_t = std::initializer_list<list>;
public:
list() : list({}) {}
template <typename T>
list(T value) : val{value} {}
// 1. The initializer list's size is even; this implies a list of
// key-value pairs or an empty document if the size is zero.
// 2. Each 'key' is a string type. In a list of key-value pairs, the 'key' is every other
// element starting at the 0th element.
//
list(initializer_list_t init) : list(init, true, true) {}
operator types::bson_value::view() {
return view();
}
types::bson_value::view view() {
return val.view();
}
private:
types::bson_value::value val;
friend v_noabi::builder::document;
friend v_noabi::builder::array;
list(initializer_list_t init, bool type_deduction, bool is_array) : val{nullptr} {
std::stringstream err_msg{"cannot construct document"};
bool valid_document = false;
if (type_deduction || !is_array) {
valid_document = [&] {
if (init.size() % 2 != 0) {
err_msg << " : must be list of key-value pairs";
return false;
}
for (size_t i = 0; i < init.size(); i += 2) {
auto t = (begin(init) + i)->val.view().type();
if (t != type::k_string) {
err_msg << " : all keys must be string type. ";
err_msg << "Found type=" << to_string(t);
return false;
}
}
return true;
}();
}
if (valid_document) {
core _core{false};
for (size_t i = 0; i < init.size(); i += 2) {
_core.key_owned(std::string((begin(init) + i)->val.view().get_string().value));
_core.append((begin(init) + i + 1)->val);
}
val = types::bson_value::value(_core.extract_document());
} else if (type_deduction || is_array) {
core _core{true};
for (auto&& ele : init)
_core.append(ele.val);
val = types::bson_value::value(_core.extract_array());
} else {
throw v_noabi::exception{v_noabi::error_code::k_unmatched_key_in_builder, err_msg.str()};
}
}
};
class document : public list {
using initializer_list_t = std::initializer_list<list>;
public:
document() : list({}, false, false) {}
document(initializer_list_t init) : list(init, false, false) {}
};
class array : public list {
using initializer_list_t = std::initializer_list<list>;
public:
array() : list({}, false, true) {}
array(initializer_list_t init) : list(init, false, true) {}
};
} // namespace builder
} // namespace v_noabi
} // namespace bsoncxx
#include <bsoncxx/config/postlude.hpp>
array-fwd.hpp
Declares bsoncxx::v_noabi::builder::basic::array.
postlude.hpp
The bsoncxx v_noabi macro guard postlude header.
prelude.hpp
The bsoncxx v_noabi macro guard prelude header.
error_code.hpp
Provides bsoncxx::v_noabi::error_code.
exception.hpp
Provides bsoncxx::v_noabi::exception.
bsoncxx::builder::array::array
array()
Creates an empty array.
Definition list.hpp:181
bsoncxx::builder::document::document
document()
Creates an empty document.
Definition list.hpp:156
bsoncxx::builder::list::list
list()
Creates an empty document.
Definition list.hpp:43
bsoncxx::v_noabi::builder::array
A JSON-like builder for creating arrays.
Definition list.hpp:174
bsoncxx::v_noabi::builder::array::array
array(initializer_list_t init)
Creates a BSON array.
Definition list.hpp:193
bsoncxx::v_noabi::builder::array::array
array()
Creates an empty array.
Definition list.hpp:181
bsoncxx::v_noabi::builder::document
A JSON-like builder for creating documents.
Definition list.hpp:149
bsoncxx::v_noabi::builder::document::document
document(initializer_list_t init)
Creates a BSON document.
Definition list.hpp:168
bsoncxx::v_noabi::builder::document::document
document()
Creates an empty document.
Definition list.hpp:156
bsoncxx::v_noabi::builder::list::view
types::bson_value::view view()
Provides a view of the underlying BSON value.
Definition list.hpp:97
bsoncxx::v_noabi::builder::list::list
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
bsoncxx::v_noabi::builder::list::view
operator types::bson_value::view()
Provides a view of the underlying BSON value.
Definition list.hpp:87
bsoncxx::v_noabi::builder::list::list
list()
Creates an empty document.
Definition list.hpp:43
bsoncxx::v_noabi::builder::list::list
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
bsoncxx::v_noabi::types::bson_value::value
An owning variant type that represents any BSON type.
Definition value.hpp:57
bsoncxx::v_noabi::types::bson_value::value::view
v_noabi::types::bson_value::view view() const noexcept
Get a view over the bson_value owned by this object.
Definition value.hpp:323
bsoncxx::v_noabi::types::bson_value::view
A non-owning variant that can contain any BSON type.
Definition view.hpp:62
bsoncxx::v_noabi::types::bson_value::view::type
v_noabi::type type() const
Returns the type of the underlying BSON value stored in this object.
Definition view.hpp:135
bsoncxx::v_noabi::types::bson_value::view::view
view() noexcept
Default constructs a bson_value::view. The resulting view will be initialized to point at a bson_valu...
Definition view.hpp:80
core.hpp
Provides bsoncxx::v_noabi::builder::core.
list-fwd.hpp
Provides entities for use with "list" BSON builder syntax.
bsoncxx::v_noabi::builder
Declares entities used to build BSON documents.
bsoncxx::v_noabi
Declares entities whose ABI stability is NOT guaranteed.
bsoncxx::v_noabi::error_code::k_unmatched_key_in_builder
@ k_unmatched_key_in_builder
Attempted to view or extract a document when a key was still awaiting a matching value.
Definition error_code.hpp:87
bsoncxx::v_noabi::to_string
std::string to_string(type rhs)
Returns a stringification of the given type.
Definition types.hpp:128
bsoncxx::v_noabi::type::k_string
@ k_string
UTF-8 string.
Definition types.hpp:49
bsoncxx
The top-level namespace within which all bsoncxx library entities are declared.
value.hpp
Provides bsoncxx::v_noabi::types::bson_value::value.