- Create the cache directly from the schema (installed) file.
[platform/upstream/libzypp.git] / zypp / Arch.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/Arch.h
10  *
11 */
12 #ifndef ZYPP_ARCH_H
13 #define ZYPP_ARCH_H
14
15 #include <iosfwd>
16 #include <functional>
17 #include <set>
18 #include <string>
19
20 #include "zypp/RelCompare.h"
21 #include "zypp/base/String.h"
22 #include "zypp/base/Iterator.h"
23
24 ///////////////////////////////////////////////////////////////////
25 namespace zypp
26 { /////////////////////////////////////////////////////////////////
27
28   ///////////////////////////////////////////////////////////////////
29   //
30   //    CLASS NAME : Arch
31   //
32   /** Architecture.
33   */
34   class Arch
35   {
36   public:
37     /** Default ctor 'noarch' */
38     Arch();
39     /** Ctor from string. */
40     explicit
41     Arch( const std::string & rhs );
42
43   public:
44     /** String representation of Arch. */
45     const std::string & asString() const;
46
47     /** Test for an empty Arch (this is "", not Arch_noarch). */
48     bool empty() const
49     { return asString().empty(); }
50
51     /** Compatibility relation.
52      * \return \c True iff \c this is compatible with \a targetArch_r.
53      * \code
54      * Arch_noarch.compatibleWith( ... )       ==> always true;
55      * Arch_i686.compatibleWith( Arch_x86_64 ) ==> true;
56      * Arch_x86_64.compatibleWith( Arch_i686 ) ==> false;
57      * \endcode
58     */
59     bool compatibleWith( const Arch & targetArch_r ) const;
60
61     /** Arch comparison.
62      * Primary key is the number of compatible Archs, then
63      * the string representation. Thus Arch_noarch is the
64      * least Arch.
65     */
66     int compare( const Arch & rhs ) const;
67
68     /** Arch comparison (static version). */
69     static int compare( const Arch & lhs, const Arch & rhs )
70     { return lhs.compare( rhs ); }
71
72   public:
73     /** Reversed arch order, best Arch first. */
74     typedef std::set<Arch,CompareByGT<Arch> > CompatSet;
75
76     /** Return a set of all Arch's \ref compatibleWith a \a targetArch_r.
77      * \note The set is ordered according to compare, thus iterating
78      * will start at \a targetArch_r and end with \c Arch_noarch.
79      * \code
80      * Arch::CompatSet cset( Arch::compatSet( Arch_x86_64 ) );
81      *
82      * cout << str::join( make_transform_iterator( cset.begin(), std::mem_fun_ref(&Arch::asString) ),
83      *                    make_transform_iterator( cset.end(), std::mem_fun_ref(&Arch::asString) ) )
84      *      << endl;
85      *
86      * // Prints: x86_64 athlon i686 i586 i486 i386 noarch
87      * \endcode
88     */
89     static CompatSet compatSet( const Arch & targetArch_r );
90
91     /** */
92     static std::string asString( const CompatSet & cset )
93     {
94       return str::join( make_transform_iterator( cset.begin(), std::mem_fun_ref(&Arch::asString) ),
95                         make_transform_iterator( cset.end(), std::mem_fun_ref(&Arch::asString) ) );
96     }
97
98   public:
99     struct CompatEntry;
100   private:
101     Arch( const CompatEntry & );
102     const CompatEntry * _entry;
103   };
104   ///////////////////////////////////////////////////////////////////
105
106   /** \name Builtin architecture constants.
107    *
108    * Defined outside Arch as e.g. \c Arch_i386, because some names,
109    * like \c i388, are used as \c #define, thus unusable as identifier
110    * like \c Arch::i386.
111   */
112   //@{
113   /** \relates Arch */
114   extern const Arch Arch_noarch;
115
116   /** \relates Arch */
117   extern const Arch Arch_x86_64;
118   /** \relates Arch */
119   extern const Arch Arch_athlon;
120   /** \relates Arch */
121   extern const Arch Arch_i686;
122   /** \relates Arch */
123   extern const Arch Arch_i586;
124   /** \relates Arch */
125   extern const Arch Arch_i486;
126   /** \relates Arch */
127   extern const Arch Arch_i386;
128
129   /** \relates Arch */
130   extern const Arch Arch_s390x;
131   /** \relates Arch */
132   extern const Arch Arch_s390;
133
134   /** \relates Arch */
135   extern const Arch Arch_ppc64;
136   /** \relates Arch */
137   extern const Arch Arch_ppc;
138
139   /** \relates Arch */
140   extern const Arch Arch_ia64;
141   //@}
142
143   ///////////////////////////////////////////////////////////////////
144
145   /** \relates Arch stream output. */
146   inline std::ostream & operator<<( std::ostream & str, const Arch & obj )
147   { return str << obj.asString(); }
148
149   /** \name Equality based on string value. */
150   //@{
151   /** \relates Arch */
152   inline bool operator==( const Arch & lhs, const Arch & rhs )
153   { return lhs.asString() == rhs.asString(); }
154
155   /** \relates Arch */
156   inline bool operator==( const Arch & lhs, const std::string & rhs )
157   { return lhs.asString() == rhs; }
158
159   /** \relates Arch */
160   inline bool operator==( const std::string & lhs, const Arch & rhs )
161   { return lhs == rhs.asString(); }
162
163   /** \relates Arch */
164   inline bool operator!=( const Arch & lhs, const Arch & rhs )
165   { return !( lhs == rhs ); }
166
167   /** \relates Arch */
168   inline bool operator!=( const Arch & lhs, const std::string & rhs )
169   { return !( lhs == rhs ); }
170
171   /** \relates Arch */
172   inline bool operator!=( const std::string & lhs, const Arch & rhs )
173   { return !( lhs == rhs ); }
174   //@}
175
176   ///////////////////////////////////////////////////////////////////
177
178   /** Functor finding compatible architectures.
179    * \see Arch::compatibleWith
180   */
181   struct ArchCompatibleWith : public std::unary_function<Arch,bool>
182   {
183     /** The target architecture */
184     Arch _targetArch;
185     /** Ctor taking the target architecture */
186     ArchCompatibleWith( const Arch & targetArch_r )
187     : _targetArch( targetArch_r )
188     {}
189     /** Call Arch::compatibleWith ( \c _targetArch ) on \a rhs. */
190     bool operator()( const Arch & rhs ) const
191     { return rhs.compatibleWith( _targetArch ); }
192   };
193
194   /////////////////////////////////////////////////////////////////
195 } // namespace zypp
196 ///////////////////////////////////////////////////////////////////
197
198 ///////////////////////////////////////////////////////////////////
199 namespace std
200 { /////////////////////////////////////////////////////////////////
201   /** \relates zypp::Arch Default order for std::container based Arch::compare.*/
202   template<>
203     inline bool less<zypp::Arch>::operator()( const zypp::Arch & lhs, const zypp::Arch & rhs ) const
204     { return lhs.compare( rhs ) < 0; }
205   /////////////////////////////////////////////////////////////////
206 } // namespace zypp
207 ///////////////////////////////////////////////////////////////////
208 #endif // ZYPP_ARCH_H