The OffsetChunkPair class (See Figure 3.19) is internally used for the convenience of coding. The class is invisible from the user.
There are two member variables in the class. One is an offset of a chunk, and the other is the pointer to the chunk.
When instantiated, the offset is initialized with the parameter and the chunk is set as 0.
The operation map(arena) maps the offset and sets the value of the chunk by invoking Arena::map(). On the other hand, the operation map(window) works in the same way but invokes Window::map().
The overloaded operators == and != can be used with either another OffsetChunkPair object or 0. The operators just compare the offsets. The assignment operator = is used with an offset to set the value of the offset. The chunk is set as 0 in the assignment.
The overloaded cast operators (OffsetType) and (Chunk *) extracts the member offset and chunk respectively. These cast operators enable the mixed usage with offsets and pointers.
The following code is a sample usage of the OffsetChunkPair class. The enhanced flexibility and clarity of coding are easily recognized. All the operators in the class are declared as inline and thus there is no performance degradation via the usage of the class.
// in the operations of the Arena class typedef Chunk *PC; OffsetType off, off2; Chunk *chunk; OffsetChunkPair obj1; SizeType size; obj1 = off; // assignment with OffsetType obj1.map(this); // mapping chunk = obj1; // converted to Chunk * off2 = obj1; // converted to OffsetType if (obj1 != off2) { ... } // comparison with OffsetType size = PC(obj1)->getSize(); // extract chunk and access