MongoDB C++ Driver  mongocxx-3.10.2
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/builder/stream/array_context-fwd.hpp>
18 #include <bsoncxx/builder/stream/key_context-fwd.hpp>
19 #include <bsoncxx/builder/stream/single_context-fwd.hpp>
20 
21 #include <bsoncxx/array/value.hpp>
22 #include <bsoncxx/builder/concatenate.hpp>
23 #include <bsoncxx/builder/core.hpp>
24 #include <bsoncxx/builder/stream/closed_context.hpp>
25 #include <bsoncxx/builder/stream/helpers.hpp>
26 #include <bsoncxx/stdx/type_traits.hpp>
27 
28 #include <bsoncxx/config/prelude.hpp>
29 
30 namespace bsoncxx {
31 namespace v_noabi {
32 namespace builder {
33 namespace stream {
34 
50 template <class base>
52  public:
59  BSONCXX_INLINE array_context(core* core) : _core(core) {}
60 
68  template <class T>
69  BSONCXX_INLINE detail::requires_not_t<array_context&,
70  detail::is_invocable<T, array_context<>>,
71  detail::is_invocable<T, single_context>,
72  detail::is_alike<T, finalize_type>>
73  operator<<(T&& t) {
74  _core->append(std::forward<T>(t));
75  return *this;
76  }
77 
86  template <typename Func>
87  BSONCXX_INLINE
88  detail::requires_t<array_context&,
89  detail::disjunction<detail::is_invocable<Func, array_context>,
90  detail::is_invocable<Func, single_context>>>
91  operator<<(Func&& func) {
92  detail::invoke(std::forward<Func>(func), *this);
93  return *this;
94  }
95 
106  template <typename T>
107  BSONCXX_INLINE detail::requires_t<bsoncxx::v_noabi::array::value,
108  std::is_same<base, closed_context>,
109  detail::is_alike<T, finalize_type>>
110  // VS2015U1 can't resolve the name.
111  operator<<(T&&) {
112  return _core->extract_array();
113  }
114 
120  BSONCXX_INLINE key_context<array_context> operator<<(const open_document_type) {
121  _core->open_document();
122  return wrap_document();
123  }
124 
135  _core->concatenate(array.view());
136  return *this;
137  }
138 
145  _core->open_array();
146  return wrap_array();
147  }
148 
154  BSONCXX_INLINE base operator<<(const close_array_type) {
155  _core->close_array();
156  return unwrap();
157  }
158 
163  BSONCXX_INLINE operator array_context<>() {
164  return array_context<>(_core);
165  }
166 
172  BSONCXX_INLINE operator single_context();
173 
174  private:
175  BSONCXX_INLINE base unwrap() {
176  return base(_core);
177  }
178 
179  BSONCXX_INLINE array_context<array_context> wrap_array() {
180  return array_context<array_context>(_core);
181  }
182 
183  BSONCXX_INLINE key_context<array_context> wrap_document() {
184  return key_context<array_context>(_core);
185  }
186 
187  core* _core;
188 };
189 
190 } // namespace stream
191 } // namespace builder
192 } // namespace v_noabi
193 } // namespace bsoncxx
194 
195 #include <bsoncxx/config/postlude.hpp>
A read-only BSON array that owns its underlying buffer.
Definition: value.hpp:36
A low-level interface for constructing BSON documents and arrays.
Definition: core.hpp:45
core & append(const types::b_double &value)
Appends a BSON double.
bsoncxx::v_noabi::array::value extract_array()
Transfers ownership of the underlying document to the caller.
core & concatenate(const bsoncxx::v_noabi::document::view &view)
Appends the keys from a BSON document into this BSON datum.
core & open_array()
Opens a sub-array within this BSON datum.
core & open_document()
Opens a sub-document within this BSON datum.
core & close_array()
Closes the current sub-array within this BSON datum.
A stream context which expects any number of values.
Definition: array_context.hpp:51
array_context operator<<(concatenate_array array)
<< operator for concatenating another array.
Definition: array_context.hpp:134
base operator<<(const close_array_type)
<< operator for closing a subarray in the core builder.
Definition: array_context.hpp:154
array_context(core *core)
Create an array_context given a core builder.
Definition: array_context.hpp:59
detail::requires_not_t< array_context &, detail::is_invocable< T, array_context<> >, detail::is_invocable< T, single_context >, detail::is_alike< T, finalize_type > > operator<<(T &&t)
<< operator for accepting a real value and appending it to the core builder.
Definition: array_context.hpp:73
A streaming interface for constructing a BSON array.
Definition: array.hpp:42
bsoncxx::v_noabi::array::view view() const
Definition: array.hpp:52
A stream context which appends a single value.
Definition: single_context.hpp:38
The top-level namespace for bsoncxx library entities.
Definition: element-fwd.hpp:19
Container to concatenate an array.
Definition: concatenate.hpp:64
The type of a stream manipulator to close a subarray.
Definition: helpers.hpp:71
The type of a stream manipulator to open a subarray.
Definition: helpers.hpp:56