BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bdlb_variantprinter

Detailed Description

Provide a suite of helper classes for printing bsl::variant.

Outline

Purpose

Provide a suite of helper classes for printing bsl::variant.

Classes

Description

This component provides utility classes bdlb::VariantPrinter and bdlb::VariantPrinterUtil for printing bsl::variant.

The class bdlb::VariantPrinter can also be used with bsl::format (it defines the bslfmt::EnableStreamedFormatter trait). For information about the supported format options see the `bslfmt` package documentation .

Usage

This section illustrates intended use of this component.

Example 1: Printing bsl::variant to a Stream

In this example, we demonstrate how to use bdlb::VariantPrinterUtil to print bsl::variant to a stream.

First, we create a streamable type that throws int on copies. This will enable us to put a variant into a valueless state by copying one into it:

struct ThrowOnCopy {
// DATA
int d_ii;
// CREATORS
ThrowOnCopy(int ii)
: d_ii(ii)
{}
ThrowOnCopy(const ThrowOnCopy& original)
: d_ii(original.d_ii)
{
}
// MANIPULATORS
ThrowOnCopy& operator=(const ThrowOnCopy& rhs)
{
d_ii = rhs.d_ii;
return *this;
}
};
bsl::ostream& operator<<(bsl::ostream& stream,
const ThrowOnCopy& value)
{
return stream << value.d_ii;
}
bsl::ostream & operator<<(bsl::ostream &stream, const bdlat_AttributeInfo &attributeInfo)
#define BSLS_THROW(X)
Definition bsls_exceptionutil.h:374
ALLOCATOR const STRING_VIEW_LIKE_TYPE & rhs
Definition bslstl_string.h:3918

Next, we create a shorthand for VariantPrinterUtil and our variant type:

Definition bslstl_variant.h:3806
Definition bdlb_variantprinter.h:264

Then, we declare a variable of variant type, which can hold, among other types, a ThrowOnCopy:

VariantIST mV(107);
const VariantIST& V = mV;
ThrowOnCopy toc(4);

Next, we start doing some output:

cout << toc << endl; // prints "4\n"
cout << Util::makePrinter(V) << endl; // prints "107\n"
mV = bsl::string("woof");
cout << Util::makePrinter(V) << endl; // prints "woof\n"
mV = 27;
cout << Util::makePrinter(V) << endl; // prints "27\n"
basic_string< char > string
Definition bslstl_string.h:844

Then, we see that the type VariantPrinter<...> returned by makePrinter has a standard BDE-style print accessor that will control formatting and indenting:

mV = bsl::string("bow");
Util::makePrinter(V).print(cout, 3, -2); // prints " bow"
mV = bsl::string("wow");
Util::makePrinter(V).print(cout, 1, 1); // prints " wow\n"

Now, we assign a ThrowOnCopy to the variant, which will throw when copied, leaving mV in a valueless state, and we observe what happens when we then print it.

assert(!V.valueless_by_exception());
{
mV = toc;
assert(false);
}
{
}
assert(V.valueless_by_exception());
cout << Util::makePrinter(V) << endl; // prints "(valueless)\n"
#define BSLS_CATCH(X)
Definition bsls_exceptionutil.h:372
#define BSLS_TRY
Definition bsls_exceptionutil.h:370

Finally, we see the output created by all this:

4
107
woof
27
bow wow
(valueless)