BDE 4.14.0 Production release
|
Provide an value-semantic type to represent type_info
objects.
std::type_info
objectCanonical header: bsl_typeindex.h
This component defines an in-core value-semantic class, bsl::type_index
, capable of representing a handle to a std::type_info
object that supports all the operations needed to serve as a key type in an associative or unordered container.
This implementation of type_index
satisfies the contracts for the native std::type_index
specified in the ISO standard, including a specialization for the native standard library std::hash
. It further provides an overload for hashAppend
to support the BDE hashing framework, and therefor idiomatic usage with bsl::hash
. In general, the recommended best practice is that users of bsl
containers should use the bsl::type_index
class, while users of std
containers should use the std::type_index
class.
This section illustrates intended use of this component.
Assume you are implementing a graphics library, and need to represent a variety of shapes. You might have a simple hierarchy, such as:
In order to manage the creation of objects in our hierarchy, we might deploy the Abstract Factory pattern: https://en.wikipedia.org/wiki/Abstract_factory_pattern using objects of type bsl::function<shared_ptr<Shape> >
as factories.
First, we define our basic class hierarchy.
Then, we create a utility class containing a registry of factory functions indexed by their corresponding std::type_info
, using bsl::type_index
to provide the value-semantic wrapper needed for the key used in the container. This registry will enable us to abstract away different constructors of the concrete object types.
Now, we can implement the register and make functions, using the standard typeid
operator to create the key values as needed.
Next, we provide several concrete implementations of our Shape
class, to demonstrate use of this hierarchy.
Then, we provide some simple factory functions to create some shapes at the specified coordinates.
Finally, we can exercise the whole system in a simple test driver. Note that as we do not register a factory function for the Polygon
class, the attempt to create a Polygon
will fail.