libzypp 17.37.10

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-core/base/String.h>
16
18 namespace zypp
19{
21 namespace base
22 {
23
25 //
26 // CLASS NAME : Flags<Enum>
27 //
57 template<typename TEnum>
58 class Flags
59 {
60 public:
61 using Enum = TEnum;
62 using Integral = std::underlying_type_t<Enum>;
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
120 Integral _val;
121 };
122
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 = "|", const 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 += 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
zypp::base::Flags
A type-safe way of storing OR-combinations of enum values (like QTs QFlags).
Definition Flags.h:59
zypp::base::Flags::operator^
constexpr Flags operator^(Enum rhs) const
Definition Flags.h:94
zypp::base::Flags::isNone
constexpr bool isNone() const
Definition Flags.h:72
zypp::base::Flags::all
static constexpr Flags all()
Definition Flags.h:70
zypp::base::Flags::Flags
constexpr Flags(Integral flag_r)
Definition Flags.h:67
zypp::base::Flags::operator|
constexpr Flags operator|(Flags rhs) const
Definition Flags.h:90
zypp::base::Flags::Enum
TEnum Enum
The underlying enum type.
Definition Flags.h:61
zypp::base::Flags::setFlag
Flags & setFlag(Enum flag_r, bool newval_r)
Definition Flags.h:103
zypp::base::Flags::integral
static constexpr Integral integral(Enum obj)
Definition Flags.h:118
zypp::base::Flags::operator~
constexpr Flags operator~() const
Definition Flags.h:96
zypp::base::Flags::operator&=
Flags & operator&=(Flags rhs)
Definition Flags.h:75
zypp::base::Flags::testFlag
constexpr bool testFlag(Enum flag_r) const
Definition Flags.h:112
zypp::base::Flags::setFlag
Flags & setFlag(Enum flag_r)
Definition Flags.h:106
zypp::base::Flags::operator^=
Flags & operator^=(Flags rhs)
Definition Flags.h:81
zypp::base::Flags::operator&
constexpr Flags operator&(Flags rhs) const
Definition Flags.h:87
zypp::base::Flags::operator&=
Flags & operator&=(Enum rhs)
Definition Flags.h:76
zypp::base::Flags::Flags
constexpr Flags(Enum flag_r)
Definition Flags.h:66
zypp::base::Flags::operator|
constexpr Flags operator|(Enum rhs) const
Definition Flags.h:91
zypp::base::Flags::_val
Integral _val
Definition Flags.h:120
zypp::base::Flags::operator&
constexpr Flags operator&(Enum rhs) const
Definition Flags.h:88
zypp::base::Flags::testFlag
constexpr bool testFlag(Flags flag_r) const
Definition Flags.h:111
zypp::base::Flags::operator^=
Flags & operator^=(Enum rhs)
Definition Flags.h:82
zypp::base::Flags::isAll
constexpr bool isAll() const
Definition Flags.h:73
zypp::base::Flags::Flags
constexpr Flags()
Definition Flags.h:65
zypp::base::Flags::operator|=
Flags & operator|=(Flags rhs)
Definition Flags.h:78
zypp::base::Flags::testFlag
constexpr bool testFlag(Integral flag) const
Definition Flags.h:115
zypp::base::Flags::unsetFlag
Flags & unsetFlag(Enum flag_r)
Definition Flags.h:109
zypp::base::Flags::setFlag
Flags & setFlag(Flags flag_r)
Definition Flags.h:105
zypp::base::Flags::setFlag
Flags & setFlag(Flags flag_r, bool newval_r)
Definition Flags.h:102
zypp::base::Flags::operator!=
constexpr bool operator!=(Enum rhs) const
Definition Flags.h:99
zypp::base::Flags::unsetFlag
Flags & unsetFlag(Flags flag_r)
Definition Flags.h:108
zypp::base::Flags::Integral
std::underlying_type_t< Enum > Integral
The underlying integral type.
Definition Flags.h:62
zypp::base::Flags::operator==
constexpr bool operator==(Enum rhs) const
Definition Flags.h:98
zypp::base::Flags::operator^
constexpr Flags operator^(Flags rhs) const
Definition Flags.h:93
zypp::base::Flags::stringify
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="|", const std::string &extro_r="]")
Stringify Build a string of OR'ed names of each flag value set in flag_r.
Definition Flags.h:137
zypp::base::Flags::none
static constexpr Flags none()
Definition Flags.h:69
zypp::base::Flags::operator|=
Flags & operator|=(Enum rhs)
Definition Flags.h:79
zypp::base::Flags::integral
static constexpr Integral integral(Flags obj)
Definition Flags.h:117
std::underlying_type_t
typename underlying_type< T >::type underlying_type_t
Definition TypeTraits.h:54
str
String related utilities and Regular expression matching.
zypp::base
Definition Random.cc:7
zypp::base::operator<<
std::ostream & operator<<(std::ostream &str, const Flags< TEnum > &obj)
Definition Flags.h:166
zypp::str::hexstring
std::string hexstring(char n, int w=4)
Definition String.h:325
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition Application.cc:20
String.h