BDE 4.14.0 Production release
|
Macros | |
#define | bdes_ObjectBuffer bsls::ObjectBuffer |
This alias is defined for backward compatibility. | |
#define | bsls_ObjectBuffer bsls::ObjectBuffer |
This alias is defined for backward compatibility. | |
Provide raw buffer with size and alignment of user-specified type.
This component provides a templated buffer type, bsls::ObjectBuffer
, that is compile-time sized and aligned to hold a specified object type. Defining a bsls::ObjectBuffer<TYPE>
object does not cause the constructor for TYPE
to be called. Similarly, destroying the object buffer does not call the destructor for TYPE
. Instead, the user instantiates bsls::ObjectBuffer
with a specific type, then constructs an object of that type within that buffer. When the object is no longer needed, the user must explicitly call its destructor. A bsls::ObjectBuffer
can reside on the stack or within another object, including within a union
.
Typically, a bsls::ObjectBuffer
is used in situations where efficient (e.g., stack-based) storage is required but where straightforward initialization or destruction of an object is not possible. For example, bsls::ObjectBuffer
can be used to construct an array where the number of used elements varies at run-time or where the element type does not have a default constructor. It can also be used to create a union
containing non-POD element types.
The examples below use a value-semantic string class, my_String
, that can be constructed from a null-terminated string and contains a member, c_str
, that returns a null-terminated string. my_String
does not have a default constructor and thus cannot be used in C-style arrays or unions.
Here we use bsls::ObjectBuffer
to create a variable-length array of my_String
objects. For efficiency, the array is created on the stack as a fixed-sized array of bsls::ObjectBuffer<my_String>
objects and the length is kept in a separate variable. Only len
calls are made to the my_String
constructor, with the unused array elements left as raw memory. An array directly containing my_String
objects would not have been possible because my_String
does not have a default constructor.
WARNING: the manipulateStrings
function below is not exception-safe. If an exception is thrown anywhere within the function (e.g., from a constructor call), the destructor will not be called on the constructed string objects. This logic would typically be augmented with guard objects that call destructors in case of an exception.
Here we use bsls::ObjectBuffer
to compose a variable-type object capable of holding a string or an integer:
#define bdes_ObjectBuffer bsls::ObjectBuffer |
#define bsls_ObjectBuffer bsls::ObjectBuffer |