MongoDB C++ Driver current
Loading...
Searching...
No Matches
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/stdx/type_traits.hpp>
23
24#include <bsoncxx/config/prelude.hpp>
25
26namespace bsoncxx {
27inline namespace v_noabi {
28namespace builder {
29namespace stream {
30
31template <class T>
32class key_context;
33
34class single_context;
35
51template <class base = closed_context>
53 public:
60 BSONCXX_INLINE array_context(core* core) : _core(core) {}
61
69 template <class T>
70 BSONCXX_INLINE detail::requires_not_t<array_context&,
71 detail::is_invocable<T, array_context<>>,
72 detail::is_invocable<T, single_context>,
73 detail::is_alike<T, finalize_type>>
74 operator<<(T&& t) {
75 _core->append(std::forward<T>(t));
76 return *this;
77 }
78
87 template <typename Func>
88 BSONCXX_INLINE
89 detail::requires_t<array_context&,
90 detail::disjunction<detail::is_invocable<Func, array_context>,
91 detail::is_invocable<Func, single_context>>>
92 operator<<(Func&& func) {
93 detail::invoke(std::forward<Func>(func), *this);
94 return *this;
95 }
96
107 template <typename T>
108 BSONCXX_INLINE detail::requires_t<bsoncxx::array::value,
109 std::is_same<base, closed_context>,
110 detail::is_alike<T, finalize_type>>
111 // VS2015U1 can't resolve the name.
112 operator<<(T&&) {
113 return _core->extract_array();
114 }
115
121 BSONCXX_INLINE key_context<array_context> operator<<(const open_document_type) {
122 _core->open_document();
123 return wrap_document();
124 }
125
136 _core->concatenate(array.view());
137 return *this;
138 }
139
146 _core->open_array();
147 return wrap_array();
148 }
149
155 BSONCXX_INLINE base operator<<(const close_array_type) {
156 _core->close_array();
157 return unwrap();
158 }
159
164 BSONCXX_INLINE operator array_context<>() {
165 return array_context<>(_core);
166 }
167
173 BSONCXX_INLINE operator single_context();
174
175 private:
176 BSONCXX_INLINE base unwrap() {
177 return base(_core);
178 }
179
180 BSONCXX_INLINE array_context<array_context> wrap_array() {
181 return array_context<array_context>(_core);
182 }
183
184 BSONCXX_INLINE key_context<array_context> wrap_document() {
185 return key_context<array_context>(_core);
186 }
187
188 core* _core;
189};
190
191} // namespace stream
192} // namespace builder
193} // namespace v_noabi
194} // namespace bsoncxx
195
196#include <bsoncxx/config/postlude.hpp>
A low-level interface for constructing BSON documents and arrays.
Definition core.hpp:43
core & append(const types::b_double &value)
Appends a BSON double.
core & open_document()
Opens a sub-document within this BSON datum.
core & close_array()
Closes the current sub-array within this BSON datum.
core & open_array()
Opens a sub-array within this BSON datum.
array::value extract_array()
Transfers ownership of the underlying document to the caller.
core & concatenate(const document::view &view)
Appends the keys from a BSON document into this BSON datum.
A stream context which expects any number of values.
Definition array_context.hpp:52
array_context operator<<(concatenate_array array)
<< operator for concatenating another array.
Definition array_context.hpp:135
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:74
base operator<<(const close_array_type)
<< operator for closing a subarray in the core builder.
Definition array_context.hpp:155
array_context(core *core)
Create an array_context given a core builder.
Definition array_context.hpp:60
A streaming interface for constructing a BSON array.
Definition array.hpp:40
bsoncxx::array::view view() const
Definition array.hpp:50
A stream context which appends a single value.
Definition single_context.hpp:36
The top-level namespace for bsoncxx library entities.
Definition element.hpp:24
Container to concatenate an array.
Definition concatenate.hpp:62
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