1414
1515#pragma once
1616
17+ #include < mongocxx/read_preference-fwd.hpp> // IWYU pragma: export
18+
19+ //
20+
21+ #include < bsoncxx/v1/detail/macros.hpp>
22+
23+ #include < mongocxx/v1/read_preference.hpp>
24+
1725#include < chrono>
1826#include < cstdint>
19- #include < memory>
27+ #include < memory> // IWYU pragma: keep: backward compatibility, to be removed.
2028#include < string> // IWYU pragma: keep: backward compatibility, to be removed.
29+ #include < utility>
2130
22- #include < mongocxx/client-fwd.hpp>
23- #include < mongocxx/collection-fwd.hpp>
24- #include < mongocxx/database-fwd.hpp>
25- #include < mongocxx/events/topology_description-fwd.hpp>
26- #include < mongocxx/options/transaction-fwd.hpp>
27- #include < mongocxx/read_preference-fwd.hpp> // IWYU pragma: export
28- #include < mongocxx/search_index_view-fwd.hpp>
29- #include < mongocxx/uri-fwd.hpp>
31+ #include < mongocxx/client-fwd.hpp> // IWYU pragma: keep: backward compatibility, to be removed.
32+ #include < mongocxx/collection-fwd.hpp> // IWYU pragma: keep: backward compatibility, to be removed.
33+ #include < mongocxx/database-fwd.hpp> // IWYU pragma: keep: backward compatibility, to be removed.
34+ #include < mongocxx/events/topology_description-fwd.hpp> // IWYU pragma: keep: backward compatibility, to be removed.
35+ #include < mongocxx/options/transaction-fwd.hpp> // IWYU pragma: keep: backward compatibility, to be removed.
36+ #include < mongocxx/search_index_view-fwd.hpp> // IWYU pragma: keep: backward compatibility, to be removed.
37+ #include < mongocxx/uri-fwd.hpp> // IWYU pragma: keep: backward compatibility, to be removed.
3038
39+ #include < bsoncxx/array/view.hpp>
3140#include < bsoncxx/array/view_or_value.hpp>
41+ #include < bsoncxx/document/view.hpp>
3242#include < bsoncxx/document/view_or_value.hpp>
3343#include < bsoncxx/stdx/optional.hpp>
3444
35- #include < mongocxx/options/transaction.hpp>
45+ #include < mongocxx/options/transaction.hpp> // IWYU pragma: keep: backward compatibility, to be removed.
3646
3747#include < mongocxx/config/prelude.hpp>
3848
@@ -47,6 +57,9 @@ namespace v_noabi {
4757// / - [Read Preference (MongoDB Manual)](https://www.mongodb.com/docs/manual/core/read-preference/)
4858// /
4959class read_preference {
60+ private:
61+ v1::read_preference _rp;
62+
5063 public:
5164 // /
5265 // / Determines which members in a replica set are acceptable to read from.
@@ -90,7 +103,31 @@ class read_preference {
90103 // /
91104 // / Constructs a new read_preference with read_mode set to k_primary.
92105 // /
93- MONGOCXX_ABI_EXPORT_CDECL () read_preference();
106+ read_preference () = default ;
107+
108+ // /
109+ // / Construct with the @ref mongocxx::v1 equivalent.
110+ // /
111+ /* explicit(false) */ read_preference(v1::read_preference rp) : _rp{std::move (rp)} {}
112+
113+ // /
114+ // / Convert to the @ref mongocxx::v1 equivalent.
115+ // /
116+ // / @par Postconditions:
117+ // / - `other` is in an assign-or-destroy-only state.
118+ // /
119+ // / @warning Invalidates all associated iterators and views.
120+ // /
121+ explicit operator v1::read_preference () && {
122+ return std::move (_rp);
123+ }
124+
125+ // /
126+ // / Convert to the @ref mongocxx::v1 equivalent.
127+ // /
128+ explicit operator v1::read_preference () const & {
129+ return _rp;
130+ }
94131
95132 // @cond DOXYGEN_DISABLE
96133 struct deprecated_tag {};
@@ -104,10 +141,12 @@ class read_preference {
104141 // /
105142 // / @deprecated Use @ref mode instead.
106143 // /
107- MONGOCXX_DEPRECATED MONGOCXX_ABI_EXPORT_CDECL () read_preference(read_mode mode);
144+ MONGOCXX_DEPRECATED read_preference (read_mode mode) : read_preference{mode, deprecated_tag{}} {}
108145
109146 // @cond DOXYGEN_DISABLE
110- MONGOCXX_ABI_EXPORT_CDECL () read_preference(read_mode mode, deprecated_tag);
147+ read_preference (read_mode mode, deprecated_tag) {
148+ this ->mode (mode);
149+ }
111150 // @endcond
112151
113152 // /
@@ -123,40 +162,15 @@ class read_preference {
123162 // /
124163 // / @deprecated Use @ref tags instead.
125164 // /
126- MONGOCXX_DEPRECATED MONGOCXX_ABI_EXPORT_CDECL () read_preference(
127- read_mode mode,
128- bsoncxx::v_noabi::document::view_or_value tags);
165+ MONGOCXX_DEPRECATED read_preference (read_mode mode, bsoncxx::v_noabi::document::view_or_value tags)
166+ : read_preference{mode, tags, deprecated_tag{}} {}
129167
130168 // @cond DOXYGEN_DISABLE
131- MONGOCXX_ABI_EXPORT_CDECL ()
132- read_preference (read_mode mode, bsoncxx::v_noabi::document::view_or_value tags, deprecated_tag);
169+ read_preference (read_mode mode, bsoncxx::v_noabi::document::view_or_value tags, deprecated_tag) {
170+ this ->mode (mode).tags (tags.view ());
171+ }
133172 // @endcond
134173
135- // /
136- // / Copy constructs a read_preference.
137- // /
138- MONGOCXX_ABI_EXPORT_CDECL () read_preference(read_preference const &);
139-
140- // /
141- // / Copy assigns a read_preference.
142- // /
143- MONGOCXX_ABI_EXPORT_CDECL (read_preference&) operator =(read_preference const &);
144-
145- // /
146- // / Move constructs a read_preference.
147- // /
148- MONGOCXX_ABI_EXPORT_CDECL () read_preference(read_preference&&) noexcept ;
149-
150- // /
151- // / Move assigns a read_preference.
152- // /
153- MONGOCXX_ABI_EXPORT_CDECL (read_preference&) operator =(read_preference&&) noexcept ;
154-
155- // /
156- // / Destroys a read_preference.
157- // /
158- MONGOCXX_ABI_EXPORT_CDECL () ~read_preference ();
159-
160174 // /
161175 // / Sets a new mode for this read_preference.
162176 // /
@@ -167,14 +181,19 @@ class read_preference {
167181 // / A reference to the object on which this member function is being called. This facilitates
168182 // / method chaining.
169183 // /
170- MONGOCXX_ABI_EXPORT_CDECL (read_preference&) mode(read_mode mode);
184+ read_preference& mode (read_mode mode) {
185+ _rp.mode (static_cast <v1::read_preference::read_mode>(mode));
186+ return *this ;
187+ }
171188
172189 // /
173190 // / Returns the current read_mode for this read_preference.
174191 // /
175192 // / @return The current read_mode.
176193 // /
177- MONGOCXX_ABI_EXPORT_CDECL (read_mode) mode() const ;
194+ read_mode mode () const {
195+ return static_cast <read_mode>(_rp.mode ());
196+ }
178197
179198 // /
180199 // / Sets or updates the tag set list for this read_preference.
@@ -189,8 +208,10 @@ class read_preference {
189208 // / A reference to the object on which this member function is being called. This facilitates
190209 // / method chaining.
191210 // /
192- MONGOCXX_ABI_EXPORT_CDECL (read_preference&)
193- tags (bsoncxx::v_noabi::document::view_or_value tag_set_list);
211+ read_preference& tags (bsoncxx::v_noabi::document::view_or_value tag_set_list) {
212+ _rp.tags (bsoncxx::v_noabi::to_v1 (tag_set_list.view ()));
213+ return *this ;
214+ }
194215
195216 // /
196217 // / Sets or updates the tag set list for this read_preference.
@@ -205,19 +226,30 @@ class read_preference {
205226 // / A reference to the object on which this member function is being called. This facilitates
206227 // / method chaining.
207228 // /
208- MONGOCXX_ABI_EXPORT_CDECL (read_preference&)
209- tags (bsoncxx::v_noabi::array::view_or_value tag_set_list);
229+ read_preference& tags (bsoncxx::v_noabi::array::view_or_value tag_set_list) {
230+ _rp.tags (bsoncxx::v_noabi::to_v1 (tag_set_list.view ()));
231+ return *this ;
232+ }
210233
211234 // /
212- // / Sets or updates the tag set list for this read_preference.
235+ // / Returns the current tag set list for this read_preference.
213236 // /
214237 // / @return The optionally set current tag set list.
215238 // /
216239 // / @see
217240 // / - https://www.mongodb.com/docs/manual/core/read-preference-tags/
218241 // /
219- MONGOCXX_ABI_EXPORT_CDECL (bsoncxx::v_noabi::stdx::optional<bsoncxx::v_noabi::document::view>)
220- tags () const ;
242+ bsoncxx::v_noabi::stdx::optional<bsoncxx::v_noabi::document::view> tags () const {
243+ bsoncxx::v_noabi::stdx::optional<bsoncxx::v_noabi::document::view> ret;
244+
245+ auto const v = _rp.tags ();
246+
247+ if (!v.empty ()) {
248+ ret.emplace (v);
249+ }
250+
251+ return ret;
252+ }
221253
222254 // /
223255 // / Sets the max staleness setting for this read_preference. Secondary
@@ -256,8 +288,13 @@ class read_preference {
256288 // /
257289 // / @return The optionally current max staleness setting.
258290 // /
259- MONGOCXX_ABI_EXPORT_CDECL (bsoncxx::v_noabi::stdx::optional<std::chrono::seconds>)
260- max_staleness () const ;
291+ bsoncxx::v_noabi::stdx::optional<std::chrono::seconds> max_staleness () const {
292+ return _rp.max_staleness ();
293+ }
294+
295+ BSONCXX_PRIVATE_WARNINGS_PUSH ();
296+ BSONCXX_PRIVATE_WARNINGS_DISABLE (GNU(" -Wdeprecated-declarations" ));
297+ BSONCXX_PRIVATE_WARNINGS_DISABLE (MSVC(4996 ));
261298
262299 // /
263300 // / Sets the hedge document to be used for the read preference. Sharded clusters running MongoDB
@@ -279,8 +316,10 @@ class read_preference {
279316 // / @return A reference to the object on which this member function is being called. This
280317 // / facilitates method chaining.
281318 // /
282- MONGOCXX_DEPRECATED MONGOCXX_ABI_EXPORT_CDECL (read_preference&) hedge(
283- bsoncxx::v_noabi::document::view_or_value hedge);
319+ MONGOCXX_DEPRECATED read_preference& hedge (bsoncxx::v_noabi::document::view_or_value hedge) {
320+ _rp.hedge (bsoncxx::v_noabi::to_v1 (hedge.view ()));
321+ return *this ;
322+ }
284323
285324 // /
286325 // / Gets the current hedge document to be used for the read preference.
@@ -290,34 +329,48 @@ class read_preference {
290329 // / @return A hedge document if one was set.
291330 // /
292331 MONGOCXX_DEPRECATED MONGOCXX_ABI_EXPORT_CDECL (bsoncxx::v_noabi::stdx::optional<bsoncxx::v_noabi::document::view> const )
293- hedge() const ;
332+ hedge() const {
333+ bsoncxx::v_noabi::stdx::optional<bsoncxx::v_noabi::document::view> ret;
334+ if (auto const opt = _rp.hedge ()) {
335+ ret.emplace (*opt);
336+ }
337+ return ret;
338+ }
339+
340+ BSONCXX_PRIVATE_WARNINGS_POP ();
294341
295342 // /
296343 // / @relates mongocxx::v_noabi::read_preference
297344 // /
298345 // / Compares two read_preference objects for (in)-equality.
299346 // /
300347 // / @{
301- friend MONGOCXX_ABI_EXPORT_CDECL (bool ) operator==(read_preference const &, read_preference const &);
302- friend MONGOCXX_ABI_EXPORT_CDECL (bool ) operator!=(read_preference const &, read_preference const &);
348+ friend bool operator ==(read_preference const & lhs, read_preference const & rhs) {
349+ return (lhs.mode () == rhs.mode ()) && (lhs.tags () == rhs.tags ()) && (lhs.max_staleness () == rhs.max_staleness ());
350+ }
351+
352+ friend bool operator !=(read_preference const & lhs, read_preference const & rhs) {
353+ return !(lhs == rhs);
354+ }
303355 // / @}
304356 // /
305357
306- private:
307- friend ::mongocxx::v_noabi::client;
308- friend ::mongocxx::v_noabi::collection;
309- friend ::mongocxx::v_noabi::database;
310- friend ::mongocxx::v_noabi::events::topology_description;
311- friend ::mongocxx::v_noabi::options::transaction;
312- friend ::mongocxx::v_noabi::search_index_view;
313- friend ::mongocxx::v_noabi::uri;
314-
315- class impl ;
358+ class internal ;
359+ };
316360
317- read_preference (std::unique_ptr<impl>&& implementation);
361+ // /
362+ // / Convert to the @ref mongocxx::v_noabi equivalent of `v`.
363+ // /
364+ inline v_noabi::read_preference from_v1 (v1::read_preference v) {
365+ return {std::move (v)};
366+ }
318367
319- std::unique_ptr<impl> _impl;
320- };
368+ // /
369+ // / Convert to the @ref mongocxx::v1 equivalent of `v`.
370+ // /
371+ inline v1::read_preference to_v1 (v_noabi::read_preference v) {
372+ return v1::read_preference{std::move (v)};
373+ }
321374
322375} // namespace v_noabi
323376} // namespace mongocxx
@@ -328,3 +381,6 @@ class read_preference {
328381// / @file
329382// / Provides @ref mongocxx::v_noabi::read_preference.
330383// /
384+ // / @par Includes
385+ // / - @ref mongocxx/v1/read_preference.hpp
386+ // /
0 commit comments