BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bdlb_cstringless.h
Go to the documentation of this file.
1/// @file bdlb_cstringless.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdlb_cstringless.h -*-C++-*-
8#ifndef INCLUDED_BDLB_CSTRINGLESS
9#define INCLUDED_BDLB_CSTRINGLESS
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bdlb_cstringless bdlb_cstringless
15/// @brief Provide a standard compatible less-than predicate for C-strings.
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdlb
19/// @{
20/// @addtogroup bdlb_cstringless
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdlb_cstringless-purpose"> Purpose</a>
25/// * <a href="#bdlb_cstringless-classes"> Classes </a>
26/// * <a href="#bdlb_cstringless-description"> Description </a>
27/// * <a href="#bdlb_cstringless-usage"> Usage </a>
28/// * <a href="#bdlb_cstringless-example-1-basic-use-of-bdlb-cstringless"> Example 1: Basic Use of bdlb::CStringLess </a>
29///
30/// # Purpose {#bdlb_cstringless-purpose}
31/// Provide a standard compatible less-than predicate for C-strings.
32///
33/// # Classes {#bdlb_cstringless-classes}
34///
35/// - bdlb::CStringLess: a standard compatible less-than predicate for C-strings.
36///
37/// @see bsl_map, bsl_set
38///
39/// # Description {#bdlb_cstringless-description}
40/// This component provides a `struct`, `bdlb::CStringLess`, that
41/// defines a functor that compares two null-terminated strings using a
42/// case-sensitive string comparison, rather than simply comparing the two
43/// addresses (as the `std::less` functor would do). This lexicographical
44/// ordering makes `CStringLess` suitable for supporting C-strings as keys in
45/// associative containers. Note that the container behavior would be undefined
46/// if the strings referenced by such pointers were to change value.
47///
48/// ## Usage {#bdlb_cstringless-usage}
49///
50///
51/// This section illustrates intended use of this component.
52///
53/// ### Example 1: Basic Use of bdlb::CStringLess {#bdlb_cstringless-example-1-basic-use-of-bdlb-cstringless}
54///
55///
56/// Suppose we need a container to store set of unique C-strings. The following
57/// code illustrates how to use `bdlb::CStringLess` as a comparator for the
58/// standard container `set`, to create a set of unique C-string values.
59///
60/// Note that the default comparator for `const char *` (i.e.,
61/// `bsl::less<const char *>`) compares the supplied addresses, rather than the
62/// contents of the C-strings to which those address typically refer. As a
63/// result, when using the default comparator, identical C-string values located
64/// at different addresses, will be successfully added to a `set` container.
65/// `bdlb::CStringLess` compares the values of the C-strings ensuring that a
66/// `set`, using `CstringLess` as a comparator, is a set of unique string
67/// values.
68///
69/// First, we create several C-strings:
70/// @code
71/// const char newYork[] = "NY";
72/// const char losAngeles[] = "LA";
73/// const char newJersey[] = "NJ";
74/// const char sanFrancisco[] = "SF";
75/// const char anotherNewYork[] = "NY";
76/// @endcode
77/// Next, we create two containers, one with default comparator and another
78/// using `bdlb::CstringLess` as a comparator:
79/// @code
80/// bsl::set<const char *> defaultComparatorContainer;
81/// bsl::set<const char *, bdlb::CStringLess> userComparatorContainer;
82/// @endcode
83/// Now, we fill containers with the same contents:
84/// @code
85/// defaultComparatorContainer.insert(newYork);
86/// defaultComparatorContainer.insert(losAngeles);
87/// defaultComparatorContainer.insert(newJersey);
88/// defaultComparatorContainer.insert(sanFrancisco);
89/// defaultComparatorContainer.insert(anotherNewYork);
90///
91/// userComparatorContainer.insert(newYork);
92/// userComparatorContainer.insert(losAngeles);
93/// userComparatorContainer.insert(newJersey);
94/// userComparatorContainer.insert(sanFrancisco);
95/// userComparatorContainer.insert(anotherNewYork);
96/// @endcode
97/// Finally, we observe that the container created with `CStringLess`
98/// (`userComparatorContainer`) contains the correct number of unique C-string
99/// values (4), while the container using the default comparator does not:
100/// @code
101/// assert(5 == defaultComparatorContainer.size());
102/// assert(4 == userComparatorContainer.size());
103/// @endcode
104/// @}
105/** @} */
106/** @} */
107
108/** @addtogroup bdl
109 * @{
110 */
111/** @addtogroup bdlb
112 * @{
113 */
114/** @addtogroup bdlb_cstringless
115 * @{
116 */
117
118#include <bdlscm_version.h>
119
124
125#include <bsls_assert.h>
126#include <bsls_review.h>
127
128#include <bsl_cstring.h>
129
130
131namespace bdlb {
132
133 // ==================
134 // struct CStringLess
135 // ==================
136
137/// This `struct` defines an ordering on null-terminated character strings,
138/// enabling them for use as keys in the standard associative containers
139/// such as `bsl::map` and `bsl::set`. Note that this class is an empty
140/// POD type.
142
143 // STANDARD TYPEDEFS
144 typedef const char *first_argument_type;
145 typedef const char *second_argument_type;
146 typedef bool result_type;
147
148 // TRAITS
152
153 // CREATORS
154
155 /// Create a `CStringLess` object.
156 CStringLess() = default;
157
158 /// Create a `CStringLess` object. Note that as `CStringLess` is an
159 /// empty (stateless) type, this operation has no observable effect.
160 CStringLess(const CStringLess& original) = default;
161
162 /// Destroy this object.
163 ~CStringLess() = default;
164
165 // MANIPULATORS
166
167 /// Assign to this object the value of the specified `rhs` object, and
168 /// return a reference providing modifiable access to this object.
169 /// Note that as `CStringLess` is an empty (stateless) type, this
170 /// operation has no observable effect.
171 CStringLess& operator=(const CStringLess& rhs) = default;
172
173 // ACCESSORS
174
175 /// Return `true` if the specified `lhs` string is lexicographically
176 /// ordered before the specified `rhs` string, and `false` otherwise.
177 /// The behavior is undefined unless both `lhs` and `rhs` point to
178 /// null-terminated strings.
179 bool operator()(const char *lhs, const char *rhs) const;
180};
181
182// ============================================================================
183// INLINE DEFINITIONS
184// ============================================================================
185
186 // ------------------
187 // struct CStringLess
188 // ------------------
189
190// ACCESSORS
191inline
192bool CStringLess::operator()(const char *lhs, const char *rhs) const
193{
194 BSLS_ASSERT(lhs);
195 BSLS_ASSERT(rhs);
196
197 return bsl::strcmp(lhs, rhs) < 0;
198}
199
200} // close package namespace
201
202
203#endif
204
205// ----------------------------------------------------------------------------
206// Copyright 2015 Bloomberg Finance L.P.
207//
208// Licensed under the Apache License, Version 2.0 (the "License");
209// you may not use this file except in compliance with the License.
210// You may obtain a copy of the License at
211//
212// http://www.apache.org/licenses/LICENSE-2.0
213//
214// Unless required by applicable law or agreed to in writing, software
215// distributed under the License is distributed on an "AS IS" BASIS,
216// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
217// See the License for the specific language governing permissions and
218// limitations under the License.
219// ----------------------------- END-OF-FILE ----------------------------------
220
221/** @} */
222/** @} */
223/** @} */
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1804
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bdlb_algorithmworkaroundutil.h:74
Definition bdlb_cstringless.h:141
const char * second_argument_type
Definition bdlb_cstringless.h:145
CStringLess(const CStringLess &original)=default
CStringLess()=default
Create a CStringLess object.
~CStringLess()=default
Destroy this object.
CStringLess & operator=(const CStringLess &rhs)=default
bool operator()(const char *lhs, const char *rhs) const
Definition bdlb_cstringless.h:192
BSLMF_NESTED_TRAIT_DECLARATION(CStringLess, bsl::is_trivially_default_constructible)
const char * first_argument_type
Definition bdlb_cstringless.h:144
bool result_type
Definition bdlb_cstringless.h:146
BSLMF_NESTED_TRAIT_DECLARATION(CStringLess, bsl::is_trivially_copyable)
Definition bslmf_istriviallycopyable.h:329
Definition bslmf_istriviallydefaultconstructible.h:293