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