d9f984dbb82533217bee1388a6bfc4f23fdc856e
[platform/upstream/libzypp.git] / zypp / LanguageCode.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/LanguageCode.h
10  *
11 */
12 #ifndef ZYPP_LANGUAGECODE_H
13 #define ZYPP_LANGUAGECODE_H
14
15 #include <iosfwd>
16 #include <string>
17
18 #include "zypp/base/PtrTypes.h"
19
20 ///////////////////////////////////////////////////////////////////
21 namespace zypp
22 { /////////////////////////////////////////////////////////////////
23
24   class LanguageCode;
25   inline bool operator==( const LanguageCode & lhs, const LanguageCode & rhs );
26   inline bool operator!=( const LanguageCode & lhs, const LanguageCode & rhs );
27
28   ///////////////////////////////////////////////////////////////////
29   //
30   //    CLASS NAME : LanguageCode
31   //
32   /** Language codes (iso639_2/iso639_1).
33    *
34    * In fact the class will not prevent to use a non iso language code.
35    * Just a warning will appear in the log.
36   */
37   class LanguageCode
38   {
39     friend std::ostream & operator<<( std::ostream & str, const LanguageCode & obj );
40
41   public:
42     /** Implementation  */
43     class Impl;
44
45   public:
46     /** Default ctor */
47     LanguageCode();
48
49     /** Ctor taking a string. */
50     explicit
51     LanguageCode( const std::string & code_r );
52
53     /** Dtor */
54     ~LanguageCode();
55
56   public:
57     /** \name LanguageCode constants. */
58     //@{
59     /** No or empty code. */
60     static const LanguageCode noCode;
61     //@}
62
63   public:
64     /** Return the language code. */
65     std::string code() const;
66
67     /** Return the language name; if not available the language code. */
68     std::string name() const;
69
70     /** <tt>*this != noCode</tt>. */
71     inline bool hasCode() const
72     { return *this != noCode; }
73
74   private:
75     /** Pointer to implementation */
76     RW_pointer<Impl> _pimpl;
77   };
78   ///////////////////////////////////////////////////////////////////
79
80   /** \relates LanguageCode Stream output */
81   inline std::ostream & operator<<( std::ostream & str, const LanguageCode & obj )
82   { return str << obj.code(); }
83
84   /** Comparison based on string value. */
85   //@{
86   /** \relates LanguageCode */
87   inline bool operator==( const LanguageCode & lhs, const LanguageCode & rhs ) {
88     return( lhs.code() == rhs.code() );
89   }
90   /** \relates LanguageCode */
91   inline bool operator==( const std::string & lhs, const LanguageCode & rhs ) {
92     return( lhs == rhs.code() );
93   }
94   /** \relates LanguageCode */
95   inline bool operator==( const LanguageCode & lhs, const std::string & rhs ) {
96     return( lhs.code() == rhs );
97   }
98
99   /** \relates LanguageCode */
100   inline bool operator!=( const LanguageCode & lhs, const LanguageCode & rhs ) {
101     return( ! operator==( lhs, rhs ) );
102   }
103   /** \relates LanguageCode */
104   inline bool operator!=( const std::string & lhs, const LanguageCode & rhs ) {
105     return( ! operator==( lhs, rhs ) );
106   }
107   /** \relates LanguageCode */
108   inline bool operator!=( const LanguageCode & lhs, const std::string & rhs ) {
109     return( ! operator==( lhs, rhs ) );
110   }
111   //@}
112
113   /////////////////////////////////////////////////////////////////
114 } // namespace zypp
115 ///////////////////////////////////////////////////////////////////
116
117 ///////////////////////////////////////////////////////////////////
118 namespace std
119 { /////////////////////////////////////////////////////////////////
120   /** \relates zypp::LanguageCode Default order for std::container based on code string value.*/
121   template<>
122     inline bool less<zypp::LanguageCode>::operator()( const zypp::LanguageCode & lhs, const zypp::LanguageCode & rhs ) const
123     { return lhs.code() < rhs.code(); }
124   /////////////////////////////////////////////////////////////////
125 } // namespace std
126 ///////////////////////////////////////////////////////////////////
127 #endif // ZYPP_LANGUAGECODE_H