BDE 4.14.0 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
119/// `bsl::unordered_set`. Note that this class is an empty POD type.
121
122 // STANDARD TYPEDEFS
123 typedef const char *first_argument_type;
124 typedef const char *second_argument_type;
125 typedef bool result_type;
126
127 // TRAITS
130 bsl::is_trivially_default_constructible)
131
132 // CREATORS
133
134 /// Create a `CStringEqualTo` object.
135 CStringEqualTo() = default;
136
137 /// Create a `CStringEqualTo` object. Note that as `CStringEqualTo` is
138 /// an empty (stateless) type, this operation has no observable effect.
139 CStringEqualTo(const CStringEqualTo& original) = default;
140
141 /// Destroy this object.
142 ~CStringEqualTo() = default;
143
144 // MANIPULATORS
145
146 /// Assign to this object the value of the specified `rhs` object, and
147 /// return a reference providing modifiable access to this object. Note
148 /// that as `CStringEqualTo` is an empty (stateless) type, this
149 /// operation has no observable effect.
150 CStringEqualTo& operator=(const CStringEqualTo&) = default;
151
152 // ACCESSORS
153
154 /// Return `true` if the specified `lhs` string has the same (case
155 /// sensitive) value as the specified `rhs` string, and `false`
156 /// otherwise. The behavior is undefined unless both `lhs` and `rhs`
157 /// point to null-terminated strings.
158 bool operator()(const char *lhs, const char *rhs) const;
159};
160
161// ============================================================================
162// INLINE DEFINITIONS
163// ============================================================================
164
165 // ---------------------
166 // struct CStringEqualTo
167 // ---------------------
168
169// ACCESSORS
170inline
171bool CStringEqualTo::operator()(const char *lhs, const char *rhs) const
172{
173 BSLS_ASSERT(lhs);
174 BSLS_ASSERT(rhs);
175
176 return 0 == bsl::strcmp(lhs, rhs);
177}
178
179} // close package namespace
180
181
182#endif
183
184// ----------------------------------------------------------------------------
185// Copyright 2015 Bloomberg Finance L.P.
186//
187// Licensed under the Apache License, Version 2.0 (the "License");
188// you may not use this file except in compliance with the License.
189// You may obtain a copy of the License at
190//
191// http://www.apache.org/licenses/LICENSE-2.0
192//
193// Unless required by applicable law or agreed to in writing, software
194// distributed under the License is distributed on an "AS IS" BASIS,
195// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
196// See the License for the specific language governing permissions and
197// limitations under the License.
198// ----------------------------- END-OF-FILE ----------------------------------
199
200/** @} */
201/** @} */
202/** @} */
#define BSLMF_NESTED_TRAIT_DECLARATION(t_TYPE, t_TRAIT)
Definition bslmf_nestedtraitdeclaration.h:231
#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_printmethods.h:283
Definition bdlb_cstringequalto.h:120
const char * first_argument_type
Definition bdlb_cstringequalto.h:123
bool result_type
Definition bdlb_cstringequalto.h:125
const char * second_argument_type
Definition bdlb_cstringequalto.h:124
Definition bslmf_istriviallycopyable.h:329