- Arch:
[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     /** Compatibility relation.
48      * \return \c True iff \c this is compatible with \a targetArch_r.
49      * \code
50      * Arch_noarch.compatibleWith( ... )       ==> always true;
51      * Arch_i686.compatibleWith( Arch_x86_64 ) ==> true;
52      * Arch_x86_64.compatibleWith( Arch_i686 ) ==> false;
53      * \endcode
54     */
55     bool compatibleWith( const Arch & targetArch_r ) const;
56
57     /** Arch comparison.
58      * Primary key is the number of compatible Archs, then
59      * the string representation. Thus Arch_noarch is the
60      * least Arch.
61     */
62     int compare( const Arch & rhs ) const;
63
64     /** Arch comparison (static version). */
65     static int compare( const Arch & lhs, const Arch & rhs )
66     { return lhs.compare( rhs ); }
67
68   public:
69     /** Reversed arch order, best Arch first. */
70     typedef std::set<Arch,CompareByGT<Arch> > CompatSet;
71
72     /** Return a set of all Arch's \compatible with a targetArch_r.
73      * \note The set is ordered according to compare, thus iterating
74      * will start at Arch_noarch.
75      * \code
76      * Arch::CompatSet cset( Arch::compatSet( Arch_x86_64 ) );
77      *
78      * cout << str::join( make_transform_iterator( cset.begin(), std::mem_fun_ref(&Arch::asString) ),
79      *                    make_transform_iterator( cset.end(), std::mem_fun_ref(&Arch::asString) ) )
80      *      << endl;
81      *
82      * // Prints: x86_64 athlon i686 i586 i486 i386 noarch
83      * \endcode
84     */
85     static CompatSet compatSet( const Arch & targetArch_r );
86
87     /** */
88     static std::string asString( const CompatSet & cset )
89     {
90       return str::join( make_transform_iterator( cset.begin(), std::mem_fun_ref(&Arch::asString) ),
91                         make_transform_iterator( cset.end(), std::mem_fun_ref(&Arch::asString) ) );
92     }
93
94   public:
95     struct CompatEntry;
96   private:
97     Arch( const CompatEntry & );
98     const CompatEntry * _entry;
99   };
100   ///////////////////////////////////////////////////////////////////
101
102   /** \name Builtin architecture constants.
103    *
104    * Defined outside Arch as e.g. \c Arch_i386, because some names,
105    * like \c i388, are used as \c #define, thus unusable as identifier
106    * like \c Arch::i386.
107   */
108   //@{
109   /** \relates Arch */
110   extern const Arch Arch_noarch;
111   /** \todo actually not an Arch but kind of resolvable. */
112   extern const Arch Arch_src;
113
114   /** \relates Arch */
115   extern const Arch Arch_x86_64;
116   /** \relates Arch */
117   extern const Arch Arch_athlon;
118   /** \relates Arch */
119   extern const Arch Arch_i686;
120   /** \relates Arch */
121   extern const Arch Arch_i586;
122   /** \relates Arch */
123   extern const Arch Arch_i486;
124   /** \relates Arch */
125   extern const Arch Arch_i386;
126
127   /** \relates Arch */
128   extern const Arch Arch_s390x;
129   /** \relates Arch */
130   extern const Arch Arch_s390;
131
132   /** \relates Arch */
133   extern const Arch Arch_ppc64;
134   /** \relates Arch */
135   extern const Arch Arch_ppc;
136
137   /** \relates Arch */
138   extern const Arch Arch_ia64;
139   //@}
140
141   ///////////////////////////////////////////////////////////////////
142
143   /** \relates Arch stream output. */
144   inline std::ostream & operator<<( std::ostream & str, const Arch & obj )
145   { return str << obj.asString(); }
146
147   /** \name Equality based on string value. */
148   //@{
149   /** \relates Arch */
150   inline bool operator==( const Arch & lhs, const Arch & rhs )
151   { return lhs.asString() == rhs.asString(); }
152
153   /** \relates Arch */
154   inline bool operator==( const Arch & lhs, const std::string & rhs )
155   { return lhs.asString() == rhs; }
156
157   /** \relates Arch */
158   inline bool operator==( const std::string & lhs, const Arch & rhs )
159   { return lhs == rhs.asString(); }
160
161   /** \relates Arch */
162   inline bool operator!=( const Arch & lhs, const Arch & rhs )
163   { return !( lhs == rhs ); }
164
165   /** \relates Arch */
166   inline bool operator!=( const Arch & lhs, const std::string & rhs )
167   { return !( lhs == rhs ); }
168
169   /** \relates Arch */
170   inline bool operator!=( const std::string & lhs, const Arch & rhs )
171   { return !( lhs == rhs ); }
172   //@}
173
174   ///////////////////////////////////////////////////////////////////
175
176   /** Functor finding compatible architectures.
177    * \see Arch::compatibleWith
178   */
179   struct ArchCompatibleWith : public std::unary_function<Arch,bool>
180   {
181     /** The target architecture */
182     Arch _targetArch;
183     /** Ctor taking the target architecture */
184     ArchCompatibleWith( const Arch & targetArch_r )
185     : _targetArch( targetArch_r )
186     {}
187     /** Call Arch::compatibleWith ( \c _targetArch ) on \a rhs. */
188     bool operator()( const Arch & rhs ) const
189     { return rhs.compatibleWith( _targetArch ); }
190   };
191
192   /////////////////////////////////////////////////////////////////
193 } // namespace zypp
194 ///////////////////////////////////////////////////////////////////
195
196 ///////////////////////////////////////////////////////////////////
197 namespace std
198 { /////////////////////////////////////////////////////////////////
199   /** \relates Arch Default order for std::container based Arch::compare.*/
200   template<>
201     inline bool less<zypp::Arch>::operator()( const zypp::Arch & lhs, const zypp::Arch & rhs ) const
202     { return lhs.compare( rhs ) < 0; }
203   /////////////////////////////////////////////////////////////////
204 } // namespace zypp
205 ///////////////////////////////////////////////////////////////////
206 #endif // ZYPP_ARCH_H