VR-Forces 4.7 Class Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
16.5 - The Reader/Writer Registry

To allow readable, writable objects to easily contain other readable, writable objects, the DtReaderWriter creates an instance of DtReaderWriterRegistry (rwRegistry.h).

The DtReaderWriter registry is a list of DtReaderWriter pointers. (Actually it is a hash list indexed by the names of the objects.) The constructor for a DtReaderWriter takes a registry pointer as an argument. If this pointer is non-null, the DtReaderWriter object registers itself with the registry pointer provided. In this way, you can compose new readable, writable objects from existing ones. It is fairly common, in fact, to have objects that generate no new readable, writable data, and therefore do not need to override putData() and getData(). Example RW-1 shows the key elements needed to make a new readable, writable class that contains a real number and an integer number, using the existing DtRwReal and DtRwInt classes.

Note
We have our new class’s constructor take a pointer to a DtReaderWriterRegistry so that it could be used as part of some other readable, writable class.

Example RW-1

In the header file realAndInt.h, we do the following:

#include "rwReal.h"
#include "rwInt.h"
class RealAndInt : public DtReaderWriter
{
public:
RealAndInt(const DtString& name,
DtReaderWriterRegistry* parentRegistry = 0);
...
protected:
DtRwReal myRealNumber;
DtRwInt myIntegerNumner;
};

In the implementation file realAndInt.cxx, we do the following:

#include "realAndInt.h"
RealAndInt::RealAndInt(const DtString& name,
DtReaderWriterRegistry* parentRegistry) :
DtReaderWriter(name, parentRegistry),
myRealNumber("real-number", &myRegistry),
myIntegerNumber("integer-number", &myRegistry)
{
myIntegerNumber = 0;
myRealNumber = 0.0;
}
...

The myRegistry variable referred to in the constructor is a member variable of the DtReaderWriter class that the RealAndInt class inherits. An example of its use and sample output, assuming an already open file pointer fp, as follows:

...
RealAndInt twoNumbers("two-numbers");
twoNumbers.setReal(7.66);
twoNumbers.setInteger(18);
twoNumbers.putSelf(fp);
...

would write to the open file pointed to by fp, as follows:

(two-numbers
(real-number 7.66)
(integer-number 18)
)
Note
The names used for DtReaderWriter objects cannot contain spaces, and must not start with a digit.

If your object does not contain any local data that it wants to write other than the readable, writable objects it uses, there is no need to override putData() and getData(), and in fact, no need for putSelf() and getSelf() to invoke these member functions. By default, these functions are not called. To indicate that an object has local data and wants getData() or putData() to be called, call the following, typically in the constructor of the object:

myRegistry.setLocalData();

[<< Multiple Inheritance and DtReaderWriter] [Home] [Top of Page] [Handling Unspecified Parameters >>]


Document ID: Generated on Fri Apr 26 21:53:14 EDT 2019 from SVN revision 197883
Copyright © 2005-2019 VT MAK. All Rights Reserved (www.mak.com)