Moreover,
The DynObj offers type safety, hence templates are required (inheritence doesn't work because the DAO is only held known by pointer and is not created on the stack but on the heap).
Thus is born the DynObj<T> class.
Foo* getFoo() {
Foo* foo = new Foo;
// do stuff with/to foo, then
return foo;
}
With DynObj you would want to do, but can't, the following:
DynObj<Foo> getFoo() {
DynObj<Foo> foo( new Foo );
// do stuff with/to foo, then
return foo;
}
It seems that a special proxy is needed to represent unnamed temporaries like the return value of getFoo(). The role of this proxy should be purely to transfer a DAO from somewhere to a receiving DynObj, in a clear and unmistakable fashion, or if there is no DynObj to recieve it, to delete it. I.e.,
DynTmp<Foo> getFoo() {
DynObj<Foo> foo( new Foo );
// do stuff with/to foo, then
return foo.moveToTmp();
}
Thus is born the DynTmp<T> class to represent unnamed temporaries. The moveToTmp() is the "clear and unmistakable fashion". DynTmp also allows
DynTmp<Foo> getFoo() { return new Foo; }
...
DynObj<Foo> dynObj = getFoo();
std::list<DynObj<Foo>::InValueContainer> list;
list.push_back(DynTmp<Foo>(new Foo));
Note that DynTmp implements move-on-copy semantics.
DynTmp<T> createT() {return new T;} // return unnamed temp
DynObj<T> dynObj = createT();
Therefore, provide a class that represents a reference to the DAO, and automatically asserts that the DAO still exists, whenever the DAO is accessed.
Thus is born the RRef<T> class. This class simply stores the pointer to the DAO, given to it by a DynObj, and does an assert before the DAO is accessed through one of RRef's member functions:
DynObj<Foo> foo;
foo().method(); // call Foo::method()
RRef<Foo> refFoo;
refFoo().method(); // first assert that foo still exists
// THEN call Foo::method()
It would therefore be useful if it would be very simple to make a class usable as an RRef. Even better, if this automatically makes all subclasses also RRef'able. Inheritance is ideal:
class Bar: public RRefable {...}; ... Bar bar; bar.method(); RRef<Bar> refbar(bar); refbar().method(); // same as bar.method(), EXCEPT that first // asserts that bar still exists before // calling Bar::method()
1.3.2