libzypp  13.10.6
Flags.h
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
12 #ifndef ZYPP_BASE_FLAGS_H
13 #define ZYPP_BASE_FLAGS_H
14 
15 #include "zypp/base/String.h"
16 
18 namespace zypp
19 {
20  namespace base
22  {
23 
25  //
26  // CLASS NAME : Flags<Enum>
27  //
57  template<typename Enum>
58  class Flags
59  {
60  public:
61  typedef Enum enum_type;
62 
63  public:
64  Flags() : _val( 0 ) {}
65  Flags( Enum flag_r ) : _val( flag_r ) {}
66  explicit Flags( unsigned flag_r ) : _val( flag_r ) {}
67 
68  Flags & operator&=( Flags rhs ) { _val &= rhs._val; return *this; }
69  Flags & operator&=( Enum rhs ) { _val &= rhs; return *this; }
70 
71  Flags & operator|=( Flags rhs ) { _val |= rhs._val; return *this; }
72  Flags & operator|=( Enum rhs ) { _val |= rhs; return *this; }
73 
74  Flags & operator^=( Flags rhs ) { _val ^= rhs._val; return *this; }
75  Flags & operator^=( Enum rhs ) { _val ^= rhs; return *this; }
76 
77  public:
78  operator unsigned() const { return _val; }
79 
80  Flags operator&( Flags rhs ) const { return Flags( *this ) &= rhs; }
81  Flags operator&( Enum rhs ) const { return Flags( *this ) &= rhs; }
82 
83  Flags operator|( Flags rhs ) const { return Flags( *this ) |= rhs; }
84  Flags operator|( Enum rhs ) const { return Flags( *this ) |= rhs; }
85 
86  Flags operator^( Flags rhs ) const { return Flags( *this ) ^= rhs; }
87  Flags operator^( Enum rhs ) const { return Flags( *this ) ^= rhs; }
88 
89  Flags operator~() const { return Flags( ~_val ); }
90 
91  public:
92  Flags & setFlag( Flags flag_r, bool newval_r ) { return( newval_r ? setFlag(flag_r) : unsetFlag(flag_r) ); }
93  Flags & setFlag( Enum flag_r, bool newval_r ) { return( newval_r ? setFlag(flag_r) : unsetFlag(flag_r) ); }
94 
95  Flags & setFlag( Flags flag_r ) { _val |= flag_r; return *this; }
96  Flags & setFlag( Enum flag_r ) { _val |= flag_r; return *this; }
97 
98  Flags & unsetFlag( Flags flag_r ) { _val &= ~flag_r; return *this; }
99  Flags & unsetFlag( Enum flag_r ) { _val &= ~flag_r; return *this; }
100 
101  bool testFlag( Flags flag_r ) const { return ( _val & flag_r ) == flag_r; }
102  bool testFlag( Enum flag_r ) const { return ( _val & flag_r ) == flag_r; }
103 
104  private:
105  unsigned _val;
106  };
108 
109  template<typename Enum>
110  inline std::ostream & operator<<( std::ostream & str, const Flags<Enum> & obj )
111  { return str << str::hexstring(obj); }
112 
114 #define ZYPP_DECLARE_FLAGS(Name,Enum) typedef zypp::base::Flags<Enum> Name
115 
117 #define ZYPP_DECLARE_OPERATORS_FOR_FLAGS(Name) \
118 inline Name operator&( Name::enum_type lhs, Name::enum_type rhs ) { return Name( lhs ) &= rhs; } \
119 inline Name operator&( Name::enum_type lhs, Name rhs ) { return rhs &= lhs; } \
120 inline Name operator|( Name::enum_type lhs, Name::enum_type rhs ) { return Name( lhs ) |= rhs; } \
121 inline Name operator|( Name::enum_type lhs, Name rhs ) { return rhs |= lhs; } \
122 inline Name operator^( Name::enum_type lhs, Name::enum_type rhs ) { return Name( lhs ) ^= rhs; } \
123 inline Name operator^( Name::enum_type lhs, Name rhs ) { return rhs ^= lhs; } \
124 inline Name operator~( Name::enum_type lhs ) { return ~Name( lhs ); }
125 
127 #define ZYPP_DECLARE_FLAGS_AND_OPERATORS(Name,Enum) \
128  ZYPP_DECLARE_FLAGS(Name,Enum); \
129  ZYPP_DECLARE_OPERATORS_FOR_FLAGS(Name)
130 
132  } // namespace base
135 } // namespace zypp
137 #endif // ZYPP_BASE_FLAGS_H
Flags & operator|=(Flags rhs)
Definition: Flags.h:71
Flags & operator^=(Flags rhs)
Definition: Flags.h:74
unsigned _val
Definition: Flags.h:105
Flags(unsigned flag_r)
Definition: Flags.h:66
Flags & operator&=(Flags rhs)
Definition: Flags.h:68
Flags(Enum flag_r)
Definition: Flags.h:65
Flags & setFlag(Enum flag_r, bool newval_r)
Definition: Flags.h:93
Flags operator|(Enum rhs) const
Definition: Flags.h:84
Flags & setFlag(Enum flag_r)
Definition: Flags.h:96
Flags operator&(Flags rhs) const
Definition: Flags.h:80
Flags operator|(Flags rhs) const
Definition: Flags.h:83
A type-safe way of storing OR-combinations of enum values (like QTs QFlags).
Definition: Flags.h:58
Enum enum_type
Definition: Flags.h:61
Flags & unsetFlag(Enum flag_r)
Definition: Flags.h:99
Flags & operator^=(Enum rhs)
Definition: Flags.h:75
Flags & operator|=(Enum rhs)
Definition: Flags.h:72
Flags & unsetFlag(Flags flag_r)
Definition: Flags.h:98
Flags operator^(Enum rhs) const
Definition: Flags.h:87
Flags operator~() const
Definition: Flags.h:89
Flags operator^(Flags rhs) const
Definition: Flags.h:86
Flags & operator&=(Enum rhs)
Definition: Flags.h:69
Flags & setFlag(Flags flag_r, bool newval_r)
Definition: Flags.h:92
Flags & setFlag(Flags flag_r)
Definition: Flags.h:95
bool testFlag(Flags flag_r) const
Definition: Flags.h:101
bool testFlag(Enum flag_r) const
Definition: Flags.h:102
Flags operator&(Enum rhs) const
Definition: Flags.h:81
std::string hexstring(char n, int w=4)
Definition: String.h:254