Topic: Proposal: Operator <-
Author: shang@corp.mot.com (David (Lujun) Shang)
Date: Fri, 21 Aug 92 19:41:34 GMT Raw View
To provide a transparent generic object pointer (reference), we need a
new operator <- to clear the side effect caused by operator ->.
Consider the following example:
template <class RefType, class ObjectType>
class REF
{ RefType ref;
public:
ObjectType * open ()
{ return (ObjectType*) ref.open(); };
void close () { ref.close(); };
ObjectType * operator-> () { return open(); };
ObjectType * operator<- () { return close(); };
ObjectType operator*() { return *open(); };
REF<RefType,ObjectType> operator++ ()
{ return REF<RefType,ObjectType> rf ={ ref.next(); };
...
};
The operator <- is always called implicitly by the compiler:
When operator-> followed by a lvalue, operator<- is called
after the assignment, otherwise, operator<- is called
immediately after the member is read.
For example:
REF<PersistentRef, Circle> circle_ref;
position = circle_ref->position;
Operator-> will call PersistentRef's "open" to read the circle object
stored in a persistent storage, and the operator <- will be called
after the object is read into the memory.
The default operator <- for standard pointer is equivalent an empty
operation.
Without built-in operator <-, the programmer must close the object
storage explicitly:
position = circle_ref->position; circle_ref.close();
If position is a member function:
position = circle_ref->position();
you must call close at the end of the function. This may cause another
problem: position() can not be called by other member functions within
the object, otherwise the open/close operations will be unbalenced.
If operator<- is provided by the language, then we can write programs
independent to any particular memory management protocol. For example,
we can write dynamic data linking structures like trees, chains, rings
that can run both in standard alloc/free memory and in MS Windows heap
without changing the source.
David Shang