|
BDE 4.14.0 Production release
|
Provide methods for uniform printing of value-semantic types.
print methodbdlb::HasPrintMethodThis component provides a namespace for print utilities that support uniform ostream printing across all printable types, including template types and containers. The bdlb::PrintMethods namespace enables clients to output the value of any printable object according to the standard BDE print protocol. If the parameterized TYPE does not provide a print method, TYPE::operator<< is used. Availability of a print method is determined by testing for the bdlb::HasPrintMethod and bdlb::TypeTraitHasPrintMethod traits.
By default, bdlb::PrintMethods::print uses the << stream output operator to print a value. This formats the entire output on one line, suppressing all indentation. A class can override this behavior by declaring certain traits related to printing. This component detects these traits and invokes an appropriate print operation. The following lists the traits recognized by this component:
Since a class may declare multiple traits (see the component-level documentation of bslalg_nestedtraitdeclaration for information about declaring traits), the relative precedence of the traits is shown above. The next sub-sections describe these traits and their effects on printing.
If a class X declares the bdlb::TypeTraitHasPrintMethod trait, then it must provide a print method with the following signature:
To output an X object with this trait declared, the bdlb::PrintMethods::print method simply forwards to this method. This means that the print operation is completely defined by the class. Ideally, it should behave according to the standard BDE print protocol that is documented as follows:
If a class X declares the bslalg::HasStlIterators trait, then it must provide access to iterators using the standard STL protocol. The BDE implementation of STL declares this trait for all STL container types that have STL iterators. Other containers that provide STL iterators should declare this trait to get correct printing behavior.
When an X object with this trait is printed using bdlb::PrintMethods::print, the contents of the object is traversed via an iterator and the output is formatted according to the standard BDE print protocol, as documented above. Additionally, an opening [ character is prepended at the beginning of the output and a closing ] character is appended at the end of the output. Each iterated element is printed using its own print method, and with an indentation level one higher than that of the container.
If a class X declares the bslmf::IsPair trait, then the class must contain two public data members named first and second. The BDE implementation of STL declares this trait for the bsl::pair struct. Other classes that have public first and second data members may declare this trait to get printing behavior similar to that of bsl::pair.
When an X object with this trait is printed using bdlb::PrintMethods::print, its output is formatted based on the standard BDE print protocol, as documented above. Additionally, an opening [ character is prepended at the beginning of the output and a closing ] character is appended at the end of the output. The first and second elements are printed using their own print methods, and with an indentation level one higher than that of the pair object.
This section illustrates intended use of this component.
Suppose we must create a value-semantic class that holds an object of parameterized TYPE and, per BDE convention for VSTs, provides a print method that shows the value in some human-readable format.
First, we define the wrapper class:
Now, we implement the print method of MyWrapper using the bdlb::PrintMethods utility. Doing so gives us a method that produces results both when TYPE defines a print method and when it does not. In the latter case TYPE::operator<< is used.
Finally, we exercise our MyWrapper class using several representative types, starting with MyDate (not shown) a class that implements a print method.
Using an int type shows how bdlb::PrintMethods::print transparently handles types that do not provide print methods:
Lastly, since MyWrapper itself is a type that implements print – and sets the bdlb::TypeTraitHasPrintMethod trait – one instance of the MyWrapper type can be wrapped by another.
See the bslmf_nestedtraitdeclaration component for more information about declaring traits for user-defined classes.