stdx::ImplPtr Class Template Reference
template<class T>
class stdx::ImplPtr< T >
The handle/body pattern is a useful idiom for truly separating a class interface from its implementation. With a traditional C++ class changes to the private data member type or adding new private members will require all users of the class to recompile - even though this is strictly an implementation change! The handle/body pattern stores just a pointer in an outer (handle) class and this pointer points to an inner (body) class. The body class contains all implemenation objects. This works well, but a programmer must be careful to correctly allocate/deallocate the body class and manage copy/assigment operators. The ImplPtr class helps with these issues. An example of its use is below:
class ExampleClass {
public:
// public members here ...
private:
struct Impl;
stdx::ImplPtr<Impl> impl;
};
The programmer then defines the nested Impl struct in the source file. The ImplPtr class will automatically use the default Impl contstructor to dynamically allocate a new body object on construction and then correctly delete the body object no destruction. Additionally, copying the ImplPtr causes a _copy_ of the body class to be made as opposed to having two handle classes pointing to the same body class.
This is loosely based on a previously proposed impl_ptr class by Peter Dimov on the boost mailing lists. I've simplified things and included create functions so that classes which use the pimpl idiom do not need to explicitly allocate the impl class. http://groups.yahoo.com/group/boost/files/impl_ptr
Note that AllocFunctions are needed to avoid the situation where we need to constructor or destruct an incomplete type.
Public Methods
- ImplPtr ()
- ImplPtr (T *ptr, AllocFunctions< T > *fns=DefaultAllocFunctions< T >::instance())
- ~ImplPtr ()
- ImplPtr (const ImplPtr &ptrToCopy)
- operator= (const ImplPtr &ptrToCopy)
- copy () const
- operator-> ()
- operator-> () const
- operator * ()
- operator * () const