- Added helper struct NVR (name,edition(version,relase))
[platform/upstream/libzypp.git] / zypp / Capability.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/Capability.h
10  *
11 */
12 #ifndef ZYPP_CAPABILITY_H
13 #define ZYPP_CAPABILITY_H
14
15 #include <iosfwd>
16 #include <functional>
17
18 #include "zypp/base/PtrTypes.h"
19
20 #include "zypp/Resolvable.h"
21 #include "zypp/CapMatch.h"
22
23 ///////////////////////////////////////////////////////////////////
24 namespace zypp
25 { /////////////////////////////////////////////////////////////////
26   ///////////////////////////////////////////////////////////////////
27   namespace capability
28   { /////////////////////////////////////////////////////////////////
29     DEFINE_PTR_TYPE(CapabilityImpl);
30     /////////////////////////////////////////////////////////////////
31   } // namespace capability
32   ///////////////////////////////////////////////////////////////////
33
34   class CapFactory;
35
36   ///////////////////////////////////////////////////////////////////
37   //
38   //    CLASS NAME : Capability
39   //
40   /** Resolvable capabilitiy.
41    *
42    * Capability is created by a Factory class. Only a default ctor
43    * creating a dummy capability is provided.
44    * \code
45    *   Capability cap;
46    *   try
47    *     {
48    *       cap = CapFactory().parse( ResTraits<Patch>::kind,
49    *                                 parsed.name,
50    *                                 parsed.op,
51    *                                 Edition( parsed.ver,
52    *                                          parsed.rel,
53    *                                          parsed.epoch ) );
54    *     }
55    *   catch ( const Exception & excpt_r )
56    *     {
57    *       ERR << excpt_r << endl;
58    *       ... Or maybe just WAR, or ?
59    *     }
60    * \endcode
61    * \see CapFactory: Factory creating Capability.
62    *
63    * \invariant Nonzero \c _pimpl
64    * \invariant Unified \c _pimpl asserted by CapFactory.
65    *
66    * \todo Need a trival return from matches. E.g. Conditional
67    * cpabilities must be able to indicate that they should be
68    * treated as if they were not present at all, if the precondition
69    * does no apply. Same for the defaut Capability.
70   */
71   class Capability
72   {
73     /** Factory */
74     friend class CapFactory;
75
76     /** Ordering for use in CapSet */
77     friend class CapOrder;
78     friend bool operator==( const Capability & lhs, const Capability & rhs );
79     friend std::ostream & operator<<( std::ostream & str, const Capability & obj );
80
81   private:
82     typedef capability::CapabilityImpl          Impl;
83     typedef capability::CapabilityImpl_Ptr      Impl_Ptr ;
84     typedef capability::CapabilityImpl_constPtr Impl_constPtr;
85
86     /** Factory ctor */
87     explicit
88     Capability( Impl_Ptr impl_r );
89
90   public:
91     /** Factory */
92     typedef CapFactory Factory;
93
94     /** DefaultCtor creating a dummy Capability. */
95     Capability();
96
97     /** Dtor */
98     virtual ~Capability();
99
100   public:
101     /** Kind of Resolvable the Capability refers to. */
102     const Resolvable::Kind & refers() const;
103
104     /** Whether to consider this Capability.
105      * Evaluates the Capabilities pre-condition (if any), and
106      * returns whether the condition applies. If not, the Capability
107      * is to be ignored.
108     */
109     bool relevant() const;
110
111     /** Return whether the Capabilities match.
112      * If either Capability is not \ref relevant, CapMatch::irrelevant
113      * is returned.
114     */
115     CapMatch matches( const Capability & rhs ) const;
116
117     /** More or less human readable representation as string. */
118     std::string asString() const;
119
120     /** Deprecated */
121     std::string index() const;
122
123   private:
124     /** Pointer to implementation */
125     RW_pointer<Impl,rw_pointer::Intrusive<Impl> > _pimpl;
126   };
127   ///////////////////////////////////////////////////////////////////
128
129   /** Ordering relation used by ::CapSet. */
130   struct CapOrder : public std::binary_function<Capability, Capability, bool>
131   {
132     bool operator()( const Capability & lhs, const Capability & rhs ) const
133     { return lhs._pimpl.get() < rhs._pimpl.get(); }
134   };
135
136   ///////////////////////////////////////////////////////////////////
137
138   /** \relates Capability  */
139   inline bool operator==( const Capability & lhs, const Capability & rhs )
140   { return lhs._pimpl.get() == rhs._pimpl.get(); }
141
142   /** \relates Capability  */
143   inline bool operator!=( const Capability & lhs, const Capability & rhs )
144   { return ! (lhs == rhs); }
145
146   /** \relates Capability Stream output */
147   extern std::ostream & operator<<( std::ostream & str, const Capability & obj );
148
149   /////////////////////////////////////////////////////////////////
150 } // namespace zypp
151 ///////////////////////////////////////////////////////////////////
152 #endif // ZYPP_CAPABILITY_H