BDE 4.14.0 Production release
|
Provide extra functions that operate on bdld::Datum
objects.
bdld::Datum
This component provides a struct, bdld::DatumUtil
that serves as a namespace for utility functions that operate on bdld::Datum
objects. The functions provided are typedPrint
and safeTypedPrint
. They are a variation on the standard BDE-style print
function but print the Datum
values in a manner that disambiguates the type when the value itself would not do so.
This section illustrates intended use of this component.
Suppose we are testing a system with operations that result in bdld::Datum
values. We verify that those results are what we have expected, including that their type matches. After a getting an unexpected value, we use normal printing and get the following test failure: "Expected 1, got 1". Obviously, the type difference is not visible. Instead, we can use bdld::DatumUtil::typedPrint
to display the type as well as the value.
First, let us define two bdld::Datum
objects that have the same value, but use different types to represent them:
Next, we demonstrate that printing these results in the same printout:
Then, we create a shorthand for bdld::DatumUtil::typedPrint
:
The 0 level
and -1 spacesPerLevel
results in single-line printout without a trailing newline, just like the stream output operator works.
Finally, we verify that now we get a different printout for the two values:
Suppose that we are testing a system that creates a complex data structure that it stores is bdld::Datum
objects. Suppose that such a system doesn't use the fail-safe Datum
builders for optimization purposes (for example it stores all map entries in one big allocation), and so it may be able to create a self-referential data structure.
It is not easy to legitimately create self-referential data structures so we won't even attempt it in a short example code.
First, we use a bdld::DatumMaker
with a local allocator so we can ignore any cleanup and allocation:
Next, we create two array datums with a Nil element each:
Then, we circumvent the type system to initialize their single elements to "contain" each other:
Finally, we use the safe printing on this trapdoor of an endless loop to nevertheless safely print them:
Were we to print the results standard out, say
we would see something akin to:
The hexadecimal numbers above identify the arrays (and maps or int-maps) so we can clearly see that the cycle "points" back to the top-level array.