Smart Pointers

smart_ptr (Policy Based Smart Pointer)

Author:
David Maisonave (Axter) (609-345-1007) (www.axter.com)
Copyright (C) 2006

Contents

Download
Revision
License
Why use smart_ptr
Policy Classes
Multithreading
smart_ptr Members
Motivation

Introduction

smart_ptr is a smart pointer policy class that can use different ownership logic and semantic polices, which allows the developer to get the best performance and/or interface for a particular requirement. It has synchronization logic which allows it to be used in a Multithreading environment.

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

Example Usage

#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;

}

Revision

$Date: 3/23/06 3:48a $ 
$Revision: 8 $
To view revison history, see Revision_History

Download

Download smart pointer source code using following link:
smartptr.zip
Note: All the smart pointers listed in this document are implemented using *.hpp files. There's no object files, .cpp files, or DLL/SO files. To use the smart pointers in your project, just add an include.
#include "smart_ptr.hpp"


Windows users can optionally use (CHM) help file, which can be downloaded through the following link:
http://axter.com/smartptr/smartptr_chm.zip

Generated on Wed Mar 29 21:58:58 2006 for Smart Pointers by  doxygen 1.4.6.Axter [Axter-Extended-Version]