The class AutoPtr implements a generalization of the class
std::auto_ptr. Like std::auto_ptr ab() can be used to handle a pointer
to an allocated data element, but unline bd(std::auto_ptr) AutoPtr can also be
used to handle a pointer to an array of data elements. Moreover, AutoPtr supports
non-destructive assignment and copy construction, therefore allowing AutoPtr
objects to be stored in, e.g., abstract containers. These latter
characteristics are supported through the use of reference counting.
Since almost all operators and members provide access to (mutable) data stored
in the AutoPtr object, no members return or expect const references, pointers
or AutoPtr objects.
NAMESPACE
FBB
All constructors, members, operators and manipulators, mentioned in this
man-page, are defined in the namespace FBB.
INHERITS FROM
FBB:RefCount
CONSTRUCTORS
AutoPtr<Type>():
The default constructor, initializing its data element pointer
to a 0-pointer to a Type data type.
explicit AutoPtr<Type>(Type *data):
The constructor initializing its data element pointer to data. The
data pointer must have been dynamically allocated and is owned by the
AutoPtr object.
AutoPtr<Type>(Type *data, size_t size):
The constructor initializing its data element pointer to data,
which is an array of sizeData elements. The
data pointer must have been dynamically allocated and is owned by the
AutoPtr object.
AutoPtr<Type>(AutoPtr<Type> &other):
The copy constructor copies the information referred to by other
to the current AutoPtr using reference counting.
OVERLOADED OPERATORS
Some of the following operators return references or pointers to data
elements owned by the AutoPtr object. It should be noted that, since reference
counting is used for the data pointer and not for the data itself, modifying a
data element using one AutoPtr object will usually also modify the data element
of other AutoPtr objects sharing the object's data pointer.
AutoPtr<Type> &operator=(AutoPtr<Type> &other):
The assignment operator assigns the information referred to by
other to the current AutoPtr using reference counting.
Type &operator*():
The dereference operator returns a reference to the data element
pointed to by the AutoPtr. If the AutoPtr object points to an array of Type
elements, a reference to its first element is returned.
Type *operator->():
The pointer to member operator returns a pointer to the data element
pointed to by the AutoPtr. If the AutoPtr object points to an array of Type
elements, a pointer to its first element is returned.
Type &operator[](int idx):
The index operator returns a reference to the element idx
owned by the AutoPtr. No array-bound checking is performed.
AutoPtr<Type> operator+(int idx):
The plus operator returns an AutoPtr object whose dereference operator
will return element idx of the current AutoPtr object, allowing users to
apply pointer arithmetic. The returned AutoPtr object shares the data with the
current object, but uses an offset value of its own to specify its own first
element. Consequently, AutoPtr's pointer arithmetic operators may be nested.
AutoPtr<Type> &operator+=(int idx):
The plus arithmetic assignment operator returns the current AutoPtr
object whose dereference operator is incremented by idx.
AutoPtr<Type> operator-(int idx):
The minus operator returns an AutoPtr object whose dereference operator
will return element -idx of the current AutoPtr object, allowing users to
apply pointer arithmetic. The returned AutoPtr object shares the data with the
current object, but uses an offset value of its own to specify its own first
element. Consequently, AutoPtr's pointer arithmetic operators may be nested.
AutoPtr<Type> &operator+=(int idx):
The subtract arithmetic assignment operator returns the current AutoPtr
object whose dereference operator is decremented by idx.
PUBLIC MEMBER FUNCTIONS
All members of std::ostringstream and std::exception are
available, as FBB::AutoPtr inherits from these classes.
Some of the following members return references or pointers to data
elements owned by the AutoPtr object. It should be noted that, since reference
counting is used for the data pointer and not for the data itself, modifying a
data element using one AutoPtr object will usually also modify the data element
of other AutoPtr objects sharing the object's data pointer.
Type *get():
The get() member returns a pointer to the data element
pointed to by the AutoPtr. If the AutoPtr object points to an array of Type
elements, a pointer to that array's first element is returned.
Type *release():
The release() member returns a pointer to the data element pointed
to by the AutoPtr. If the AutoPtr object points to an array of Type elements, a
pointer to its first element is returned. The returned pointer is owned by
release()'s caller and the current object's data pointer is set to
zero. If the data were shared among several AutoPtr objects, the remaining
objects will keep their data. In that case the returned pointer is a pointer
to a copy of the shared data. The Type data type must support a copy
constructor.
Type *releaseAll():
The releaseAll() member returns a pointer to the data element
pointed to by the AutoPtr. If the AutoPtr object points to an array of Type
elements, a pointer to its first element is returned. The returned pointer is
owned by releaseAll()'s caller. The data pointers of all AutoPtr objects that
shared the returned data become zero-pointers. if the AutoPtr object contains a
pointer to an array of Type objects, the returned pointer will also point
to such an array. The Type data type must support a copy constructor.
FREE OVERLOADED OPERATOR
Note that the following operator is defined in the FBB
namespace. Koenig lookup is used to select it whenever the appropriate types
of arguments are provided to the plus operator.
AutoPtr<Type> FBB::operator+(int idx, AutoPtr<Type> &rhs):
This free (classless) plus operator returns an AutoPtr object whose
dereference operator will return element idx of the rhsAutoPtr object,
allowing users to apply pointer arithmetic. The returned AutoPtr object shares
the data with the rhs object, but uses an offset value of its own to
specify its own first element. Consequently, AutoPtr's pointer arithmetic
operators may be nested.