BDE 4.14.0 Production release
|
Macros | |
#define | bslalg_ConstructorProxy bslalg::ConstructorProxy |
This alias is defined for backward compatibility. | |
Provide a proxy for constructing and destroying objects.
This component provides a proxy class template, bslalg::ConstructorProxy
, for creating a proxied object of parameter type OBJECT_TYPE
using a uniform constructor syntax, regardless of whether OBJECT_TYPE
is allocator-aware (AA) – i.e., uses an allocator to supply memory. This proxy is useful in generic programming situations where an object of a given type must be constructed, but it is not known in advance which allocator model the object supports, if any. In these situations, client code unconditionally passes an allocator as the last argument to the ConstructorProxy
constructor; the constructor forwards the allocator to the proxied object if OBJECT_TYPE
is AA and discards it otherwise.
The proxied object is owned by the ConstructorProxy
object. Modifiable and non-modifiable access to the proxied object may be obtained using the overloaded object
methods. When the proxy is destroyed, it automatically destroys its proxied object.
See the bslma
package-level documentation for more information about using allocators.
In this example, we create a key-value class template consiting of a string key paired with a value of template-parameter type. Since the value type might be allocator aware (AA), we want to ensure that our key-value class template can pass an allocator to its value-type constructor.
First, we define a simple AA string class that will be our value type for testing:
Next, we define the constructors, destructor, and equality-comparison operators. For brevity, we've omited the implementation of the assignment operator, which is not used in this example:
Now we are ready to define our key-value template. The data portion of the template needs a member for the key and one for the value. Rather than defining the value member as simply a member variable of TYPE
, we use bslalg::ConstructorProxy
to ensure that we will be able to construct it in a uniform way even though we do not know whether or not it is allocator-aware:
Next, we declare the creators and manipulators typical of an AA attribute class:
Next, we declare the accessessors and, for convenience in this example, define them inline. Note that the value
accessor extracts the proxied object from the d_valueProxy
member:
Next, we define the value constructor, which passes its allocator argument to both data members' constructors. Note that the d_valueProxy
, constructor always expects an allocator argument, even if TYPE
is not AA:
Next, we define the copy constructor and assignment operator. Since bslalg::ConstructorProxy
is not copyable, we must manually extract the proxied object in the assignment operator. This extraction is not needed in the copy constructor because the single-value proxy constructor automatically "unwraps" its argument when presented with an instantiation of bslalg::ConstructorProxy
:
Last, we define the destructor, which does nothing explicit (and could therefore have been defaulted), because both String
and ConstructorProxy
clean up after themselves:
Now we can illustrate the use of our key-value pair by defining a string-int pair and constructing it with a test allocator. Note that the allocator was passed to the (String
) key, as we would expect:
Next, we define a string-string pair and show that the allocator was passed to both the key and value parts of the pair:
Finally, we declare a bslalg::ConstructorProxy
of KeyValue
and show how we can pass more than one argument (up to 14) – in addition to the allocator – to the proxied type's constructor:
#define bslalg_ConstructorProxy bslalg::ConstructorProxy |