add basic locale interface to ResPool, remove deprecated _gxx hashes
[platform/upstream/libzypp.git] / zypp / Locale.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/Locale.h
10  *
11 */
12 #ifndef ZYPP_LOCALE_H
13 #define ZYPP_LOCALE_H
14
15 #include <iosfwd>
16 #include <tr1/unordered_set>
17
18 #include "zypp/base/PtrTypes.h"
19
20 #include "zypp/IdString.h"
21 #include "zypp/LanguageCode.h"
22 #include "zypp/CountryCode.h"
23
24 ///////////////////////////////////////////////////////////////////
25 namespace zypp
26 { /////////////////////////////////////////////////////////////////
27
28   ///////////////////////////////////////////////////////////////////
29   //
30   //    CLASS NAME : Locale
31   //
32   /**
33    * \todo migrate to IdString
34   */
35   class Locale
36   {
37     friend std::ostream & operator<<( std::ostream & str, const Locale & obj );
38
39   public:
40     /** Implementation  */
41     class Impl;
42
43   public:
44     /** Default ctor */
45     Locale();
46
47     /** Ctor taking a string. */
48     explicit
49     Locale( IdString code_r );
50
51     explicit
52     Locale( const std::string & code_r );
53
54     explicit
55     Locale( const char * code_r );
56
57     /** Ctor taking LanguageCode and optional CountryCode. */
58     Locale( const LanguageCode & language_r,
59             const CountryCode & country_r = CountryCode() );
60
61     /** Dtor */
62     ~Locale();
63
64   public:
65     /** \name Locale constants. */
66     //@{
67     /** No or empty code. */
68     static const Locale noCode;
69     //@}
70
71   public:
72     /** */
73     const LanguageCode & language() const;
74     /** */
75     const CountryCode & country() const;
76
77     /** Return the locale code. */
78     std::string code() const;
79
80     /** Return the name made of language and country name. */
81     std::string name() const;
82
83     /** Return a fallback locale for this locale, when giving up, returns empty Locale() */
84     Locale fallback() const;
85
86   private:
87     /** Pointer to implementation */
88     RW_pointer<Impl> _pimpl;
89   };
90   ///////////////////////////////////////////////////////////////////
91
92   /** \relates Locale Stream output */
93   inline std::ostream & operator<<( std::ostream & str, const Locale & obj )
94   { return str << obj.code(); }
95
96   /** Comparison based on string value. */
97   //@{
98   /** \relates Locale */
99   inline bool operator==( const Locale & lhs, const Locale & rhs ) {
100     return( lhs.code() == rhs.code() );
101   }
102   /** \relates Locale */
103   inline bool operator==( const std::string & lhs, const Locale & rhs ) {
104     return( lhs == rhs.code() );
105   }
106   /** \relates Locale */
107   inline bool operator==( const Locale & lhs, const std::string & rhs ) {
108     return( lhs.code() == rhs );
109   }
110
111   /** \relates Locale */
112   inline bool operator!=( const Locale & lhs, const Locale & rhs ) {
113     return( ! operator==( lhs, rhs ) );
114   }
115   /** \relates Locale */
116   inline bool operator!=( const std::string & lhs, const Locale & rhs ) {
117     return( ! operator==( lhs, rhs ) );
118   }
119   /** \relates Locale */
120   inline bool operator!=( const Locale & lhs, const std::string & rhs ) {
121     return( ! operator==( lhs, rhs ) );
122   }
123   //@}
124
125   ///////////////////////////////////////////////////////////////////
126
127   typedef std::tr1::unordered_set<Locale> LocaleSet;
128
129   /////////////////////////////////////////////////////////////////
130 } // namespace zypp
131 ///////////////////////////////////////////////////////////////////
132
133 namespace std { namespace tr1 {
134   /** \relates ::zypp::Locale hash function */
135   template<> struct hash< ::zypp::Locale>
136   {
137     size_t operator()( const ::zypp::Locale & __s ) const
138     { return hash<std::string>()(__s.code()); }
139   };
140 }}
141
142 ///////////////////////////////////////////////////////////////////
143 namespace std
144 { /////////////////////////////////////////////////////////////////
145   /** \relates zypp::Locale Default order for std::container based on code string value.*/
146   template<>
147     inline bool less<zypp::Locale>::operator()( const zypp::Locale & lhs, const zypp::Locale & rhs ) const
148     { return lhs.code() < rhs.code(); }
149   /////////////////////////////////////////////////////////////////
150 } // namespace std
151 ///////////////////////////////////////////////////////////////////
152 #endif // ZYPP_LOCALE_H