Imported Upstream version 16.3.2
[platform/upstream/libzypp.git] / zypp / base / EnumClass.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/base/EnumClass.h
10  */
11 #ifndef ZYPP_BASE_ENUMCLASS_H
12 #define ZYPP_BASE_ENUMCLASS_H
13
14 #include <iosfwd>
15
16 #include "zypp/base/PtrTypes.h"
17
18 ///////////////////////////////////////////////////////////////////
19 namespace zypp
20 {
21   ///////////////////////////////////////////////////////////////////
22   namespace base
23   {
24     ///////////////////////////////////////////////////////////////////
25     /// \class EnumClass
26     /// \brief Type safe enum (workaround SWIG not supporting enum class)
27     /// \code
28     /// struct EColorDef { enum Enum { R, G ,B }; };
29     /// typedef EnumClass<EColorDef> Color;
30     /// \endcode
31     /// Conversion to from string can be easily added, e.g. like this:
32     /// \code
33     /// struct EColorDef {
34     ///   enum Enum { R, G ,B };
35     ///   static Enum fromString( const std::string & val_r );
36     ///   static const std::string & asString( Enum val_r );
37     /// };
38     /// std::ostream & operator<<( std::ostream & str, const EColorDef & obj )
39     /// { return str << EColorDef::asString( obj.inSwitch() ); }
40     ///
41     /// typedef EnumClass<EColorDef> Color;
42     /// Color red = Color::fromString("red");
43     /// cout << red << endl; // "red"
44     /// \endcode
45     ///////////////////////////////////////////////////////////////////
46     template<typename TEnumDef>
47     class EnumClass : public TEnumDef
48     {
49     public:
50       typedef typename TEnumDef::Enum Enum;             ///< The underlying enum type
51       typedef typename std::underlying_type<Enum>::type Integral;///< The underlying integral type
52
53       EnumClass( Enum val_r ) : _val( val_r ) {}
54
55       /** Underlying enum value for use in switch
56        * \code
57        * struct EColorDef { enum Enum { R, G ,B }; }
58        * typedef EnumClass<EColorDef> Color;
59        *
60        * Color a;
61        * switch ( a.asEnum() )
62        * \endcode
63        */
64       Enum asEnum() const { return _val; }
65
66       /** Underlying integral value (e.g. array index)
67        * \code
68        * struct EColorDef { enum Enum { R, G ,B }; }
69        * typedef EnumClass<EColorDef> Color;
70        *
71        * Color a;
72        * std::string table[] = { "red", "green", "blue" };
73        * std::cout << table[a.asIntegral()] << std::endl;
74        */
75       Integral asIntegral() const { return static_cast<Integral>(_val); }
76
77       friend bool operator==( const EnumClass & lhs, const EnumClass & rhs ) { return lhs._val == rhs._val; }
78       friend bool operator!=( const EnumClass & lhs, const EnumClass & rhs ) { return lhs._val != rhs._val; }
79       friend bool operator< ( const EnumClass & lhs, const EnumClass & rhs ) { return lhs._val <  rhs._val; }
80       friend bool operator<=( const EnumClass & lhs, const EnumClass & rhs ) { return lhs._val <= rhs._val; }
81       friend bool operator> ( const EnumClass & lhs, const EnumClass & rhs ) { return lhs._val >  rhs._val; }
82       friend bool operator>=( const EnumClass & lhs, const EnumClass & rhs ) { return lhs._val >= rhs._val; }
83
84     private:
85       Enum _val;
86     };
87   } // namespace base
88   ///////////////////////////////////////////////////////////////////
89 } // namespace zypp
90 ///////////////////////////////////////////////////////////////////
91 #endif // ZYPP_BASE_ENUMCLASS_H