BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bdlb_pairutil.h
Go to the documentation of this file.
1/// @file bdlb_pairutil.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdlb_pairutil.h -*-C++-*-
8#ifndef INCLUDED_BDLB_PAIRUTIL
9#define INCLUDED_BDLB_PAIRUTIL
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id$ $CSID$")
13
14/// @defgroup bdlb_pairutil bdlb_pairutil
15/// @brief Provide a function that creates a pair of references.
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdlb
19/// @{
20/// @addtogroup bdlb_pairutil
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdlb_pairutil-purpose"> Purpose</a>
25/// * <a href="#bdlb_pairutil-classes"> Classes </a>
26/// * <a href="#bdlb_pairutil-description"> Description </a>
27/// * <a href="#bdlb_pairutil-usage"> Usage </a>
28/// * <a href="#bdlb_pairutil-example-1-basic-usage"> Example 1: Basic Usage </a>
29///
30/// # Purpose {#bdlb_pairutil-purpose}
31/// Provide a function that creates a pair of references.
32///
33/// # Classes {#bdlb_pairutil-classes}
34///
35/// - bdlb::PairUtil: namespace for the `bdlb::PairUtil::tie` function
36///
37/// # Description {#bdlb_pairutil-description}
38/// This component provides the class `bdlb::PairUtil`, which has a
39/// single static member function, `tie`, which is intended to be used in place
40/// of `bsl::tie` when the right-hand side of the assignment is a `bsl::pair`.
41///
42/// ## Usage {#bdlb_pairutil-usage}
43///
44///
45/// This section illustrates intended use of this component.
46///
47/// ### Example 1: Basic Usage {#bdlb_pairutil-example-1-basic-usage}
48///
49///
50/// Suppose we need to implement a function that takes a `bsl::map` and stores
51/// into out-parameters the key and value corresponding to the first entry in
52/// the map. Using `bsl::map`s container interface, we can obtain a reference
53/// to a `bsl::pair` of the key and value. We can then use
54/// `bdlb::PairUtil::tie` to assign from both the key and value in a single
55/// expression:
56/// @code
57/// /// Load into the specified `key` and the specified `value` the key and
58/// /// value for the first entry in the specified `map` and return `true`,
59/// /// or else fail by storing 0 and an empty string and return `false`
60/// /// when `map` is empty.
61/// bool getFirst(int *key,
62/// bsl::string *value,
63/// const bsl::map<int, bsl::string>& map)
64/// {
65/// if (map.empty()) {
66/// *key = 0;
67/// value->clear();
68/// return false; // RETURN
69/// }
70/// bdlb::PairUtil::tie(*key, *value) = *map.begin();
71/// return true;
72/// }
73///
74/// void usageExample()
75/// {
76/// bsl::map<int, bsl::string> map;
77/// map[30782530] = "bbi10";
78///
79/// int uuid;
80/// bsl::string username;
81///
82/// bool result = getFirst(&uuid, &username, map);
83/// assert(result);
84/// assert(30782530 == uuid);
85/// assert("bbi10" == username);
86/// }
87/// @endcode
88/// @}
89/** @} */
90/** @} */
91
92/** @addtogroup bdl
93 * @{
94 */
95/** @addtogroup bdlb
96 * @{
97 */
98/** @addtogroup bdlb_pairutil
99 * @{
100 */
101
102#include <bdlscm_version.h>
103
104#include <bsl_utility.h>
105
106
107namespace bdlb {
108
109 // ==============
110 // class PairUtil
111 // ==============
112
113/// This `struct` provides a namespace for the `tie` static function.
114struct PairUtil {
115
116 // CLASS METHODS
117
118 /// Return a `bsl::pair` object holding a reference to the specified
119 /// `first` and a reference to the specified `second`, respectively.
120 template <class t_FIRST, class t_SECOND>
121 static bsl::pair<t_FIRST&, t_SECOND&> tie(t_FIRST& first,
122 t_SECOND& second);
123};
124
125// ============================================================================
126// INLINE DEFINITIONS
127// ============================================================================
128
129 // ---------------
130 // struct PairUtil
131 // ---------------
132
133// CLASS METHODS
134template <class t_FIRST, class t_SECOND>
135inline
136bsl::pair<t_FIRST&, t_SECOND&> PairUtil::tie(t_FIRST& first, t_SECOND& second)
137{
138 return bsl::pair<t_FIRST&, t_SECOND&>(first, second);
139}
140
141} // close package namespace
142
143
144#endif
145
146// ----------------------------------------------------------------------------
147// Copyright 2023 Bloomberg Finance L.P.
148//
149// Licensed under the Apache License, Version 2.0 (the "License");
150// you may not use this file except in compliance with the License.
151// You may obtain a copy of the License at
152//
153// http://www.apache.org/licenses/LICENSE-2.0
154//
155// Unless required by applicable law or agreed to in writing, software
156// distributed under the License is distributed on an "AS IS" BASIS,
157// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
158// See the License for the specific language governing permissions and
159// limitations under the License.
160// ----------------------------- END-OF-FILE ----------------------------------
161
162/** @} */
163/** @} */
164/** @} */
Definition bslstl_pair.h:1210
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bdlb_algorithmworkaroundutil.h:74
This struct provides a namespace for the tie static function.
Definition bdlb_pairutil.h:114
static bsl::pair< t_FIRST &, t_SECOND & > tie(t_FIRST &first, t_SECOND &second)
Definition bdlb_pairutil.h:136