|
BDE 4.14.0 Production release
|
Macros | |
| #define | bslstl_StringRefData bslstl::StringRefData |
| This alias is defined for backward compatibility. | |
Provide a base class for bslstl::StringRef.
Canonical header: bsl_string.h
Initially, this component provided a complex-constrained, in-core (value-semantic) attribute class, bslstl::StringRefData, that represented a reference to character string data. Note that bslstl::StringRefData was intended for use as a base class for bslstl::StringRef and as parameter of bsl::string constructor, enabling a conversion from bslstl::StringRef to bsl::string without having a cyclic dependency among these three classes.
But nowadays it only provides compatibility between the two constructors of bsl::string (implicit constructor, accepting StringRefData object by value and explicit constructor, accepting bsl::string_view object by value) to allow existing users of bslstl::StringRef not to change their code.
The dependencies between these components are shown on the following diagram:
This section illustrates intended use of this component.
In this example we demonstrate how bslstl::StringRefData allows us to break the cyclic dependency between hypothetical String and StringRef classes.
Objects of our String and StringRef classes need to be convertible to each other. However, only one of these classes can depend on the definition of the other one, otherwise they will be cyclically dependent.
First, we define a hypothetical String class, whose implementation is intentionally simple and contains only the essential constructors and accessor methods; the important thing to notice is that String does not depend on StringRef, which has not been defined yet:
Notice that the constructor of String takes a bslstl::StringRefData argument and then uses its members data and length to initialize the string object.
Then, we define a hypothetical StringRef class, whose instances can be initialized either with a String object (to enable the conversion from String to StringRef) or with two const char * pointers:
Note that StringRef also derives from bslstl::StringRefData so that an object of StringRef can be passed to the constructor of String as a reference to bslstl::StringRefData, which enables the conversion from StringRef to String.
Finally, we verify that the conversions between String and StringRef work:
| #define bslstl_StringRefData bslstl::StringRefData |