BDE 4.14.0 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`. Note that
164/// this class is an empty POD type.
166
167 // TYPES
168
169 /// Type alias indicating this is a transparent comparator.
170 typedef void is_transparent;
171
172 // CREATORS
173
174 /// Create a `TransparentLess` object.
175 TransparentLess() = default;
176
177 /// Create a `TransparentLess` object. Note that as `TransparentLess`
178 /// is an empty (stateless) type, this operation has no observable
179 /// effect.
180 TransparentLess(const TransparentLess& original) = default;
181
182 /// Destroy this object.
183 ~TransparentLess() = default;
184
185 // MANIPULATORS
186
187 /// Assign to this object the value of the specified `rhs` object, and
188 /// return a reference providing modifiable access to this object.
189 /// Note that as `TransparentLess` is an empty (stateless) type, this
190 /// operation has no observable effect.
192
193 // ACCESSORS
194
195 /// Return `true` if the specified `lhs` is less than the specified
196 /// `rhs` and `false` otherwise.
197 template <class LHS, class RHS>
198 bool operator()(const LHS& lhs, const RHS& rhs) const;
199};
200
201// ============================================================================
202// INLINE DEFINITIONS
203// ============================================================================
204
205 // ----------------------
206 // struct TransparentLess
207 // ----------------------
208
209// ACCESSORS
210template <class LHS, class RHS>
211inline
212bool TransparentLess::operator()(const LHS& lhs, const RHS& rhs) const
213{
214 return lhs < rhs;
215}
216
217} // close package namespace
218
219
220#endif
221
222// ----------------------------------------------------------------------------
223// Copyright 2021 Bloomberg Finance L.P.
224//
225// Licensed under the Apache License, Version 2.0 (the "License");
226// you may not use this file except in compliance with the License.
227// You may obtain a copy of the License at
228//
229// http://www.apache.org/licenses/LICENSE-2.0
230//
231// Unless required by applicable law or agreed to in writing, software
232// distributed under the License is distributed on an "AS IS" BASIS,
233// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
234// See the License for the specific language governing permissions and
235// limitations under the License.
236// ----------------------------- END-OF-FILE ----------------------------------
237
238/** @} */
239/** @} */
240/** @} */
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bdlb_algorithmworkaroundutil.h:74
Definition bdlb_transparentless.h:165
TransparentLess(const TransparentLess &original)=default
void is_transparent
Type alias indicating this is a transparent comparator.
Definition bdlb_transparentless.h:170
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:212