Moved the smart pointer types and related cast functions (PtrTypes.h)
[platform/upstream/libzypp.git] / zypp / Resolvable.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/Resolvable.h
10  *
11 */
12 #ifndef ZYPP_RESOLVABLE_H
13 #define ZYPP_RESOLVABLE_H
14
15 #include <iosfwd>
16 #include <string>
17
18 #include "zypp/base/ReferenceCounted.h"
19 #include "zypp/base/NonCopyable.h"
20 #include "zypp/base/PtrTypes.h"
21 #include "zypp/ResTraits.h"
22
23 #include "zypp/Edition.h"
24 #include "zypp/Arch.h"
25 #include "zypp/Dependencies.h"
26
27 ///////////////////////////////////////////////////////////////////
28 namespace zypp
29 { /////////////////////////////////////////////////////////////////
30
31   ///////////////////////////////////////////////////////////////////
32   //
33   //    CLASS NAME : Resolvable
34   //
35   /** Interface base for resolvable objects (identification and dependencies).
36   */
37   class Resolvable : public base::ReferenceCounted, private base::NonCopyable
38   {
39   public:
40     typedef Resolvable               Self;
41     typedef ResTraits<Self>          TraitsType;
42     typedef TraitsType::KindType     Kind;
43     typedef TraitsType::PtrType      Ptr;
44     typedef TraitsType::constPtrType constPtr;
45     friend std::ostream & operator<<( std::ostream & str, const Resolvable & obj );
46
47   public:
48     /**  */
49     const Kind & kind() const;
50     /**  */
51     const std::string & name() const;
52     /**  */
53     const Edition & edition() const;
54     /**  */
55     const Arch & arch() const;
56     /**  */
57     const Dependencies & deps() const;
58     /** */
59     void setDeps( const Dependencies & val_r );
60
61   protected:
62     /** Ctor */
63     Resolvable( const Kind & kind_r,
64                 const std::string & name_r,
65                 const Edition & edition_r,
66                 const Arch & arch_r );
67     /** Dtor */
68     virtual ~Resolvable();
69     /** Helper for stream output */
70     virtual std::ostream & dumpOn( std::ostream & str ) const;
71
72   private:
73     /** Implementation */
74     struct Impl;
75     /** Pointer to implementation */
76     RW_pointer<Impl> _pimpl;
77   };
78   ///////////////////////////////////////////////////////////////////
79
80   /** Test whether a Resolvable::Ptr is of a certain Kind.
81    * \return \c Ture iff \a p is not \c NULL and points to a Resolvable
82    * of the specified Kind.
83    * \relates Resolvable
84    * \code
85    * isKind<Package>(resPtr);
86    * \endcode
87   */
88   template<class _Res>
89     inline bool isKind( const Resolvable::constPtr & p )
90     { return p && p->kind() == ResTraits<_Res>::kind; }
91
92   // Specialization for Resolvable: Always true.
93   template<>
94     inline bool isKind<Resolvable>( const Resolvable::constPtr & p )
95     { return p; }
96
97   // Specialization for ResObject: Always true.
98   template<>
99     inline bool isKind<ResObject>( const Resolvable::constPtr & p )
100     { return p; }
101
102
103   /** Convert Resolvable::Ptr into Ptr of a certain Kind.
104    * \return \c NULL iff \a p is \c NULL or points to a Resolvable
105    * not of the specified Kind.
106    * \relates Resolvable
107    * \code
108    * asKind<Package>(resPtr);
109    * \endcode
110   */
111   template<class _Res>
112     inline typename ResTraits<_Res>::PtrType asKind( const Resolvable::Ptr & p )
113     { return dynamic_pointer_cast<_Res>(p); }
114
115   template<class _Res>
116     inline typename ResTraits<_Res>::constPtrType asKind( const Resolvable::constPtr & p )
117     { return dynamic_pointer_cast<const _Res>(p); }
118
119   ///////////////////////////////////////////////////////////////////
120
121   /** Required by intrusive_ptr to add a reference. */
122   inline void intrusive_ptr_add_ref( const Resolvable * ptr_r )
123   { base::ReferenceCounted::add_ref( ptr_r ); }
124
125   /** Required by intrusive_ptr to release a reference. */
126   inline void intrusive_ptr_release( const Resolvable * ptr_r )
127   { base::ReferenceCounted::release( ptr_r ); }
128
129   /** \relates Resolvable Stream output via Resolvable::dumpOn */
130   inline std::ostream & operator<<( std::ostream & str, const Resolvable & obj )
131   { return obj.dumpOn( str ); }
132
133   /////////////////////////////////////////////////////////////////
134 } // namespace zypp
135 ///////////////////////////////////////////////////////////////////
136 #endif // ZYPP_RESOLVABLE_H