Tools.h++ Manual

22-124 104011 Tandem Computers Incorporated
22
second line, the expression
vec[i]
is being used as an lvalue. In this case, the
compiler uses the assignment operator for
RWTVirtualElement<T>
. This
assignment operator recognizes that the expression is being used as an lvalue
and automatically marks the appropriate page as "dirty", thus guaranteeing
that it will be written back out to the swapping medium.
Slices, as well as individual elements, can also be addressed. These should be
used wherever possible as they are much more efficient because they allow a
page to be locked and used multiple times before unlocking.
The class
T
must have:
well-defined copy semantics (
T::T(const T&)
or equiv.);
well-defined assignment semantics (
T::operator=(const T&)
or equiv.).
In addition, you must never take the address of an element.
Example In this example, a virtual vector of objects of type
ErsatzInt
is exercised. A
disk-based page heap is used for swapping space.
Code Example 22-8
#include <rw/tvrtarry.h>
#include <rw/rstream.h>
#include <rw/diskpage.h>
#include <stdlib.h>
#include <stdio.h>
struct ErsatzInt {
char buf[8];
ErsatzInt(int i) { sprintf(buf, "%d", i); }
friend ostream& operator<<(ostream& str, ErsatzInt& i)
{ str << atoi(i.buf); return str; }
};
main() {
RWDiskPageHeap heap;
RWTValVirtualArray<ErsatzInt> vec1(10000L, &heap);
for (long i=0; i<1000L; i++)
vec1[i] = i; // Some compilers may need a cast here
cout << vec1[100] << endl; // Prints "100"