There are a few design decisions that will affect how the classes are used. Besides these the classes are much like normal standard containers and provides almost the same interface. The new conventions are:
If the user tries to insert the null pointer, the operation will throw a bad_pointer exception (see Example 1).
Use nullable to allow null pointers.
Please notice that all preconditions of the form
x != 0;
are not active when the you have instantiated a container with nullable<T> as in
boost::ptr_vector< boost::nullable<animal> > vec; vec.push_back( 0 ); // ok
This is done to make the containers easier and safer to use. It promotes a kind of pointer-less programming and the user of a class needs not worry about pointers except when allocating them (see Example 2). Iterators that provide access to the naked pointers are also provided since they might be useful in rare cases. For example, whenever begin() returns an iterator, ptr_begin() will return an iterator that allows one to iterate over the stored pointers.
For example, in ptr_set<T> the ordering is by default done by boost::ptr_less<T> which compares the indirected pointers. Similarly, operator==() for container<Foo> compares all objects with operator==(const Foo&, const Foo&).
This is because most polymorphic objects cannot be copied directly, but they can often be so by a use of a member function (see Example 4). Often it does not even make sense to clone an object in which case a large subset of the operations are still workable.
This is necessary because all pointer containers take ownerships of stored objects (see Example 5).
All containers take ownership of the stored pointers and therefore a container needs to have its own copies (see Example 5).
This can of course also be convenient. Whenever it happens, an SmartContainer::auto_type object is used to provide an exception-safe transfer (see Example 6).
This makes it possible to exchange data safely between different pointer containers without cloning the objects again (see Example 7).
Two special member functions, clone() and release(), both return an auto_ptr<SmartContainer> which can be assigned to another pointer container. This effectively reduces the cost of returning a container to one heap-allocation plus a call to swap() (see Example 3).
Because the containers in this library wrap standard containers, the rules for invalidation of iterators are the same as the rules of the corresponding standard container.
For example, for both boost::ptr_vector<T> and std::vector<U> insertion and deletion only invalidates the deleted element and elements following it; all elements before the inserted/deleted element remain valid.
Navigate:
Copyright: | Thorsten Ottosen 2004-2006. Use, modification and distribution is subject to the Boost Software License, Version 1.0 (see LICENSE_1_0.txt). |
---|