provide minimal scoring on architectures
[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 <string>
18
19 ///////////////////////////////////////////////////////////////////
20 namespace zypp
21 { /////////////////////////////////////////////////////////////////
22
23   ///////////////////////////////////////////////////////////////////
24   //
25   //    CLASS NAME : Arch
26   //
27   /** Architecture.
28    * \todo improve compatibleWith implementation
29    * \todo unify strings and optimize opertor==
30   */
31   class Arch
32   {
33   public:
34     /** Default ctor 'noarch' */
35     Arch();
36     /** Ctor from string. */
37     explicit
38     Arch( const std::string & rhs );
39     /** Dtor */
40     ~Arch()
41     {}
42     /** String representation of Arch. */
43     const std::string & asString() const
44     { return _value; }
45
46   public:
47     /** Compatibility relation.
48      * \return \c True iff \c this is compatible with \a rhs.
49     */
50     bool compatibleWith( const Arch & rhs ) const;
51
52     /** Order on Arch (arbitrary).
53      * \todo Adjust logical operators below to follow compare.
54     */
55     int compare( const Arch & rhs ) const
56     { return (_score < rhs._score) ? -1 : ((_score > rhs._score ) ? 1 : 0); }
57
58   private:
59     /** String representation of Arch. */
60     std::string _value;
61     int _score;
62   };
63   ///////////////////////////////////////////////////////////////////
64
65   /** \name Builtin architecture constants.
66    *
67    * Defined outside Arch as e.g. \c Arch_i386, because some names,
68    * like \c i388, are used as \c #define, thus unusable as identifier
69    * like \c Arch::i386.
70   */
71   //@{
72   /** \relates Arch */
73   extern const Arch Arch_noarch;
74   extern const Arch Arch_src;
75
76   /** \relates Arch */
77   extern const Arch Arch_x86_64;
78   /** \relates Arch */
79   extern const Arch Arch_athlon;
80   /** \relates Arch */
81   extern const Arch Arch_i686;
82   /** \relates Arch */
83   extern const Arch Arch_i586;
84   /** \relates Arch */
85   extern const Arch Arch_i486;
86   /** \relates Arch */
87   extern const Arch Arch_i386;
88
89   /** \relates Arch */
90   extern const Arch Arch_s390x;
91   /** \relates Arch */
92   extern const Arch Arch_s390;
93
94   /** \relates Arch */
95   extern const Arch Arch_ppc64;
96   /** \relates Arch */
97   extern const Arch Arch_ppc;
98
99   /** \relates Arch */
100   extern const Arch Arch_ia64;
101   //@}
102
103   ///////////////////////////////////////////////////////////////////
104
105   inline Arch::Arch()
106   : _value( Arch_noarch._value )
107   {}
108
109   ///////////////////////////////////////////////////////////////////
110
111   /** \relates Arch stream output. */
112   inline std::ostream & operator<<( std::ostream & str, const Arch & obj )
113   { return str << obj.asString(); }
114
115   /** \name Comparison based on string value. */
116   //@{
117   /** \relates Arch */
118   inline bool operator==( const Arch & lhs, const Arch & rhs )
119   { return lhs.asString() == rhs.asString(); }
120
121   /** \relates Arch */
122   inline bool operator==( const Arch & lhs, const std::string & rhs )
123   { return lhs.asString() == rhs; }
124
125   /** \relates Arch */
126   inline bool operator==( const std::string & lhs, const Arch & rhs )
127   { return lhs == rhs.asString(); }
128
129   /** \relates Arch */
130   inline bool operator!=( const Arch & lhs, const Arch & rhs )
131   { return !( lhs == rhs ); }
132
133   /** \relates Arch */
134   inline bool operator!=( const Arch & lhs, const std::string & rhs )
135   { return !( lhs == rhs ); }
136
137   /** \relates Arch */
138   inline bool operator!=( const std::string & lhs, const Arch & rhs )
139   { return !( lhs == rhs ); }
140   //@}
141
142   ///////////////////////////////////////////////////////////////////
143
144   /** Functor finding compatible architectures.
145    * \see Arch::compatibleWith
146   */
147   struct ArchCompatibleWith : public std::unary_function<Arch,bool>
148   {
149     /** The target architecture */
150     Arch _targetArch;
151     /** Ctor taking the target architecture */
152     ArchCompatibleWith( const Arch & targetArch_r )
153     : _targetArch( targetArch_r )
154     {}
155     /** Call Arch::compatibleWith ( \c _targetArch ) on \a rhs. */
156     bool operator()( const Arch & rhs ) const
157     { return rhs.compatibleWith( _targetArch ); }
158   };
159
160   /////////////////////////////////////////////////////////////////
161 } // namespace zypp
162 ///////////////////////////////////////////////////////////////////
163
164 ///////////////////////////////////////////////////////////////////
165 namespace std
166 { /////////////////////////////////////////////////////////////////
167   /** \relates Arch Default order for std::container based on string value.*/
168   template<>
169     inline bool less<zypp::Arch>::operator()( const zypp::Arch & lhs, const zypp::Arch & rhs ) const
170     { return lhs.asString() < rhs.asString(); }
171   /////////////////////////////////////////////////////////////////
172 } // namespace zypp
173 ///////////////////////////////////////////////////////////////////
174 #endif // ZYPP_ARCH_H