BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bdlb_transparentless.h
Go to the documentation of this file.
1/// @file bdlb_transparentless.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdlb_transparentless.h -*-C++-*-
8#ifndef INCLUDED_BDLB_TRANSPARENTLESS
9#define INCLUDED_BDLB_TRANSPARENTLESS
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bdlb_transparentless bdlb_transparentless
15/// @brief Provide a transparent less-than predicate.
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdlb
19/// @{
20/// @addtogroup bdlb_transparentless
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdlb_transparentless-purpose"> Purpose</a>
25/// * <a href="#bdlb_transparentless-classes"> Classes </a>
26/// * <a href="#bdlb_transparentless-description"> Description </a>
27/// * <a href="#bdlb_transparentless-usage"> Usage </a>
28/// * <a href="#bdlb_transparentless-example-1-basic-use-of-bdlb-transparentless"> Example 1: Basic Use of bdlb::TransparentLess </a>
29///
30/// # Purpose {#bdlb_transparentless-purpose}
31/// Provide a transparent less-than predicate.
32///
33/// # Classes {#bdlb_transparentless-classes}
34///
35/// - bdlb::TransparentLess: a transparent less-than predicate.
36///
37/// @see bsl_map, bsl_set
38///
39/// # Description {#bdlb_transparentless-description}
40/// This component provides a `struct`, `bdlb::TransparentLess`,
41/// that defines a functor that can be used as transparent less-than comparator
42/// for heterogeneous lookup.
43///
44/// ## Usage {#bdlb_transparentless-usage}
45///
46///
47/// This section illustrates intended use of this component.
48///
49/// ### Example 1: Basic Use of bdlb::TransparentLess {#bdlb_transparentless-example-1-basic-use-of-bdlb-transparentless}
50///
51///
52/// Suppose we need a container to store set of unique `bsl::strings` objects.
53/// We use `bsl::set` for our container, which uses `bsl::less` as default
54/// comparator. `bsl::less` is suitable if we want to search an entry using a
55/// `bsl::string` object as a key. However, if we were to try and search using
56/// a `bsl::string_view` object the code would not compile, even though the
57/// comparison operator between `bsl::string` and `bsl::string_view` exists. In
58/// addition, implicit conversions where they are available, may lead to
59/// additional memory allocation for temporary objects. The following code
60/// illustrates how to use `bdlb::TransparentLess` as a comparator for the
61/// standard container `set`, in this case to allow a `bsl::set<bsl::string>` to
62/// be searched with a `bsl::string_view`.
63///
64/// First, we create several C-strings:
65/// @code
66/// const char *newYork = "NY";
67/// const char *losAngeles = "LA";
68/// const char *cityWithLongName = "The abbreviation of really really long "
69/// "name of the city. The name is so long "
70/// "that even its abbreviation definitely "
71/// "exceeds Small String Optimization limit";
72/// const char *sanFrancisco = "SF";
73/// @endcode
74/// Next, we create several allocators to precisely control memory allocation:
75/// @code
76/// bslma::TestAllocator ca("container", veryVeryVeryVerbose);
77/// bslma::TestAllocator sa("strings", veryVeryVeryVerbose);
78/// @endcode
79/// And set one of them as default:
80/// @code
81/// bslma::TestAllocator da("default", veryVeryVeryVerbose);
82/// bslma::DefaultAllocatorGuard dag(&da);
83/// @endcode
84/// Then, we create two containers, one with default comparator and another
85/// using `bdlb::TransparentLess` as a comparator:
86/// @code
87/// bsl::set<bsl::string> defaultSet(&ca);
88/// bsl::set<bsl::string, bdlb::TransparentLess> userSet(&ca);
89/// @endcode
90/// Now, we fill the containers with the strings:
91/// @code
92/// bsl::string newYorkStr (newYork, &sa);
93/// bsl::string losAngelesStr (losAngeles, &sa);
94/// bsl::string cityWithLongNameStr(cityWithLongName, &sa);
95/// bsl::string sanFranciscoStr (sanFrancisco, &sa);
96///
97/// defaultSet.insert(newYorkStr );
98/// defaultSet.insert(losAngelesStr );
99/// defaultSet.insert(cityWithLongNameStr);
100///
101/// userSet.insert(newYorkStr );
102/// userSet.insert(losAngelesStr );
103/// userSet.insert(cityWithLongNameStr);
104/// @endcode
105/// Finally, we observe that the container created with `TransparentLess`
106/// container (`userSet`) allows to use @ref string_view object as a key and does
107/// not make any implicit conversions:
108/// @code
109/// bsl::string_view newYorkStrView (newYork );
110/// bsl::string_view cityWithLongNameStrView(cityWithLongName);
111/// bsl::string_view sanFranciscoStrView (sanFrancisco );
112///
113/// assert(userSet.end() != userSet.find(newYorkStrView ));
114/// assert(userSet.end() != userSet.find(cityWithLongNameStrView));
115/// assert(userSet.end() == userSet.find(sanFranciscoStrView ));
116///
117/// assert(0 == da.numBytesTotal());
118///
119/// assert(userSet.end() != userSet.find(newYork ));
120/// assert(userSet.end() != userSet.find(cityWithLongName));
121/// assert(userSet.end() == userSet.find(sanFrancisco ));
122///
123/// assert(0 == da.numBytesTotal());
124/// @endcode
125/// While the container using the default comparator implicitly converts
126/// C-strings to `bsl::string` objects:
127/// @code
128/// assert(defaultSet.end() != defaultSet.find(newYork ));
129/// assert(defaultSet.end() != defaultSet.find(cityWithLongName));
130/// assert(defaultSet.end() == defaultSet.find(sanFrancisco ));
131///
132/// assert(0 < da.numBytesTotal());
133/// @endcode
134/// @}
135/** @} */
136/** @} */
137
138/** @addtogroup bdl
139 * @{
140 */
141/** @addtogroup bdlb
142 * @{
143 */
144/** @addtogroup bdlb_transparentless
145 * @{
146 */
147
148#include <bdlscm_version.h>
149
153
154
155namespace bdlb {
156
157 // ======================
158 // struct TransparentLess
159 // ======================
160
161/// This `struct` defines an ordering on objects of different types,
162/// enabling them for use for heterogeneous comparison in the standard
163/// associative containers such as `bsl::map` and `bsl::set`.
164///
165/// \note Note that this class is an empty POD type.
166///
167/// See @ref bdlb_transparentless
169
170 // TYPES
171
172 /// Type alias indicating this is a transparent comparator.
173 typedef void is_transparent;
174
175 // CREATORS
176
177 /// Create a `TransparentLess` object.
178 TransparentLess() = default;
179
180 /// Create a `TransparentLess` object.
181 /// \note Note that as `TransparentLess`
182 /// is an empty (stateless) type, this operation has no observable
183 /// effect.
184 TransparentLess(const TransparentLess& original) = default;
185
186 /// Destroy this object.
187 ~TransparentLess() = default;
188
189 // MANIPULATORS
190
191 /// Assign to this object the value of the specified `rhs` object, and
192 /// return a reference providing modifiable access to this object.
193 ///
194 /// \note Note that as `TransparentLess` is an empty (stateless) type, this
195 /// operation has no observable effect.
197
198 // ACCESSORS
199
200 /// Return `true` if the specified `lhs` is less than the specified
201 /// `rhs` and `false` otherwise.
202 template <class LHS, class RHS>
203 bool operator()(const LHS& lhs, const RHS& rhs) const;
204};
205
206// ============================================================================
207// INLINE DEFINITIONS
208// ============================================================================
209
210 // ----------------------
211 // struct TransparentLess
212 // ----------------------
213
214// ACCESSORS
215template <class LHS, class RHS>
216inline
217bool TransparentLess::operator()(const LHS& lhs, const RHS& rhs) const
218{
219 return lhs < rhs;
220}
221
222} // close package namespace
223
224
225#endif
226
227// ----------------------------------------------------------------------------
228// Copyright 2021 Bloomberg Finance L.P.
229//
230// Licensed under the Apache License, Version 2.0 (the "License");
231// you may not use this file except in compliance with the License.
232// You may obtain a copy of the License at
233//
234// http://www.apache.org/licenses/LICENSE-2.0
235//
236// Unless required by applicable law or agreed to in writing, software
237// distributed under the License is distributed on an "AS IS" BASIS,
238// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
239// See the License for the specific language governing permissions and
240// limitations under the License.
241// ----------------------------- END-OF-FILE ----------------------------------
242
243/** @} */
244/** @} */
245/** @} */
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bdlb_algorithmworkaroundutil.h:74
Definition bdlb_transparentless.h:168
TransparentLess(const TransparentLess &original)=default
void is_transparent
Type alias indicating this is a transparent comparator.
Definition bdlb_transparentless.h:173
TransparentLess & operator=(const TransparentLess &rhs)=default
~TransparentLess()=default
Destroy this object.
TransparentLess()=default
Create a TransparentLess object.
bool operator()(const LHS &lhs, const RHS &rhs) const
Definition bdlb_transparentless.h:217