MongoDB C++ Driver current
key_context.hpp
1
2// Copyright 2014 MongoDB Inc.
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16#pragma once
17
18#include <bsoncxx/builder/core.hpp>
19#include <bsoncxx/builder/stream/closed_context.hpp>
20#include <bsoncxx/builder/stream/value_context.hpp>
21#include <bsoncxx/stdx/string_view.hpp>
22#include <bsoncxx/util/functor.hpp>
23
24#include <bsoncxx/config/prelude.hpp>
25
26namespace bsoncxx {
27BSONCXX_INLINE_NAMESPACE_BEGIN
28namespace builder {
29namespace stream {
30
48template <class base = closed_context>
50 public:
57 key_context(core* core) : _core(core) {}
58
68 template <std::size_t n>
69 BSONCXX_INLINE value_context<key_context> operator<<(const char (&v)[n]) {
70 _core->key_view(stdx::string_view{v, n - 1});
71 return value_context<key_context>(_core);
72 }
73
83 BSONCXX_INLINE value_context<key_context> operator<<(std::string str) {
84 _core->key_owned(std::move(str));
85 return value_context<key_context>(_core);
86 }
87
97 BSONCXX_INLINE value_context<key_context> operator<<(stdx::string_view str) {
98 _core->key_view(std::move(str));
99 return value_context<key_context>(_core);
100 }
101
110 template <typename T>
111 BSONCXX_INLINE
112 typename std::enable_if<util::is_functor<T, void(key_context<>)>::value, key_context>::type&
113 operator<<(T&& func) {
114 func(*this);
115 return *this;
116 }
117
129 template <typename T>
130 BSONCXX_INLINE typename std::enable_if<
131 std::is_same<base, closed_context>::value &&
132 std::is_same<typename std::remove_reference<T>::type, const finalize_type>::value,
133 // TODO(MSVC): This should just be 'document::value', but
134 // VS2015U1 can't resolve the name.
136 operator<<(T&&) {
137 return _core->extract_document();
138 }
139
150 _core->concatenate(doc);
151 return *this;
152 }
153
160 BSONCXX_INLINE base operator<<(const close_document_type) {
161 _core->close_document();
162 return unwrap();
163 }
164
169 BSONCXX_INLINE operator key_context<>() {
170 return key_context<>(_core);
171 }
172
173 private:
174 BSONCXX_INLINE base unwrap() {
175 return base(_core);
176 }
177
178 core* _core;
179};
180
181} // namespace stream
182} // namespace builder
183BSONCXX_INLINE_NAMESPACE_END
184} // namespace bsoncxx
185
186#include <bsoncxx/config/postlude.hpp>
A low-level interface for constructing BSON documents and arrays.
Definition: core.hpp:42
core & key_owned(std::string key)
Appends a key passed as an STL string.
core & key_view(stdx::string_view key)
Appends a key passed as a non-owning stdx::string_view.
core & concatenate(const document::view &view)
Appends the keys from a BSON document into this BSON datum.
core & close_document()
Closes the current sub-document within this BSON datum.
document::value extract_document()
Transfers ownership of the underlying document to the caller.
A stream context which expects a key, which can later be followed by value, then more key/value pairs...
Definition: key_context.hpp:49
value_context< key_context > operator<<(const char(&v)[n])
<< operator for accepting a literal key and appending it to the core builder.
Definition: key_context.hpp:69
base operator<<(const close_document_type)
<< operator for closing a subdocument in the core builder.
Definition: key_context.hpp:160
key_context(core *core)
Create a key_context given a core builder.
Definition: key_context.hpp:57
key_context operator<<(concatenate_doc doc)
<< operator for concatenating another document.
Definition: key_context.hpp:149
A stream context which expects a value, which can later be followed by more key/value pairs.
Definition: value_context.hpp:48
A read-only BSON document that owns its underlying buffer.
Definition: value.hpp:34
Top level namespace for MongoDB C++ BSON functionality.
Definition: element.hpp:24
type
An enumeration of each BSON type.
Definition: types.hpp:46
Container to concatenate a document.
Definition: concatenate.hpp:30
The type of a stream manipulator to close a subdocument.
Definition: helpers.hpp:44
The type of a stream manipulator to finalize a document.
Definition: helpers.hpp:83