MongoDB C++ Driver mongocxx-3.0.1
Loading...
Searching...
No Matches
key_context.hpp
1// Copyright 2014 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 <bsoncxx/builder/core.hpp>
18#include <bsoncxx/builder/stream/closed_context.hpp>
19#include <bsoncxx/builder/stream/value_context.hpp>
20#include <bsoncxx/stdx/string_view.hpp>
21#include <bsoncxx/util/functor.hpp>
22
23#include <bsoncxx/config/prelude.hpp>
24
25namespace bsoncxx {
26BSONCXX_INLINE_NAMESPACE_BEGIN
27namespace builder {
28namespace stream {
29
47template <class base = closed_context>
49 public:
57 }
58
66 template <std::size_t n>
67 BSONCXX_INLINE value_context<key_context> operator<<(const char(&v)[n]) {
68 _core->key_view(stdx::string_view{v, n - 1});
69 return value_context<key_context>(_core);
70 }
71
79 BSONCXX_INLINE value_context<key_context> operator<<(std::string str) {
80 _core->key_owned(std::move(str));
81 return value_context<key_context>(_core);
82 }
83
91 BSONCXX_INLINE value_context<key_context> operator<<(stdx::string_view str) {
92 _core->key_view(std::move(str));
93 return value_context<key_context>(_core);
94 }
95
104 template <typename T>
105 BSONCXX_INLINE
106 typename std::enable_if<util::is_functor<T, void(key_context<>)>::value, key_context>::type&
107 operator<<(T&& func) {
108 func(*this);
109 return *this;
110 }
111
123 template <typename T>
124 BSONCXX_INLINE typename std::enable_if<
125 std::is_same<base, closed_context>::value &&
126 std::is_same<typename std::remove_reference<T>::type, const finalize_type>::value,
127 // TODO(MSVC): This should just be 'document::value', but
128 // VS2015U1 can't resolve the name.
130 operator<<(T&&) {
131 return _core->extract_document();
132 }
133
144 _core->concatenate(doc);
145 return *this;
146 }
147
154 BSONCXX_INLINE base operator<<(const close_document_type) {
155 _core->close_document();
156 return unwrap();
157 }
158
163 BSONCXX_INLINE operator key_context<>() {
164 return key_context<>(_core);
165 }
166
167 private:
168 BSONCXX_INLINE base unwrap() {
169 return base(_core);
170 }
171
172 core* _core;
173};
174
175} // namespace stream
176} // namespace builder
177BSONCXX_INLINE_NAMESPACE_END
178} // namespace bsoncxx
179
180#include <bsoncxx/config/postlude.hpp>
A low-level interface for constructing BSON documents and arrays.
Definition core.hpp:42
void close_document()
Closes the current sub-document within this BSON datum.
void key_view(stdx::string_view key)
Appends a key passed as a non-owning stdx::string_view.
void concatenate(const document::view &view)
Appends the keys from a BSON document into this BSON datum.
document::value extract_document()
Transfer ownership of the underlying document to the caller.
void key_owned(std::string key)
Appends a key passed as a STL string.
A stream context which expects a key, which can later be followed by value, then more key/value pairs...
Definition key_context.hpp:48
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:67
base operator<<(const close_document_type)
<< operator for closing a subdocument in the core builder.
Definition key_context.hpp:154
key_context(core *core)
Create a key_context given a core builder.
Definition key_context.hpp:56
key_context operator<<(concatenate_doc doc)
<< operator for concatenating another document.
Definition key_context.hpp:143
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:33
Container to concatenate a document.
Definition concatenate.hpp:30