[
Lit Window Library at SourceForge
] [
Lit Window Productions Homepage
] [
wxWidgets Tips&Tricks ]
[
wxVisualSetup ]
Data Adapters Howto
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
- loading/saving settings
- data exchange for dialogs
- printing debug information
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.
To prepare your program for data adapters, add...
- IMPLEMENT_ADAPTER_TYPE(name) for simple data types and enumerations.
- BEGIN_ADAPTER(name) / END_ADAPTER() tables for struct or class definitions.
- IMPLEMENT_CONTAINER(name) for container types.
Choose which adapter to use...
- Use litwindow::accessor to access the values of simple data types and enumerations.
- Use litwindow::aggregate to access members of struct and class definitions.
- Use litwindow::container to access the elements in a container.
To convert the basic adapter litwindow::accessor into the other forms...
- Use litwindow::accessor::is_aggregate to determine if the adapter has an an aggregate adapter.
- Use litwindow::accessor::get_aggregate to get the aggregate adapter for the object.
- Use litwindow::accessor::is_container to determine if the adapter has a container adapter.
- Use litwindow::accessor::get_container to get the container adapter for the object.
Data adapters come in three varieties.
Each kind has a const and a non const version.
- litwindow::accessor adapters allow access to the value of an object. The object itself is considered opaque. It has only one value and can be accessed only in its entirety.
- litwindow::aggregate adapters allow access to individual members of aggregates - struct or class type objects. They access one member at a time. aggregate adapters define an iterator that iterates over all member variables of the aggregate, including its superclasses if any.
- litwindow::container adapters allow access to the objects in a container. They define an iterator that iterates over all objects in the container, as well as common iterator operations such as begin, end, insert, delete.
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.
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.
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)
int i1=10;
const_accessor a(make_const_accessor(i1));
CPPUNIT_ASSERT_EQUAL(tstring(_T("10")), a.to_string());
CPPUNIT_ASSERT(a.is_int());
CPPUNIT_ASSERT(a.to_int()==10);
CPPUNIT_ASSERT(a.is_container()==false);
Fix1 f;
a=make_const_accessor(f);
CPPUNIT_ASSERT(a.is_int()==false);
CPPUNIT_ASSERT(a.is_container()==false);
Use from_string and pass a string representation of the value to set the object value.
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