BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bdlb_cstringequalto.h
Go to the documentation of this file.
1/// @file bdlb_cstringequalto.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdlb_cstringequalto.h -*-C++-*-
8#ifndef INCLUDED_BDLB_CSTRINGEQUALTO
9#define INCLUDED_BDLB_CSTRINGEQUALTO
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bdlb_cstringequalto bdlb_cstringequalto
15/// @brief Provide a standard compatible equality predicate for C-strings.
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdlb
19/// @{
20/// @addtogroup bdlb_cstringequalto
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdlb_cstringequalto-purpose"> Purpose</a>
25/// * <a href="#bdlb_cstringequalto-classes"> Classes </a>
26/// * <a href="#bdlb_cstringequalto-description"> Description </a>
27/// * <a href="#bdlb_cstringequalto-usage"> Usage </a>
28/// * <a href="#bdlb_cstringequalto-example-1-basic-use-of-bdlb-cstringequalto"> Example 1: Basic Use of bdlb::CStringEqualTo </a>
29///
30/// # Purpose {#bdlb_cstringequalto-purpose}
31/// Provide a standard compatible equality predicate for C-strings.
32///
33/// # Classes {#bdlb_cstringequalto-classes}
34///
35/// - bdlb::CStringEqualTo: a standard compatible C-string equality predicate
36///
37/// @see
38///
39/// # Description {#bdlb_cstringequalto-description}
40/// This component provides a `struct`, `bdlb::CStringEqualTo`,
41/// that defines a functor that checks two null-terminated strings for equality
42/// using a case-sensitive string comparison, rather than simply comparing the
43/// two addresses (as the `std::equal_to` functor would do). This comparison
44/// functor is suitable for supporting C-strings as keys in unordered
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_cstringequalto-usage}
49///
50///
51/// This section illustrates intended use of this component.
52///
53/// ### Example 1: Basic Use of bdlb::CStringEqualTo {#bdlb_cstringequalto-example-1-basic-use-of-bdlb-cstringequalto}
54///
55///
56/// The following snippets of code illustrate how to create and use a
57/// `bdlb::CStringEqualTo` object as a binary predicate for the standard library
58/// function `bsl::equal` to test that two ranges of null-terminated character
59/// strings are equal.
60///
61/// First, we create few sequences with null-terminated character strings,
62/// making sure that their elements have different memory addresses:
63/// @code
64/// const char hello1[] = { 'h', 'e', 'l', 'l', 'o', 0};
65/// const char hello2[] = { 'h', 'e', 'l', 'l', 'o', 0};
66///
67/// const char* arrayA[3] = { "A", "B", hello1 };
68/// const char* arrayB[3] = { "A", "B", hello2 };
69/// @endcode
70/// Now, use bdlb::CStringEqualTo() as a binary predicate to compare sequences:
71/// @code
72/// bool bdlbEqualTo = bsl::equal(arrayA, arrayA+3, arrayB,
73/// bdlb::CStringEqualTo());
74/// bool bslEqualTo = bsl::equal(arrayA, arrayA+3, arrayB,
75/// bsl::equal_to<const char *>());
76/// @endcode
77/// Finally, we observe that `bdlb::CStringEqualTo` compares character string by
78/// their values, while the default comparator compares addresses:
79/// @code
80/// assert( true == bdlbEqualTo );
81/// assert( false == bslEqualTo );
82/// @endcode
83/// @}
84/** @} */
85/** @} */
86
87/** @addtogroup bdl
88 * @{
89 */
90/** @addtogroup bdlb
91 * @{
92 */
93/** @addtogroup bdlb_cstringequalto
94 * @{
95 */
96
97#include <bdlscm_version.h>
98
103
104#include <bsls_assert.h>
105#include <bsls_review.h>
106
107#include <bsl_cstring.h>
108
109
110namespace bdlb {
111
112 // =====================
113 // struct CStringEqualTo
114 // =====================
115
116/// This `struct` defines a comparison functor on null-terminated character
117/// strings, enabling them for use as keys in the standard unordered
118/// associative containers such as `bsl::unordered_map` and `bsl::unordered_set`.
119///
120/// \note Note that this class is an empty POD type.
121///
122/// See @ref bdlb_cstringequalto
124
125 // STANDARD TYPEDEFS
126 typedef const char *first_argument_type;
127 typedef const char *second_argument_type;
128 typedef bool result_type;
129
130 // TRAITS
133 bsl::is_trivially_default_constructible)
134
135 // CREATORS
136
137 /// Create a `CStringEqualTo` object.
138 CStringEqualTo() = default;
139
140 /// Create a `CStringEqualTo` object.
141 /// \note Note that as `CStringEqualTo` is
142 /// an empty (stateless) type, this operation has no observable effect.
143 CStringEqualTo(const CStringEqualTo& original) = default;
144
145 /// Destroy this object.
146 ~CStringEqualTo() = default;
147
148 // MANIPULATORS
149
150 /// Assign to this object the value of the specified `rhs` object, and
151 /// return a reference providing modifiable access to this object.
152 ///
153 /// \note Note that as `CStringEqualTo` is an empty (stateless) type, this
154 /// operation has no observable effect.
155 CStringEqualTo& operator=(const CStringEqualTo&) = default;
156
157 // ACCESSORS
158
159 /// Return `true` if the specified `lhs` string has the same (case
160 /// sensitive) value as the specified `rhs` string, and `false` otherwise.
161 ///
162 /// \pre The behavior is undefined unless both `lhs` and `rhs`
163 /// point to null-terminated strings.
164 bool operator()(const char *lhs, const char *rhs) const;
165};
166
167// ============================================================================
168// INLINE DEFINITIONS
169// ============================================================================
170
171 // ---------------------
172 // struct CStringEqualTo
173 // ---------------------
174
175// ACCESSORS
176inline
177bool CStringEqualTo::operator()(const char *lhs, const char *rhs) const
178{
179 BSLS_ASSERT(lhs);
180 BSLS_ASSERT(rhs);
181
182 return 0 == bsl::strcmp(lhs, rhs);
183}
184
185} // close package namespace
186
187
188#endif
189
190// ----------------------------------------------------------------------------
191// Copyright 2015 Bloomberg Finance L.P.
192//
193// Licensed under the Apache License, Version 2.0 (the "License");
194// you may not use this file except in compliance with the License.
195// You may obtain a copy of the License at
196//
197// http://www.apache.org/licenses/LICENSE-2.0
198//
199// Unless required by applicable law or agreed to in writing, software
200// distributed under the License is distributed on an "AS IS" BASIS,
201// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
202// See the License for the specific language governing permissions and
203// limitations under the License.
204// ----------------------------- END-OF-FILE ----------------------------------
205
206/** @} */
207/** @} */
208/** @} */
#define BSLMF_NESTED_TRAIT_DECLARATION(t_TYPE, t_TRAIT)
Definition bslmf_nestedtraitdeclaration.h:231
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1976
#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 bdlat_valuetypefunctions.h:939
Definition bdlb_cstringequalto.h:123
const char * first_argument_type
Definition bdlb_cstringequalto.h:126
bool result_type
Definition bdlb_cstringequalto.h:128
const char * second_argument_type
Definition bdlb_cstringequalto.h:127
Definition bslmf_istriviallycopyable.h:324