libzypp  17.14.0
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 TEnum>
58  class Flags
59  {
60  public:
61  typedef TEnum Enum;
62  typedef typename std::underlying_type<Enum>::type Integral;
63 
64  public:
65  constexpr Flags() : _val( 0 ) {}
66  constexpr Flags( Enum flag_r ) : _val( integral(flag_r) ) {}
67  constexpr explicit Flags( Integral flag_r ) : _val( flag_r ) {}
68 
69  constexpr static Flags none() { return Flags( Integral(0) ); }
70  constexpr static Flags all() { return Flags( ~Integral(0) ); }
71 
72  constexpr bool isNone() const { return _val == Integral(0); }
73  constexpr bool isAll() const { return _val == ~Integral(0); }
74 
75  Flags & operator&=( Flags rhs ) { _val &= integral(rhs); return *this; }
76  Flags & operator&=( Enum rhs ) { _val &= integral(rhs); return *this; }
77 
78  Flags & operator|=( Flags rhs ) { _val |= integral(rhs); return *this; }
79  Flags & operator|=( Enum rhs ) { _val |= integral(rhs); return *this; }
80 
81  Flags & operator^=( Flags rhs ) { _val ^= integral(rhs); return *this; }
82  Flags & operator^=( Enum rhs ) { _val ^= integral(rhs); return *this; }
83 
84  public:
85  constexpr operator Integral() const { return _val; }
86 
87  constexpr Flags operator&( Flags rhs ) const { return Flags( _val & integral(rhs) ); }
88  constexpr Flags operator&( Enum rhs ) const { return Flags( _val & integral(rhs) ); }
89 
90  constexpr Flags operator|( Flags rhs ) const { return Flags( _val | integral(rhs) ); }
91  constexpr Flags operator|( Enum rhs ) const { return Flags( _val | integral(rhs) ); }
92 
93  constexpr Flags operator^( Flags rhs ) const { return Flags( _val ^ integral(rhs) ); }
94  constexpr Flags operator^( Enum rhs ) const { return Flags( _val ^ integral(rhs) ); }
95 
96  constexpr Flags operator~() const { return Flags( ~_val ); }
97 
98  constexpr bool operator==( Enum rhs ) const { return( _val == integral(rhs) ); }
99  constexpr bool operator!=( Enum rhs ) const { return( _val != integral(rhs) ); }
100 
101  public:
102  Flags & setFlag( Flags flag_r, bool newval_r ) { return( newval_r ? setFlag(flag_r) : unsetFlag(flag_r) ); }
103  Flags & setFlag( Enum flag_r, bool newval_r ) { return( newval_r ? setFlag(flag_r) : unsetFlag(flag_r) ); }
104 
105  Flags & setFlag( Flags flag_r ) { _val |= integral(flag_r); return *this; }
106  Flags & setFlag( Enum flag_r ) { _val |= integral(flag_r); return *this; }
107 
108  Flags & unsetFlag( Flags flag_r ) { _val &= ~integral(flag_r); return *this; }
109  Flags & unsetFlag( Enum flag_r ) { _val &= ~integral(flag_r); return *this; }
110 
111  constexpr bool testFlag( Flags flag_r ) const { return testFlag( integral(flag_r) ); }
112  constexpr bool testFlag( Enum flag_r ) const { return testFlag( integral(flag_r) ); }
113 
114  private:
115  constexpr bool testFlag( Integral flag ) const { return flag ? ( _val & flag ) == flag : !_val; }
116 
117  constexpr static Integral integral( Flags obj ) { return obj._val; }
118  constexpr static Integral integral( Enum obj ) { return static_cast<Integral>(obj); }
119 
121  };
123 
136  template<typename Enum>
137  std::string stringify( const Flags<Enum> & flag_r, const std::initializer_list<std::pair<Flags<Enum>,std::string> > & flaglist_r = {},
138  std::string intro_r = "[", std::string sep_r = "|", std::string extro_r = "]" )
139  {
140  std::string ret( std::move(intro_r) );
141  std::string sep;
142 
143  Flags<Enum> mask;
144  for ( const auto & pair : flaglist_r )
145  {
146  if ( flag_r.testFlag( pair.first ) )
147  {
148  mask |= pair.first;
149  ret += sep;
150  ret += pair.second;
151  if ( sep.empty() && !sep_r.empty() )
152  { sep = std::move(sep_r); }
153  }
154  }
155  mask = flag_r & ~mask;
156  if ( mask )
157  {
158  ret += sep;
159  ret += str::hexstring( mask, 0 );
160  }
161  ret += std::move(extro_r);
162  return ret;
163  }
164 
165  template<typename TEnum>
166  inline std::ostream & operator<<( std::ostream & str, const Flags<TEnum> & obj )
167  { return str << str::hexstring(obj); }
168 
169  template<typename TEnum>
170  inline std::ostream & operator<<( std::ostream & str, const typename Flags<TEnum>::Enum & obj )
171  { return str << Flags<TEnum>(obj); }
172 
174 #define ZYPP_DECLARE_FLAGS(Name,Enum) typedef zypp::base::Flags<Enum> Name
175 
177 #define ZYPP_DECLARE_OPERATORS_FOR_FLAGS(Name) \
178 inline constexpr bool operator==( Name::Enum lhs, Name rhs ) { return( rhs == lhs ); } \
179 inline constexpr bool operator!=(Name:: Enum lhs, Name rhs ) { return( rhs != lhs ); } \
180 inline constexpr Name operator&( Name::Enum lhs, Name::Enum rhs ) { return Name( lhs ) & rhs; } \
181 inline constexpr Name operator&( Name::Enum lhs, Name rhs ) { return rhs & lhs; } \
182 inline constexpr Name operator|( Name::Enum lhs, Name::Enum rhs ) { return Name( lhs ) | rhs; } \
183 inline constexpr Name operator|( Name::Enum lhs, Name rhs ) { return rhs | lhs; } \
184 inline constexpr Name operator^( Name::Enum lhs, Name::Enum rhs ) { return Name( lhs ) ^ rhs; } \
185 inline constexpr Name operator^( Name::Enum lhs, Name rhs ) { return rhs ^ lhs; } \
186 inline constexpr Name operator~( Name::Enum lhs ) { return ~Name( lhs ); }
187 
189 #define ZYPP_DECLARE_FLAGS_AND_OPERATORS(Name,Enum) \
190  ZYPP_DECLARE_FLAGS(Name,Enum); \
191  ZYPP_DECLARE_OPERATORS_FOR_FLAGS(Name)
192 
194  } // namespace base
197 } // namespace zypp
199 #endif // ZYPP_BASE_FLAGS_H
constexpr bool testFlag(Integral flag) const
Definition: Flags.h:115
constexpr Flags(Integral flag_r)
Definition: Flags.h:67
constexpr bool isAll() const
Definition: Flags.h:73
Flags & operator|=(Flags rhs)
Definition: Flags.h:78
constexpr Flags(Enum flag_r)
Definition: Flags.h:66
TEnum Enum
The underlying enum type.
Definition: Flags.h:61
Flags & operator^=(Enum rhs)
Definition: Flags.h:82
Integral _val
Definition: Flags.h:120
String related utilities and Regular expression matching.
std::ostream & operator<<(std::ostream &str, const Flags< TEnum > &obj)
Definition: Flags.h:166
static constexpr Integral integral(Flags obj)
Definition: Flags.h:117
std::string stringify(const Flags< Enum > &flag_r, const std::initializer_list< std::pair< Flags< Enum >, std::string > > &flaglist_r={}, std::string intro_r="[", std::string sep_r="|", std::string extro_r="]")
Definition: Flags.h:137
constexpr Flags()
Definition: Flags.h:65
constexpr Flags operator^(Enum rhs) const
Definition: Flags.h:94
constexpr bool operator!=(Enum rhs) const
Definition: Flags.h:99
A type-safe way of storing OR-combinations of enum values (like QTs QFlags).
Definition: Flags.h:58
Flags & operator&=(Flags rhs)
Definition: Flags.h:75
Flags & operator|=(Enum rhs)
Definition: Flags.h:79
Flags & setFlag(Enum flag_r)
Definition: Flags.h:106
static constexpr Integral integral(Enum obj)
Definition: Flags.h:118
constexpr bool testFlag(Enum flag_r) const
Definition: Flags.h:112
Flags & operator&=(Enum rhs)
Definition: Flags.h:76
constexpr bool operator==(Enum rhs) const
Definition: Flags.h:98
Flags & unsetFlag(Flags flag_r)
Definition: Flags.h:108
static constexpr Flags all()
Definition: Flags.h:70
Flags & operator^=(Flags rhs)
Definition: Flags.h:81
constexpr Flags operator^(Flags rhs) const
Definition: Flags.h:93
Flags & setFlag(Flags flag_r, bool newval_r)
Definition: Flags.h:102
constexpr bool testFlag(Flags flag_r) const
Definition: Flags.h:111
constexpr bool isNone() const
Definition: Flags.h:72
Flags & unsetFlag(Enum flag_r)
Definition: Flags.h:109
Flags & setFlag(Enum flag_r, bool newval_r)
Definition: Flags.h:103
constexpr Flags operator|(Flags rhs) const
Definition: Flags.h:90
constexpr Flags operator|(Enum rhs) const
Definition: Flags.h:91
constexpr Flags operator~() const
Definition: Flags.h:96
std::underlying_type< Enum >::type Integral
The underlying integral type.
Definition: Flags.h:62
static constexpr Flags none()
Definition: Flags.h:69
constexpr Flags operator &(Flags rhs) const
Definition: Flags.h:87
Flags & setFlag(Flags flag_r)
Definition: Flags.h:105
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:1
std::string hexstring(char n, int w=4)
Definition: String.h:323