|
The smart_ptr class can be used with STL containers to create containers of smart pointers, moreover it can be used to create a container of abstract based objects via smart_ptr. In general, smart_ptr is faster than boost::shared_ptr. When used with STL containers, the smart pointer is faster than the boost pointer containers. More importantly, the interface for an STL container of smart_ptr is a 100% compatible with STL containers, which is not the case with the boost pointer containers.
smart_ptr has a policy that allows it to synchronize access to both the smart pointer and the pointee. This allows both the smart_ptr and the pointee to safely be used in a multithreading environment.
The smart_ptr has been compiled and tested on VC++ 6.0, 7.1, 8.0, GNU 3.x, Borland 5.5, and Comeau 4.3.
This help document includes three other smart pointers.
cow_ptr
copy_ptr
sync_ptr
These three smart pointers are not part of the smart_ptr class, but because much of smart_ptr's implementation derived from these three classes, they're included in the help document as a base of reference. They're also used in the unit testing as a base line comparison, and to insure that the smart_ptr policies are not effecting performance.
For more information about other types of smart pointers, see Other_Smart_Pointers
#include <vector> #include <list> #include <deque> #include <set> #include <map> #include "../shape_test.h" #include "../baseclass_test.h" #include "../smartptr/smart_ptr.hpp" //Only required header for normal smart_ptr usage void example_smart_ptr_usage() { smart_ptr<Shape> pShape1(new Circle("blue")); smart_ptr<Base, deep_copy_policy<> > pBase1 = new Derived1; smart_ptr<Shape, shared_ptr_policy<ref_link_policy>, clone_function_allocator_policy > pShape2(new Square("white")); smart_ptr<Base, copy_on_write_policy<> > pBase3(new Derived2); smart_ptr<Shape, shared_ptr_policy<>, clone_function_allocator_policy > pShape4(new Square("red")); smart_ptr<Base, copy_on_write_policy<ref_link_policy> > pBase5(new Derived1); smart_ptr<Shape, shared_ptr_policy<ref_link_policy> , clone_static_function_allocator_policy> pShape6(new Square("black")); std::vector<smart_ptr<Shape> > vShape; std::list<smart_ptr<Shape, deep_copy_policy<> > > lstShape; std::deque<smart_ptr<Shape, copy_on_write_policy<ref_link_policy>, clone_static_function_allocator_policy> > dqShape; //The smart_ptr can also be used with sorted containers (std::map, std::set). //When used with sorted containers, the base class must have an operator<() function. std::map<int, smart_ptr<Shape> > mShape1; std::map<smart_ptr<Shape>, int> mShape2; std::set<smart_ptr<Shape> > sShape; }
$Date: 3/23/06 3:48a $ $Revision: 8 $
#include "smart_ptr.hpp"