Quick Links:

bal | bbl | bdl | bsl

Namespaces

Component balber_berdecoder
[Package balber]

Provide a BER decoder class. More...

Namespaces

namespace  balber

Detailed Description

Outline
Purpose:
Provide a BER decoder class.
Classes:
balber::BerDecoder BER decoder
See also:
Component balber_berencoder, bdem_bdemdecoder, Component balxml_decoder
Description:
This component defines a single class, balber::BerDecoder, that contains a parameterized decode function. The decode function decodes data read from a specified stream and loads the corresponding object to an object of the parameterized type. The decode method is overloaded for two types of input streams:
  • bsl::streambuf
  • bsl::istream
This class decodes objects based on the X.690 BER specification and is restricted to types supported by the bdlat framework.
Usage:
This section illustrates intended use of this component.
Example 1: Decoding an Employee Record:
Suppose that an "employee record" consists of a sequence of attributes -- name, age, and salary -- that are of types bsl::string, int, and float, respectively. Furthermore, we have a need to BER encode employee records as a sequence of values (for out-of-process consumption).
Assume that we have defined a usage::EmployeeRecord class to represent employee record values, and assume that we have provided the bdlat specializations that allow the balber codec components to represent class values as a sequence of BER primitive values. See bdlat_sequencefunctions|Usage for details of creating specializations for a sequence type.
First, we create an employee record object having typical values:
  usage::EmployeeRecord bob("Bob", 56, 1234.00);
  assert("Bob"   == bob.name());
  assert(  56    == bob.age());
  assert(1234.00 == bob.salary());
Next, we create a balber::Encoder object and use it to encode our bob object. Here, to facilitate the examination of our results, the BER encoding data is delivered to a bslsb::MemOutStreamBuf object:
  bdlsb::MemOutStreamBuf osb;
  balber::BerEncoder     encoder;
  int                    rc = encoder.encode(&osb, bob);
  assert( 0 == rc);
  assert(18 == osb.length());
Now, we create a bdlsb::FixedMemInStreamBuf object to manage our access to the data portion of the bdlsb::MemOutStreamBuf (where our BER encoding resides), decode the values found there, and use them to set the value of an usage::EmployeeRecord object.
  balber::BerDecoderOptions  options;
  balber::BerDecoder         decoder(&options);
  bdlsb::FixedMemInStreamBuf isb(osb.data(), osb.length());
  usage::EmployeeRecord      obj;

  rc = decoder.decode(&isb, &obj);
  assert(0 == rc);
Finally, we confirm that the object defined by the BER encoding has the same value as the original object.
  assert(bob.name()   == obj.name());
  assert(bob.age()    == obj.age());
  assert(bob.salary() == obj.salary());