[ Lit Window Library at SourceForge[ Lit Window Productions Homepage ]  [ wxWidgets Tips&Tricks ]  [  wxVisualSetup ]

Main Page | Modules | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

Data Adapters Howto

Overview

Data adapters allow access to objects when the class declaration is not available at compile time. Use data adapters to create a library of algorithms for common issues such as

Use this library with objects of any class, even if the class declaration did not exist when you wrote the library. Data adapters provide the bridge between your class declaration and the algorithm in the library.

Quick Summary

To prepare your program for data adapters, add...
  1. IMPLEMENT_ADAPTER_TYPE(name) for simple data types and enumerations.
  2. BEGIN_ADAPTER(name) / END_ADAPTER() tables for struct or class definitions.
  3. IMPLEMENT_CONTAINER(name) for container types.

Choose which adapter to use...

  1. Use litwindow::accessor to access the values of simple data types and enumerations.
  2. Use litwindow::aggregate to access members of struct and class definitions.
  3. Use litwindow::container to access the elements in a container.

To convert the basic adapter litwindow::accessor into the other forms...

  1. Use litwindow::accessor::is_aggregate to determine if the adapter has an an aggregate adapter.
  2. Use litwindow::accessor::get_aggregate to get the aggregate adapter for the object.
  3. Use litwindow::accessor::is_container to determine if the adapter has a container adapter.
  4. Use litwindow::accessor::get_container to get the container adapter for the object.

Three different kinds of data adapters.

Data adapters come in three varieties.

Each kind has a const and a non const version.

You can create accessor adapters from any kind of object, including aggregates and containers. Aggregate adapters can only be created from aggregate data types and container adapters only from container data types.

accessor and const_accessor.

The most basic form of an adapter is the litwindow::accessor. Think of an accessor as a pointer. Like an untyped pointer it can point to any type of object, but it also contains the type information of the object it points to.

Preparing to use accessors

Before you can use an accessor for a data type, you must make this data type available to the accessor adapter logic. Use IMPLEMENT_ADAPTER_TYPE(name) to make a data type available for accessor adapters. Include this macro anywhere in your sourcecode. You can use this macro even for data types you did not write yourself.
Note:
You will probably use this macro only for enumerations. The basic data types char, int, float etc... are included in the litwindow library. Aggregate data types (see next section) are defined differently.
IMPLEMENT_ADAPTER_TYPE(Fix1::E1)    // Prepare enumeration E1 for use with data adapters.

Using accessors

        //beginexample testbasicaccess
            // define a test object
        int i1=10;
            // use 'make_const_accessor' to create a const_accessor for any kind of object
        const_accessor a(make_const_accessor(i1));

            // use 'to_string' to return the value as a string
        CPPUNIT_ASSERT_EQUAL(tstring(_T("10")), a.to_string());

            // 'is_int' returns true if the value is an integer
        CPPUNIT_ASSERT(a.is_int());
            // 'to_int' returns the value as an integer - provided is_int returns true
        CPPUNIT_ASSERT(a.to_int()==10);

            // 'is_container' returns true if the value is a container
        CPPUNIT_ASSERT(a.is_container()==false);

        Fix1 f;
            // 'const_accessor's can be assigned just like pointers
        a=make_const_accessor(f);
            // this would throw an exception because there is no 'to_string' method for f...
//        CPPUNIT_ASSERT(a.to_string()=="100");
        CPPUNIT_ASSERT(a.is_int()==false);
        CPPUNIT_ASSERT(a.is_container()==false);
        //endexample

Use from_string and pass a string representation of the value to set the object value.

aggregate and const_aggregate

aggregate adapters allow access to individual members of an aggregate data type (struct or class). It is a collection of accessors. Each accessor represents a member pointer.

Copyright 2004, Hajo Kirchhoff, Lit Window Productions