Quick Links:

bal | bbl | bdl | bsl

Namespaces

Component bdlb_printadapter
[Package bdlb]

Provide object for streaming objects using bdlb::PrintMethods. More...

Namespaces

namespace  bdlb

Detailed Description

Outline
Purpose:
Provide object for streaming objects using bdlb::PrintMethods.
Classes:
bdlb::PrintAdapter class for streaming objects using bdlb::PrintMethods.
bdlb::PrintAdapterUtil utility for constructing bdlb::PrintAdapter.
Description:
This component provides a template class that will enable any object that can be streamed using bdlb::PrintMethods to be streamed via <<.
Through using this class, one can stream an object using operator<< and at the same time specify level and spacesPerLevel arguments to appropriately indent and format the output.
Usage:
This section illustrates intended use of this component.
Example 1: Use in C++03:
Suppose we have a few bdlb::IdentSpan objects that we want to print, only we want to stream them with operator<< and we want to specify non-default values of level and spacesPerLevel to their streaming.
  const bdlb::IndexSpan a(3, 7), b(241, 22), c(23, 17);
First, we create a typedef of PrintAdapter<IdentSpan> to use: Then, we create a line of dashes:
  const char * const line = "------------------------------------"
                            "------------------------------------\n";
Now, we use the typedef to construct temporary PrintAdapter objects to stream our IndexSpan objects:
  cout << "    a: " << line << PAIS(&a, 2, 2)
       << "    b: " << line << PAIS(&b, 3, 2)
       << "    c: " << line << PAIS(&c, 4, 2);
Finally, we see the output:
  a: ------------------------------------------------------------------------
  [
    position = 3
    length = 7
  ]
  b: ------------------------------------------------------------------------
    [
      position = 241
      length = 22
    ]
  c: ------------------------------------------------------------------------
      [
        position = 23
        length = 17
      ]
Example 2: Use of PrintAdapterUtil::makeAdapter:
Suppose we have a few bdlb::IdentSpan objects that we want to print, only we want to stream them with operator<< and we want to specify non-default values of level and spacesPerLevel to their streaming.
  const bdlb::IndexSpan a(3, 7), b(241, 22), c(23, 17);
First, we make a typedef to the namespace struct:
  typedef bdlb::PrintAdapterUtil Util;
Then, we create a line of dashes:
  const char * const line = "------------------------------------"
                            "------------------------------------\n";
Now, we call the static function PrintAdapterUtil::makeAdapter on the IndexSpan objects to stream which will return streamable PrintAdapter objects:
  cout << "    a: " << line << Util::makeAdapter(a, 2, 2)
       << "    b: " << line << Util::makeAdapter(b, 3, 2)
       << "    c: " << line << Util::makeAdapter(c, 4, 2);
Finally, we see the output:
  a: ------------------------------------------------------------------------
  [
    position = 3
    length = 7
  ]
  b: ------------------------------------------------------------------------
    [
      position = 241
      length = 22
    ]
  c: ------------------------------------------------------------------------
      [
        position = 23
        length = 17
      ]
Example 3: Use in C++17 With CTAD:
Suppose you have a few bdlb::IdentSpan objects that you want to print, only you want to stream them with operator<< and you want to specify non-default values of level and spacesPerLevel to their streaming.
  const bdlb::IndexSpan a(3, 7), b(241, 22), c(23, 17);
First, we create a line of dashes:
  const char * const line = "------------------------------------"
                            "------------------------------------\n";
Now, you call the constructor PrintAdapter on the IndexSpan objects to stream and CTAD will create the right template specification:
  cout << "    a: " << line << bdlb::PrintAdapter(&a, 2, 2)
       << "    b: " << line << bdlb::PrintAdapter(&b, 3, 2)
       << "    c: " << line << bdlb::PrintAdapter(&c, 4, 2);
Finally, we see the output:
  a: ------------------------------------------------------------------------
  [
    position = 3
    length = 7
  ]
  b: ------------------------------------------------------------------------
    [
      position = 241
      length = 22
    ]
  c: ------------------------------------------------------------------------
      [
        position = 23
        length = 17
      ]