BDE 4.14.0 Production release
|
Provide a utility to build a Datum
object holding a map.
Datum
map valueThis component defines a mechanism, bdld::DatumMapOwningKeysBuilder
, used to populate a Datum
map value in an exception-safe manner. In addition to providing exception safety, a DatumMapOwningKeysBuilder
is particularly useful when the size of the map to be constructed is not known in advance. The user can append elements to the datum map as needed, and when there are no more elements to append the user calls commit
or sortAndCommit
and ownership of the populated Datum
object is transferred to the caller. After calling commit
or sortAndCommit
, no additional elements can be appended to the Datum
map value. Note that sortAndCommit
method will sort the populated map (by keys) and tag the resulting Datum
map value as sorted. Also note that the user can insert elements in a (ascending) sorted order and tag the map as sorted. The behaviour is undefined if unsorted map is tagged sorted.
The only difference between this component and bdld_datummapbuilder is that this component makes a copy of the map entries keys and the resulting Datum
object owns memory for the map entries keys.
This section illustrates intended use of this component.
Suppose we need a map for some personal data. The values in that map can be different types and keys must be protected from destruction as this map object can be passed out of scope. The following code illustrates how to use bdld::DatumMapOwningKeysBuilder
to create such map easily.
First, we need data to fill our map:
Next, we create an object of DatumMapOwningKeysBuilder
class with initial capacity sufficient for storing all our data:
Then, we load our builder with these data:
Next, we adopt the map, held by our builder, by newly created Datum
object:
Now, we can check that all data have been correctly added to the map at the required order:
Finally, we destroy the Datum
object to release all allocated memory correctly: