1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/base/PtrTypes.h
10 * \ingroup ZYPP_SMART_PTR
13 #ifndef ZYPP_BASE_PTRTYPES_H
14 #define ZYPP_BASE_PTRTYPES_H
18 #include <boost/scoped_ptr.hpp>
19 #include <boost/shared_ptr.hpp>
20 #include <boost/weak_ptr.hpp>
21 #include <boost/intrusive_ptr.hpp>
23 ///////////////////////////////////////////////////////////////////
25 { /////////////////////////////////////////////////////////////////
27 /** \defgroup ZYPP_SMART_PTR Smart pointer types
28 * Smart pointer types.
30 * Namespace zypp provides 3 smart pointer types \b using the
31 * boost smart pointer library.
33 * \li \c scoped_ptr Simple sole ownership of single objects. Noncopyable.
35 * \li \c shared_ptr Object ownership shared among multiple pointers
37 * \li \c weak_ptr Non-owning observers of an object owned by shared_ptr.
39 * And \ref zypp::RW_pointer, as wrapper around a smart pointer,
40 * poviding \c const correct read/write access to the object it refers.
45 using boost::scoped_ptr;
48 using boost::shared_ptr;
51 using boost::weak_ptr;
53 /** Use boost::intrusive_ptr as Ptr type*/
54 using boost::intrusive_ptr;
56 using boost::static_pointer_cast;
57 using boost::const_pointer_cast;
58 using boost::dynamic_pointer_cast;
60 ///////////////////////////////////////////////////////////////////
62 // CLASS NAME : RW_pointer
64 /** Wrapper for \c const correct access via \ref ZYPP_SMART_PTR.
66 * zypp::RW_pointer<tt>\<_D,_Ptr></tt> stores a \ref ZYPP_SMART_PTR
67 * of type \c _Ptr, which must be convertible into a <tt>_D *</tt>. Pointer
68 * style access (via \c -> and \c *) offers a <tt>const _D *</tt> in const
69 * a context, otherwise a <tt>_D *</tt>. Thus \em RW_ means \em read/write,
70 * as you get a different type, dependent on whether you're allowed to
73 * Forwarding access from an interface to an implemantation class, an
74 * RW_pointer prevents const interface methods from accidentally calling
75 * nonconst implementation methods. In case you have to do so, call
76 * unconst to get the <tt>_D *</tt>.
78 * The second template argument defaults to <tt>_Ptr = shared_ptr<_D></tt>.
80 * #include "zypp/base/PtrTypes.h"
86 * // Implementation class
88 * // Pointer to implementation; actually a shared_ptr<Impl>
89 * RW_pointer<Impl> _pimpl;
91 * void baa() { _pimpl->... } // is Impl *
92 * void baa() const { _pimpl->... } // is Impl const *
95 * \todo refine ctor and assign.
97 template<class _D, class _Ptr = shared_ptr<_D> >
100 typedef _D element_type;
103 RW_pointer( typename _Ptr::element_type * dptr = 0 )
108 RW_pointer( _Ptr dptr )
112 _D & operator*() { return *_dptr; }
113 const _D & operator*() const { return *_dptr; };
114 _D * operator->() { return _dptr.get(); }
115 const _D * operator->() const { return _dptr.get(); }
116 _D * get() { return _dptr.get(); }
117 const _D * get() const { return _dptr.get(); }
119 _D * unconst() const { return _dptr.get(); }
124 /** \relates RW_pointer Stream output.
126 * Print the \c _D object the RW_pointer refers, or \c "NULL"
127 * if the pointer is \c NULL.
129 template<class _D, class _Ptr>
130 inline std::ostream &
131 operator<<( std::ostream & str, const RW_pointer<_D, _Ptr> & obj )
134 return str << *obj.get();
135 return str << std::string("NULL");
138 ///////////////////////////////////////////////////////////////////
139 /** Wrapper for \c const correct access via pointer.
141 * Template specialization of RW_pointer, storing a raw <tt>_P *</tt>,
142 * which must be convertible into a <tt>_D *</tt>.
144 * \note The object pointed to will \b not be deleted. If you need
145 * automatic cleanup, use a \ref ZYPP_SMART_PTR instead of a
148 template<class _D,class _P>
149 struct RW_pointer<_D,_P*>
151 typedef _D element_type;
154 RW_pointer( _P * dptr = 0 )
158 _D & operator*() { return *_dptr; }
159 const _D & operator*() const { return *_dptr; };
160 _D * operator->() { return _dptr; }
161 const _D * operator->() const { return _dptr; }
162 _D * get() { return _dptr; }
163 const _D * get() const { return _dptr; }
165 _D * unconst() const { return _dptr; }
170 /////////////////////////////////////////////////////////////////
174 /////////////////////////////////////////////////////////////////
176 ///////////////////////////////////////////////////////////////////
178 /** Forward declaration of Ptr types */
179 #define DEFINE_PTR_TYPE(NAME) \
181 extern void intrusive_ptr_add_ref( const NAME * ); \
182 extern void intrusive_ptr_release( const NAME * ); \
183 typedef zypp::intrusive_ptr<NAME> NAME##_Ptr; \
184 typedef zypp::intrusive_ptr<const NAME> NAME##_constPtr;
186 ///////////////////////////////////////////////////////////////////
187 #endif // ZYPP_BASE_PTRTYPES_H