autoptr3bobcat
A generalization of std::auto_ptr
Usage
#include <bobcat/autoptr>
Overview
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 copy- and move construction, therefore allowing AutoPtr objects to be stored in, e.g., abstract containers. The class uses reference counting to share data among multiple objects.
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
- o
-
AutoPtr<Type>():
The default constructor, initializing its data element pointer to a 0-pointer to a Type data type.
- o
-
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 theAutoPtrobject.
- o
-
AutoPtr<Type>(Type *data, size_t size):
The constructor initializing its data element pointer to data, which is an array of size Data elements. The data pointer must have been dynamically allocated and is owned by theAutoPtrobject.
- o
-
AutoPtr<Type> offers copy and move constructors.
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.
- o
-
AutoPtr<Type> &operator=(AutoPtr<Type> &other):
The assignment operator assigns the information referred to by other to the currentAutoPtrusing reference counting.
- o
-
Type &operator*():
The dereference operator returns a reference to the data element pointed to by theAutoPtr. If theAutoPtrobject points to an array of Type elements, a reference to its first element is returned.
- o
-
Type *operator->():
The pointer to member operator returns a pointer to the data element pointed to by theAutoPtr. If theAutoPtrobject points to an array of Type elements, a pointer to its first element is returned.
- o
-
Type &operator[](int idx):
The index operator returns a reference to the element idx owned by theAutoPtr. No array-bound checking is performed.
- o
-
AutoPtr<Type> operator+(int idx):
The plus operator returns anAutoPtrobject whose dereference operator will return element idx of the currentAutoPtrobject, allowing users to apply pointer arithmetic. The returnedAutoPtrobject 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.
- o
-
AutoPtr<Type> &operator+=(int idx):
The plus arithmetic assignment operator returns the currentAutoPtrobject whose dereference operator is incremented by idx.
- o
-
AutoPtr<Type> operator-(int idx):
The minus operator returns anAutoPtrobject whose dereference operator will return element -idx of the currentAutoPtrobject, allowing users to apply pointer arithmetic. The returnedAutoPtrobject 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.
- o
-
AutoPtr<Type> &operator+=(int idx):
The subtract arithmetic assignment operator returns the currentAutoPtrobject 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.
- o
-
Type *get():
Theget()member returns a pointer to the data element pointed to by theAutoPtr. If theAutoPtrobject points to an array of Type elements, a pointer to that array’s first element is returned.
- o
-
Type *release():
Therelease()member returns a pointer to the data element pointed to by theAutoPtr. If theAutoPtrobject points to an array of Type elements, a pointer to its first element is returned. The returned pointer is owned byrelease()’s caller and the current object’s data pointer is set to zero. If the data were shared among severalAutoPtrobjects, 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.
- o
-
Type *releaseAll():
ThereleaseAll()member returns a pointer to the data element pointed to by theAutoPtr. If theAutoPtrobject points to an array of Type elements, a pointer to its first element is returned. The returned pointer is owned byreleaseAll()’s caller. The data pointers of allAutoPtrobjects that shared the returned data become zero-pointers. if theAutoPtrobject 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.
- o
-
AutoPtr<Type> FBB::operator+(int idx, AutoPtr<Type> &rhs):
This free (classless) plus operator returns anAutoPtrobject whose dereference operator will return element idx of the rhsAutoPtrobject, allowing users to apply pointer arithmetic. The returnedAutoPtrobject 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.
Examples
#include "../autoptr"
using namespace std;
using namespace FBB;
int main()
{
AutoPtr<int> autoInt(new int(4));
cout << "\n1: test simple construction and copy construction\n\n";
{
AutoPtr<int> auto2(autoInt);
cout << *auto2 << endl;
cout << *((0 + auto2) - 2) << endl;
}
cout << *autoInt << "\n" <<
"\n2: test array\n\n";
{
AutoPtr<int> autoarr(new int[5], 5);
autoarr[1] = 1;
autoarr[2] = 2;
cout << autoarr[2] << "\n" <<
autoarr[3] << endl;
cout << "\n3: assign to former autoPtr of different data size\n\n";
autoInt = autoarr;
}
cout << autoInt[2] << "\n" <<
autoInt[3] << endl;
cout << "\n4: pointer-based operations\n\n";
cout << *((autoInt + 4) - 2) << "\n" <<
*((autoInt + 6) - 4) << "\n" <<
((6 + autoInt) - 4)[0] << endl;
cout << "\n5: resetting\n\n";
AutoPtr<int> autoSave(autoInt);
autoInt.reset(new int(12345));
cout << *autoInt << "\n" <<
autoSave[2] << endl;
cout << "\n6: resetting all\n\n";
autoSave = autoInt;
autoInt.resetAll(new int(12345));
cout << *autoInt << "\n" <<
*autoSave << endl;
}
Files
bobcat/autoptr - defines the class interface
Distribution files
- o
-
bobcat_2.08.01-x.dsc: detached signature;
- o
-
bobcat_2.08.01-x.tar.gz: source archive;
- o
-
bobcat_2.08.01-x_i386.changes: change log;
- o
-
libbobcat1_2.08.01-x_*.deb: debian package holding the libraries;
- o
-
libbobcat1-dev_2.08.01-x_*.deb: debian package holding the libraries, headers and manual pages;
- o
-
http://sourceforge.net/projects/bobcat: public archive location;
Bobcat
Bobcat is an acronym of `Brokken’s Own Base Classes And Templates’.
Releases
2 releases since Debian 5.0 (Lenny); the text changed 1 time.