1 ////////////////////////////////////////////////////////////////////////////////
3 // Copyright (c) 2006 Peter Kümmel
4 // Permission to use, copy, modify, distribute and sell this software for any
5 // purpose is hereby granted without fee, provided that the above copyright
6 // notice appear in all copies and that both that copyright notice and this
7 // permission notice appear in supporting documentation.
8 // The author makes no representations about the
9 // suitability of this software for any purpose. It is provided "as is"
10 // without express or implied warranty.
11 ////////////////////////////////////////////////////////////////////////////////
12 #ifndef LOKI_PIMPL_INC_
13 #define LOKI_PIMPL_INC_
15 // $Id: Pimpl.h 751 2006-10-17 19:50:37Z syntheticpp $
18 /// \defgroup PimplGroup Pimpl
20 #ifndef LOKI_INHERITED_PIMPL_NAME
21 #define LOKI_INHERITED_PIMPL_NAME d
24 #ifndef LOKI_INHERITED_RIMPL_NAME
25 #define LOKI_INHERITED_RIMPL_NAME d
31 //////////////////////////////////////////
32 /// \class ConstPropPtr
34 /// \ingroup PimplGroup
35 /// Simple const propagating smart pointer
36 /// Is the default smart pointer of Pimpl.
37 //////////////////////////////////////////
42 explicit ConstPropPtr(T *p) : ptr_(p) {}
56 const T *operator->() const
60 const T &operator*() const
67 ConstPropPtr(const ConstPropPtr &);
68 ConstPropPtr &operator=(const ConstPropPtr &);
73 ////////////////////////////////////////////////////////////////////////////////
76 /// \ingroup PimplGroup
78 /// Implements the Pimpl idiom. It's a wrapper for a smart pointer which
79 /// automatically creates and deletes the implementation object and adds
80 /// const propagation to the smart pointer.
84 ////////////////////////////////////////////////////////////////////////////////
89 typename Pointer = ConstPropPtr<T>
102 // Don't compile with incomplete type
104 // If compilation breaks here make sure
105 // the compiler does not auto-generate the
106 // destructor of the class hosting the pimpl:
107 // - implement the destructor of the class
108 // - don't inline the destructor
109 typedef char T_must_be_defined[sizeof(T) ? 1 : -1 ];
115 return ptr_.operator->();
120 return ptr_.operator*();
123 const T *operator->() const
125 return ptr_.operator->();
128 const T &operator*() const
130 return ptr_.operator*();
138 const Pointer &wrapped() const
145 Pimpl(const Pimpl &);
146 Pimpl &operator=(const Pimpl &);
152 template<class T, typename Pointer = ConstPropPtr<T> >
155 Pimpl<T,Pointer> LOKI_INHERITED_PIMPL_NAME;
159 //////////////////////////////////////////
162 /// \ingroup PimplGroup
163 /// Convenience template for the
164 /// implementations which Pimpl points to.
165 //////////////////////////////////////////
171 //////////////////////////////////////////
174 /// \ingroup PimplGroup
175 /// Convenience template which uses ImplOf
176 /// as implementation structure
177 //////////////////////////////////////////
180 template<class T, template<class> class Ptr = ConstPropPtr>
186 typedef Pimpl<ImplOf<T>, Ptr<ImplOf<T> > > Type;
189 typedef PimplOwner<ImplOf<T>, Ptr<ImplOf<T> > > Owner;
193 template<class T, class UsedPimpl = typename PimplOf<T>::Type >
196 typedef typename UsedPimpl::Impl &Type;
203 Owner() : LOKI_INHERITED_RIMPL_NAME(*pimpl)
206 Type LOKI_INHERITED_RIMPL_NAME;
213 #endif // end file guardian