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