Quick Links:

bal | bbl | bdl | bsl

Classes

Component bdlat_symbolicconverter
[Package bdlat]

Provide a utility for convert types with matching member symbols. More...

Classes

struct  bdlat_SymbolicConverter
 symbolic converter utility More...
class  bdlat_SymbolicConverter_Imp
class  bdlat_SymbolicConverter_StoreValue< LVALUE_TYPE >
class  bdlat_SymbolicConverter_LoadValue< RVALUE_TYPE >
class  bdlat_SymbolicConverter_StoreInSequence< SEQUENCE_TYPE >
class  bdlat_SymbolicConverter_StoreInChoice< CHOICE_TYPE >
class  bdlat_SymbolicConverter_StoreInArrayElement< ARRAY_TYPE >
class  bdlat_SymbolicConverter_StoreInNullable< NULLABLE_TYPE >
struct  bdlat_SymbolicConverter_Imp_resolveDynamicRhsProxy< LHS_TYPE, LHS_CATEGORY >
struct  bdlat_SymbolicConverter_Imp_resolveDynamicLhsProxy< RHS_TYPE, RHS_CATEGORY >

Detailed Description

Outline
Purpose:
Provide a utility for convert types with matching member symbols.
Classes:
bdlat_SymbolicConverter symbolic converter utility
See also:
Description:
The bdlat_SymbolicConverter utility provided by this component defines a single parameterized function convert. The convert function takes two arguments: a destination and a source object. The destination and source objects may be of different types.
Each type can fall into one of the following categories:
  Category          Reference
  --------          ---------
  Sequence          bdlat_sequencefunctions
  Choice            bdlat_choicefunctions
  Array             bdlat_arrayfunctions
  Enumeration       bdlat_enumfunctions
  NullableValue     bdlat_nullablevaluefunctions
  CustomizedType    bdlat_customizedtypefunctions
  Simple            basic C++ fundamental types & other value-semantic types
The bdlat_SymbolicConverter utility converts from one type to another using the following criteria:
  Destination Category  Source Category   Comments
  --------------------  ---------------   --------
  Sequence              Sequence          The conversion will fail if each
                                          attribute in the set of attributes
                                          from the source does not have a
                                          corresponding attribute (with the
                                          same name) in the destination.  The
                                          conversion will also fail if any
                                          attributes from the source fail to
                                          convert to the corresponding
                                          attribute in the destination.  Any
                                          attribute in the destination that
                                          does not have a corresponding
                                          attribute in the source will be set
                                          to its default value.

  Choice                Choice            The conversion will fail if the
                                          destination does not have a
                                          selection with the same name as the
                                          current selection in the source.
                                          The conversion will also fail if
                                          the selection from the source fails
                                          to convert to the corresponding
                                          selection in the destination.  If
                                          nothing is selected in the source,
                                          then the destination will be reset.

  Array                 Array             The conversion will fail if the
                                          elements in the source fail to
                                          convert to the elements in the
                                          destination.  Upon completion, the
                                          destination array will contain the
                                          same number of elements as the
                                          source array.

  Enumeration           Enumeration       The conversion will fail if the
                                          destination does not have a string
                                          value that is identical to the
                                          string value of the source.

  Enumeration           char/short/int    The conversion will fail if the
                                          destination does not have an
                                          enumerator with the numeric value
                                          of the source.

  char/short/int        Enumeration       The conversion will fail if the
                                          numeric value of the enumeration is
                                          outside the bounds of the
                                          destination type.

  Enumeration           bsl::string       The conversion will fail if the
                                          destination does not have an
                                          enumerator with the symbolic string
                                          name of the source.

  bsl::string           Enumeration       This conversion always succeeds.

  NullableValue         NullableValue     The conversion will fail if the
                                          source has a value that fails to
                                          convert to the destination value.
                                          If the source is null, then the
                                          destination is nulled.

  NullableValue         AnyType           The conversion will fail if the
                                          source fails to convert to the
                                          destination value.

  AnyType               NullableValue     The conversion will fail if the
                                          source is not null and the value in
                                          the source fails to convert to the
                                          destination.  If the source is
                                          null, then the destination will
                                          be set to its default value.

  CustomizedType        CustomizedType    The conversion will fail if the
                                          base value in the source fails to
                                          convert to the base value in the
                                          destination and the base value is
                                          able to convert to the customized
                                          value.

  CustomizedType        AnyType           The conversion will fail if the
                                          source fails to convert to the base
                                          value in the destination and the
                                          base value is able to convert to
                                          the customized value.

  AnyType               CustomizedType    The conversion will fail if the
                                          base value in the source fails to
                                          convert to the destination.

  SimpleType            SimpleType        The conversion will fail if there
                                          is no accessible compile-time
                                          assignment operator from the
                                          destination to the source.  This is
                                          determined using
                                          'bslmf_isconvertible'.
Any other combination of destination and source categories will fail to convert.
Usage:
The following snippets of code illustrate the usage of this component. This component can be used with types supported by the bdlat framework. In particular, types generated by the bas_codegen.pl tool can be used. For example, suppose we have the following XML schema inside a file called xsdfile.xsd:
  <?xml version='1.0' encoding='UTF-8'?>
  <xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'
             xmlns:bdem='http://bloomberg.com/schemas/bdem'
             elementFormDefault='unqualified'>

      <xs:complexType name='Employee'>
          <xs:sequence>
              <xs:element name='Name'   type='string'/>
              <xs:element name='Dept'   type='string'/>
              <xs:element name='Age'    type='int'/>
              <xs:element name='Salary' type='float'/>
          </xs:sequence>
      </xs:complexType>

      <xs:complexType name='Trainee'>
          <xs:sequence>
              <xs:element name='Name' type='string'/>
              <xs:element name='Dept' type='string'/>
              <xs:element name='Age'  type='int'/>
          </xs:sequence>
      </xs:complexType>

  </xs:schema>
Using the bas_codegen.pl tool, we can generate C++ classes for this schema:
  $ bas_codegen.pl -g h -g cpp -p test xsdfile.xsd
This tool will generate the header and implementation files for the test_employee and test_trainee components in the current directory.
Now suppose we want to create a hireTrainee function, that converts a trainee to an employee. Such a function could be written as follows:
  #include <test_employee.h>
  #include <test_trainee.h>

  #include <bdlat_symbolicconverter.h>

  using namespace BloombergLP;

  int hireTrainee(test::Employee       *result,
                  const test::Trainee&  trainee,
                  float                 salary)
  {
      int retCode = bdlat_SymbolicConverter::convert(result, trainee);

      result->salary() = salary;

      return retCode;
  }
The hireTrainee function can be used as follows:
  void usageExample()
  {
      test::Trainee trainee;

      trainee.name() = "Bob";
      trainee.dept() = "RnD";
      trainee.age()  = 24;

      test::Employee employee;

      int result = hireTrainee(&employee, trainee, 20000.00f);

      assert(0         == result);
      assert("Bob"     == employee.name());
      assert("RnD"     == employee.dept());
      assert(24        == employee.age());
      assert(20000.00f == employee.salary());
  }