Smart Pointers

smart_ptr Motivation

(smart_ptr ) A Policy Based Smart Pointer
By David Maisonave (Axter)
www.axter.com

STL and abstract polymorphism are powerful C++ tools, but they don’t mix well using what’s available in the current standard library. The auto_ptr can’t be used for STL containers, and even smart pointers like boost::shared_ptr can give unwanted results when copying a container of abstract smart pointers from one container to another or one part of a container to another. Moreover, is not easy to tweak the commonly available smart pointers so-as to give the best performance for an individual or unique requirement. The smart_ptr class is a policy based smart pointer that allows easy mixing of abstract types with STL containers and STL algorithms.

The smart_ptr has policies that allow the cloning of objects, without requiring the object to have clone functions/logic, and it has additional policies that allow optimal performance increase for unique target requirements.

It can be used with sorted containers, and unlike the boost pointer container, the smart_ptr can be used as the first type in a map container.

The smart_ptr has three main ownership policy classes, which include deep_copy_policy, copy_on_write_policy, and shared_ptr_policy.

The copy_on_write_policy and shared_ptr_policy have sub policies that allow three different types of reference policy (ref_link_policy, ref_count_policy, and ref_intrusive_policy).

All three ownership policies have an optional LOCK policy that can be used to create a synchronized smart pointer.

If all these policies sound confusing, or overwhelming, don’t worry, because the smart_ptr has default policies for the most general requirements. For most requirements the smart_ptr can be used as-is, without explicitly specifying any particular policy. smart_ptr<Shape> pShape(new Circle);

The smart_ptr can be used as a general purpose pointer, or as a requirement unique specific customizable smart pointer. The policies are useful tools for those who need to get the best optimal performance for a particular usage, the best compile time or runtime type checking, and/or to develop more generic code. There’s also a synchronization policy that allows smart_ptr to not only synchronize the smart_ptr, but also synchronize the pointee itself. This allows smart_ptr to be used safely in a multithreading environment.


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