MongoDB C++ Driver  mongocxx-3.7.0
All Classes Namespaces Functions Typedefs Enumerations Enumerator Friends Pages
array_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/array/value.hpp>
18 #include <bsoncxx/builder/concatenate.hpp>
19 #include <bsoncxx/builder/core.hpp>
20 #include <bsoncxx/builder/stream/closed_context.hpp>
21 #include <bsoncxx/builder/stream/helpers.hpp>
22 #include <bsoncxx/util/functor.hpp>
23 
24 #include <bsoncxx/config/prelude.hpp>
25 
26 namespace bsoncxx {
27 BSONCXX_INLINE_NAMESPACE_BEGIN
28 namespace builder {
29 namespace stream {
30 
31 template <class T>
33 
34 class single_context;
35 
51 template <class base = closed_context>
53  public:
60  BSONCXX_INLINE array_context(core* core) : _core(core) {}
61 
69  template <class T>
70  BSONCXX_INLINE typename std::enable_if<
71  !(util::is_functor<T, void(array_context<>)>::value ||
72  util::is_functor<T, void(single_context)>::value ||
73  std::is_same<typename std::remove_reference<T>::type, const finalize_type>::value),
75  operator<<(T&& t) {
76  _core->append(std::forward<T>(t));
77  return *this;
78  }
79 
88  template <typename Func>
89  BSONCXX_INLINE typename std::enable_if<(util::is_functor<Func, void(array_context<>)>::value ||
90  util::is_functor<Func, void(single_context)>::value),
92  operator<<(Func&& func) {
93  func(*this);
94  return *this;
95  }
96 
108  template <typename T>
109  BSONCXX_INLINE typename std::enable_if<
110  std::is_same<base, closed_context>::value &&
111  std::is_same<typename std::remove_reference<T>::type, const finalize_type>::value,
112  // TODO(MSVC): This should just be 'array::value', but
113  // VS2015U1 can't resolve the name.
115  operator<<(T&&) {
116  return _core->extract_array();
117  }
118 
126  _core->open_document();
127  return wrap_document();
128  }
129 
140  _core->concatenate(array.view());
141  return *this;
142  }
143 
151  _core->open_array();
152  return wrap_array();
153  }
154 
161  BSONCXX_INLINE base operator<<(const close_array_type) {
162  _core->close_array();
163  return unwrap();
164  }
165 
170  BSONCXX_INLINE operator array_context<>() {
171  return array_context<>(_core);
172  }
173 
179  BSONCXX_INLINE operator single_context();
180 
181  private:
182  BSONCXX_INLINE base unwrap() {
183  return base(_core);
184  }
185 
186  BSONCXX_INLINE array_context<array_context> wrap_array() {
187  return array_context<array_context>(_core);
188  }
189 
190  BSONCXX_INLINE key_context<array_context> wrap_document() {
191  return key_context<array_context>(_core);
192  }
193 
194  core* _core;
195 };
196 
197 } // namespace stream
198 } // namespace builder
199 BSONCXX_INLINE_NAMESPACE_END
200 } // namespace bsoncxx
201 
202 #include <bsoncxx/config/postlude.hpp>
bsoncxx
Top level namespace for MongoDB C++ BSON functionality.
Definition: element.hpp:24
bsoncxx::builder::stream::array
A streaming interface for constructing a BSON array.
Definition: array.hpp:40
bsoncxx::builder::stream::close_array_type
The type of a stream manipulator to close a subarray.
Definition: helpers.hpp:71
bsoncxx::builder::stream::open_array_type
The type of a stream manipulator to open a subarray.
Definition: helpers.hpp:56
bsoncxx::builder::stream::open_document_type
The type of a stream manipulator to open a subdocument.
Definition: helpers.hpp:32
bsoncxx::builder::concatenate_array
Container to concatenate an array.
Definition: concatenate.hpp:62
bsoncxx::builder::stream::finalize_type
The type of a stream manipulator to finalize a document.
Definition: helpers.hpp:83
bsoncxx::builder::stream::array_context::operator<<
std::enable_if< !(util::is_functor< T, void(array_context<>)>::value||util::is_functor< T, void(single_context)>::value||std::is_same< typename std::remove_reference< T >::type, const finalize_type >::value), array_context >::type & operator<<(T &&t)
<< operator for accepting a real value and appending it to the core builder.
Definition: array_context.hpp:75
bsoncxx::builder::core::append
core & append(const types::b_double &value)
Appends a BSON double.
bsoncxx::builder::stream::array_context::operator<<
array_context operator<<(concatenate_array array)
<< operator for concatenating another array.
Definition: array_context.hpp:139
bsoncxx::builder::stream::single_context
A stream context which appends a single value.
Definition: single_context.hpp:36
bsoncxx::builder::stream::array_context::operator<<
base operator<<(const close_array_type)
<< operator for closing a subarray in the core builder.
Definition: array_context.hpp:161
bsoncxx::builder::stream::array_context
A stream context which expects any number of values.
Definition: array_context.hpp:52
bsoncxx::array::value
A read-only BSON array that owns its underlying buffer.
Definition: value.hpp:34
bsoncxx::builder::core::open_array
core & open_array()
Opens a sub-array within this BSON datum.
bsoncxx::builder::core::close_array
core & close_array()
Closes the current sub-array within this BSON datum.
bsoncxx::builder::core
A low-level interface for constructing BSON documents and arrays.
Definition: core.hpp:42
bsoncxx::builder::stream::array_context::array_context
array_context(core *core)
Create an array_context given a core builder.
Definition: array_context.hpp:60
bsoncxx::builder::core::open_document
core & open_document()
Opens a sub-document within this BSON datum.
bsoncxx::builder::stream::array::view
bsoncxx::array::view view() const
Definition: array.hpp:50
bsoncxx::type
type
An enumeration of each BSON type.
Definition: types.hpp:46
bsoncxx::builder::core::extract_array
array::value extract_array()
Transfers ownership of the underlying document to the caller.
bsoncxx::builder::core::concatenate
core & concatenate(const document::view &view)
Appends the keys from a BSON document into this BSON datum.
bsoncxx::builder::stream::key_context
A stream context which expects a key, which can later be followed by value, then more key/value pairs...
Definition: array_context.hpp:32