BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bslstl_stringrefdata.h
Go to the documentation of this file.
1/// @file bslstl_stringrefdata.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslstl_stringrefdata.h -*-C++-*-
8#ifndef INCLUDED_BSLSTL_STRINGREFDATA
9#define INCLUDED_BSLSTL_STRINGREFDATA
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslstl_stringrefdata bslstl_stringrefdata
15/// @brief Provide a base class for `bslstl::StringRef`.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslstl
19/// @{
20/// @addtogroup bslstl_stringrefdata
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslstl_stringrefdata-purpose"> Purpose</a>
25/// * <a href="#bslstl_stringrefdata-classes"> Classes </a>
26/// * <a href="#bslstl_stringrefdata-canonical-header"> Canonical Header </a>
27/// * <a href="#bslstl_stringrefdata-description"> Description </a>
28/// * <a href="#bslstl_stringrefdata-usage"> Usage </a>
29/// * <a href="#bslstl_stringrefdata-example-1-breaking-cyclic-dependency-between-string-and-stringref"> Example 1: Breaking Cyclic Dependency Between String and StringRef </a>
30///
31/// # Purpose {#bslstl_stringrefdata-purpose}
32/// Provide a base class for `bslstl::StringRef`.
33///
34/// # Classes {#bslstl_stringrefdata-classes}
35///
36/// - bslstl::StringRefData: intermediate class providing StringRef compatibility
37///
38/// # Canonical Header {#bslstl_stringrefdata-canonical-header}
39/// bsl_string.h
40///
41/// # Description {#bslstl_stringrefdata-description}
42/// Initially, this component provided a complex-constrained,
43/// in-core (value-semantic) attribute class, `bslstl::StringRefData`, that
44/// represented a reference to character string data. Note that
45/// `bslstl::StringRefData` was intended for use as a base class for
46/// `bslstl::StringRef` and as parameter of `bsl::string` constructor, enabling
47/// a conversion from `bslstl::StringRef` to `bsl::string` without having a
48/// cyclic dependency among these three classes.
49///
50/// But nowadays it only provides compatibility between the two constructors of
51/// `bsl::string` (implicit constructor, accepting `StringRefData` object by
52/// value and explicit constructor, accepting `bsl::string_view` object by
53/// value) to allow existing users of `bslstl::StringRef` not to change their
54/// code.
55///
56/// The dependencies between these components are shown on the following
57/// diagram:
58/// @code
59/// +-----------------------+
60/// | |
61/// /--------o bslstl::StringRef |
62/// / | |
63/// / +-----------------------+
64/// / |
65/// / |
66/// +------------------+ +----------V------------+
67/// | | | |
68/// | bsl::string o--------| bslstl::StringRefData |
69/// | | | |
70/// +--------------o---+ +-----------------------+
71/// \ |
72/// \ |
73/// \ +----------V------------+
74/// \ | |
75/// \-------| bsl::string_view |
76/// | |
77/// +-----------------------+
78/// @endcode
79///
80/// ## Usage {#bslstl_stringrefdata-usage}
81///
82///
83/// This section illustrates intended use of this component.
84///
85/// ### Example 1: Breaking Cyclic Dependency Between String and StringRef {#bslstl_stringrefdata-example-1-breaking-cyclic-dependency-between-string-and-stringref}
86///
87///
88/// In this example we demonstrate how `bslstl::StringRefData` allows us to
89/// break the cyclic dependency between hypothetical `String` and `StringRef`
90/// classes.
91///
92/// Objects of our `String` and `StringRef` classes need to be convertible to
93/// each other. However, only one of these classes can depend on the definition
94/// of the other one, otherwise they will be cyclically dependent.
95///
96/// First, we define a hypothetical `String` class, whose implementation is
97/// intentionally simple and contains only the essential constructors and
98/// accessor methods; the important thing to notice is that `String` does not
99/// depend on `StringRef`, which has not been defined yet:
100/// @code
101/// namespace Usage {
102///
103/// class String {
104/// private:
105/// const char *d_begin_p;
106/// const char *d_end_p;
107///
108/// public:
109/// typedef const char *const_iterator;
110///
111/// String(bslstl::StringRefData<char> const& stringRef)
112/// : d_begin_p(stringRef.data())
113/// , d_end_p(stringRef.data() + stringRef.length())
114/// {}
115///
116/// const char *data() const
117/// {
118/// return d_begin_p;
119/// }
120///
121/// std::size_t length() const
122/// {
123/// return static_cast<std::size_t>(d_end_p - d_begin_p);
124/// }
125/// };
126/// @endcode
127/// Notice that the constructor of `String` takes a `bslstl::StringRefData`
128/// argument and then uses its members `data` and `length` to initialize the
129/// string object.
130///
131/// Then, we define a hypothetical `StringRef` class, whose instances can be
132/// initialized either with a `String` object (to enable the conversion from
133/// `String` to `StringRef`) or with two `const char *` pointers:
134/// @code
135/// class StringRef : public bslstl::StringRefData<char>
136/// {
137/// public:
138/// StringRef(const char *begin, const char *end)
139/// : bslstl::StringRefData<char>(begin, end)
140/// {}
141///
142/// StringRef(const String& str)
143/// : bslstl::StringRefData<char>(str.data(), str.data() + str.length())
144/// {}
145/// };
146///
147/// } // close namespace Usage
148/// @endcode
149/// Note that `StringRef` also derives from `bslstl::StringRefData` so that an
150/// object of `StringRef` can be passed to the constructor of `String` as a
151/// reference to `bslstl::StringRefData`, which enables the conversion from
152/// `StringRef` to `String`.
153///
154/// Finally, we verify that the conversions between `String` and `StringRef`
155/// work:
156/// @code
157/// using Usage::String;
158/// using Usage::StringRef;
159///
160/// const char str[] = "test string";
161/// StringRef strRef(str, str + sizeof(str));
162///
163/// String strObj = strRef; // convert `StringRef` to `String`
164/// StringRef strRf2 = strObj; // convert `String` to `StringRef`
165///
166/// assert(strObj.data() == strRef.data());
167/// assert(strObj.length() == strRef.length());
168/// assert(strObj.data() == strRf2.data());
169/// assert(strObj.length() == strRf2.length());
170/// @endcode
171/// @}
172/** @} */
173/** @} */
174
175/** @addtogroup bsl
176 * @{
177 */
178/** @addtogroup bslstl
179 * @{
180 */
181/** @addtogroup bslstl_stringrefdata
182 * @{
183 */
184
185#include <bslscm_version.h>
186
187#include <bslstl_stringview.h>
188
191
192#include <bsls_assert.h>
193
194#include <iosfwd>
195
196#ifndef BDE_DONT_ALLOW_TRANSITIVE_INCLUDES
197#include <bsls_nativestd.h>
198#endif // BDE_DONT_ALLOW_TRANSITIVE_INCLUDES
199
200
201
202namespace bslstl {
203
204 // ===================
205 // class StringRefData
206 // ===================
207
208/// This class is an intermediate link between `bslstl::StringRef` used in
209/// BDE and `bsl::string_view` and is kept only for compatibility with
210/// legacy code.
211///
212/// See @ref bslstl_stringrefdata
213template <class CHAR_TYPE>
214class StringRefData : public bsl::basic_string_view<CHAR_TYPE> {
215
216 private:
217 // PRIVATE TYPES
219
220 public:
221 // CLASS METHODS
222
223 /// Return the number of `CHAR_TYPE` characters in the specified
224 /// null-terminated `data` string, up to but not including the terminal
225 /// null value.
226 static std::size_t cStringLength(const CHAR_TYPE *data);
227
228 // TRAITS
230
231 // CREATORS
232
233 /// Create a `StringRefData` object having default attribute values:
234 /// @code
235 /// begin() == 0
236 /// end() == 0
237 /// @endcode
239
240 /// Create a `StringRefData` object the specified `begin` and `end` attribute values.
241 ///
242 /// \pre The behavior is undefined unless `begin <= end`
243 /// and `!begin == !end`.
244 StringRefData(const CHAR_TYPE *begin, const CHAR_TYPE *end);
245
246 /// Create a `StringRefData` object from the specified `view`.
248
249 StringRefData(const StringRefData&) = default;
250 ~StringRefData() = default;
251
252 // MANIPULATORS
254
255};
256
257// ============================================================================
258// TEMPLATE FUNCTION DEFINITIONS
259// ============================================================================
260
261 // -------------------
262 // class StringRefData
263 // -------------------
264
265// CLASS METHODS
266template <class CHAR_TYPE>
267inline
269 const CHAR_TYPE *data)
270{
271 BSLS_ASSERT_SAFE(data);
272
273 return std::char_traits<CHAR_TYPE>::length(data);
274}
275
276// CREATORS
277template <class CHAR_TYPE>
278inline
283
284template <class CHAR_TYPE>
285inline
287 ::StringRefData(const CHAR_TYPE *begin,
288 const CHAR_TYPE *end)
289: Base(begin, end - begin)
290{
293}
294
295template <class CHAR_TYPE>
296inline
299: Base(view)
300{
301}
302
303} // close package namespace
304
305#ifndef BDE_OPENSOURCE_PUBLICATION // BACKWARD_COMPATIBILITY
306// ============================================================================
307// BACKWARD COMPATIBILITY
308// ============================================================================
309
310#ifdef bslstl_StringRefData
311#undef bslstl_StringRefData
312#endif
313/// This alias is defined for backward compatibility.
314#define bslstl_StringRefData bslstl::StringRefData
315#endif // BDE_OPENSOURCE_PUBLICATION -- BACKWARD_COMPATIBILITY
316
317
318
319#endif
320
321// ----------------------------------------------------------------------------
322// Copyright 2019 Bloomberg Finance L.P.
323//
324// Licensed under the Apache License, Version 2.0 (the "License");
325// you may not use this file except in compliance with the License.
326// You may obtain a copy of the License at
327//
328// http://www.apache.org/licenses/LICENSE-2.0
329//
330// Unless required by applicable law or agreed to in writing, software
331// distributed under the License is distributed on an "AS IS" BASIS,
332// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
333// See the License for the specific language governing permissions and
334// limitations under the License.
335// ----------------------------- END-OF-FILE ----------------------------------
336
337/** @} */
338/** @} */
339/** @} */
Definition bslstl_stringview.h:471
BSLS_KEYWORD_CONSTEXPR const_iterator end() const BSLS_KEYWORD_NOEXCEPT
Return the past-the-end iterator for this view.
Definition bslstl_stringview.h:1848
BSLS_KEYWORD_CONSTEXPR const_pointer data() const BSLS_KEYWORD_NOEXCEPT
Definition bslstl_stringview.h:1988
BSLS_KEYWORD_CONSTEXPR const_iterator begin() const BSLS_KEYWORD_NOEXCEPT
Definition bslstl_stringview.h:1830
Definition bslstl_stringrefdata.h:214
StringRefData & operator=(const StringRefData &)=default
StringRefData(const StringRefData &)=default
BSLMF_NESTED_TRAIT_DECLARATION(StringRefData, bsl::is_trivially_copyable)
static std::size_t cStringLength(const CHAR_TYPE *data)
Definition bslstl_stringrefdata.h:268
StringRefData()
Definition bslstl_stringrefdata.h:279
#define BSLS_ASSERT_SAFE(X)
Definition bsls_assert.h:1917
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bslstl_algorithm.h:84
Definition bslmf_istriviallycopyable.h:324