Added comparison operators to Edition interfacae.
[platform/upstream/libzypp.git] / zypp / base / PtrTypes.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/base/PtrTypes.h
10  *  \ingroup ZYPP_BASE_SMART_PTR
11  *  \see ZYPP_BASE_SMART_PTR
12 */
13 #ifndef ZYPP_BASE_PTRTYPES_H
14 #define ZYPP_BASE_PTRTYPES_H
15
16 #include <boost/scoped_ptr.hpp>
17 #include <boost/shared_ptr.hpp>
18 #include <boost/weak_ptr.hpp>
19 #include <boost/intrusive_ptr.hpp>
20
21 ///////////////////////////////////////////////////////////////////
22 namespace zypp
23 { /////////////////////////////////////////////////////////////////
24   ///////////////////////////////////////////////////////////////////
25   namespace base
26   { /////////////////////////////////////////////////////////////////
27
28     /** \defgroup ZYPP_BASE_SMART_PTR ZYPP_BASE_SMART_PTR
29      *  Smart pointer types.
30      *
31      * Namespace zypp::base provides 3 smart pointer types \b using the
32      * boost smart pointer library.
33      *
34      * \li \c scoped_ptr Simple sole ownership of single objects. Noncopyable.
35      *
36      * \li \c shared_ptr Object ownership shared among multiple pointers
37      *
38      * \li \c weak_ptr Non-owning observers of an object owned by shared_ptr.
39     */
40     /*@{*/
41
42     /** */
43     using boost::scoped_ptr;
44
45     /** */
46     using boost::shared_ptr;
47
48     /** */
49     using boost::weak_ptr;
50
51     /** Use boost::intrusive_ptr as Ptr type*/
52     using boost::intrusive_ptr;
53     using boost::static_pointer_cast;
54     using boost::const_pointer_cast;
55     using boost::dynamic_pointer_cast;
56
57     ///////////////////////////////////////////////////////////////////
58     //
59     //  CLASS NAME : ImplPtr
60     //
61     /** Wrapper for \c const correct access via \ref ZYPP_BASE_SMART_PTR.
62      *
63      * zypp::base::ImplPtr<tt>\<_D,_Ptr></tt> stores a \ref ZYPP_BASE_SMART_PTR
64      * of type \c _Ptr, which must be convertible into a <tt>_D *</tt>. Pointer
65      * style access (via \c -> and \c *) offers a <tt>const _D *</tt> in const
66      * a context, otherwise a <tt>_D *</tt>.
67      *
68      * Forwarding access from an interface to an implemantation class, an
69      * ImplPtr prevents const interface methods from accidentally calling
70      * nonconst implementation methods. In case you have to do so, call
71      * unconst to get the <tt>_D *</tt>.
72      *
73      * The second template argument defaults to <tt>_Ptr = shared_ptr<_D></tt>.
74      *
75      * \todo refine ctor and assign.
76     */
77     template<class _D, class _Ptr = shared_ptr<_D> >
78       struct ImplPtr
79       {
80         typedef _D element_type;
81
82         explicit
83         ImplPtr( typename _Ptr::element_type * dptr = 0 )
84         : _dptr( dptr )
85         {}
86
87         explicit
88         ImplPtr( _Ptr dptr )
89         : _dptr( dptr )
90         {}
91
92         _D & operator*() { return *_dptr; }
93         const _D & operator*() const { return *_dptr; };
94         _D * operator->() { return _dptr.get(); }
95         const _D * operator->() const { return _dptr.get(); }
96         _D * get() { return _dptr.get(); }
97         const _D * get() const { return _dptr.get(); }
98
99         _D * unconst() const { return _dptr.get(); }
100
101
102         _Ptr _dptr;
103       };
104     ///////////////////////////////////////////////////////////////////
105     /** Wrapper for \c const correct access via pointer.
106      *
107      * Template specialization of ImplPtr, storing a raw <tt>_P *</tt>,
108      * which must be convertible into a <tt>_D *</tt>.
109      *
110      * \note The object pointed to will \b not be deleted. If you need
111      * automatic cleanup, use a \ref ZYPP_BASE_SMART_PTR instead of a
112      * raw pointer.
113     */
114     template<class _D,class _P>
115       struct ImplPtr<_D,_P*>
116       {
117         typedef _D element_type;
118
119         explicit
120         ImplPtr( _P * dptr = 0 )
121         : _dptr( dptr )
122         {}
123
124         _D & operator*() { return *_dptr; }
125         const _D & operator*() const { return *_dptr; };
126         _D * operator->() { return _dptr; }
127         const _D * operator->() const { return _dptr; }
128         _D * get() { return _dptr; }
129         const _D * get() const { return _dptr; }
130
131         _D * unconst() const { return _dptr; }
132
133
134         _P * _dptr;
135       };
136     /////////////////////////////////////////////////////////////////
137
138     /*@}*/
139
140     /////////////////////////////////////////////////////////////////
141   } // namespace base
142   ///////////////////////////////////////////////////////////////////
143   /////////////////////////////////////////////////////////////////
144 } // namespace zypp
145 ///////////////////////////////////////////////////////////////////
146
147 /** Forward declaration of Ptr types */
148 #define DEFINE_PTR_TYPE(NAME) \
149 class NAME;                                                      \
150 extern void intrusive_ptr_add_ref( const NAME * );               \
151 extern void intrusive_ptr_release( const NAME * );               \
152 typedef zypp::base::intrusive_ptr<NAME>       NAME##Ptr;         \
153 typedef zypp::base::intrusive_ptr<const NAME> const##NAME##Ptr;  \
154 typedef zypp::base::intrusive_ptr<NAME>       NAME##_Ptr;        \
155 typedef zypp::base::intrusive_ptr<const NAME> NAME##_constPtr;
156
157 ///////////////////////////////////////////////////////////////////
158 #endif // ZYPP_BASE_PTRTYPES_H