31 #ifndef PTLIB_BITWISE_ENUM_H
32 #define PTLIB_BITWISE_ENUM_H
42 template <
typename BaseEnum, BaseEnum MaxValue,
typename BaseInt =
unsigned>
101 #define P_BITWISE_ENUM_INTERNAL_OP1(op) (static_cast<IntType>(m_enum) op static_cast<IntType>(rhs))
102 #define P_BITWISE_ENUM_INTERNAL_OP2(op) static_cast<Enumeration>(P_BITWISE_ENUM_INTERNAL_OP1(op))
146 #define P_DECLARE_BITWISE_ENUM_1(_0,_1)_0=0,_1=1
147 #define P_DECLARE_BITWISE_ENUM_2(_0,_1,_2)P_DECLARE_BITWISE_ENUM_1(_0,_1),_2=2
148 #define P_DECLARE_BITWISE_ENUM_3(_0,_1,_2,_3)P_DECLARE_BITWISE_ENUM_2(_0,_1,_2),_3=4
149 #define P_DECLARE_BITWISE_ENUM_4(_0,_1,_2,_3,_4)P_DECLARE_BITWISE_ENUM_3(_0,_1,_2,_3),_4=8
150 #define P_DECLARE_BITWISE_ENUM_5(_0,_1,_2,_3,_4,_5)P_DECLARE_BITWISE_ENUM_4(_0,_1,_2,_3,_4),_5=16
151 #define P_DECLARE_BITWISE_ENUM_6(_0,_1,_2,_3,_4,_5,_6)P_DECLARE_BITWISE_ENUM_5(_0,_1,_2,_3,_4,_5),_6=32
152 #define P_DECLARE_BITWISE_ENUM_7(_0,_1,_2,_3,_4,_5,_6,_7)P_DECLARE_BITWISE_ENUM_6(_0,_1,_2,_3,_4,_5,_6),_7=64
153 #define P_DECLARE_BITWISE_ENUM_8(_0,_1,_2,_3,_4,_5,_6,_7,_8)P_DECLARE_BITWISE_ENUM_7(_0,_1,_2,_3,_4,_5,_6,_7),_8=128
154 #define P_DECLARE_BITWISE_ENUM_9(_0,_1,_2,_3,_4,_5,_6,_7,_8,_9)P_DECLARE_BITWISE_ENUM_8(_0,_1,_2,_3,_4,_5,_6,_7,_8),_9=256
156 #define P_DECLARE_BITWISE_NAMES_1(_0,_1)#_0,#_1
157 #define P_DECLARE_BITWISE_NAMES_2(_0,_1,_2)P_DECLARE_BITWISE_NAMES_1(_0,_1),#_2
158 #define P_DECLARE_BITWISE_NAMES_3(_0,_1,_2,_3)P_DECLARE_BITWISE_NAMES_2(_0,_1,_2),#_3
159 #define P_DECLARE_BITWISE_NAMES_4(_0,_1,_2,_3,_4)P_DECLARE_BITWISE_NAMES_3(_0,_1,_2,_3),#_4
160 #define P_DECLARE_BITWISE_NAMES_5(_0,_1,_2,_3,_4,_5)P_DECLARE_BITWISE_NAMES_4(_0,_1,_2,_3,_4),#_5
161 #define P_DECLARE_BITWISE_NAMES_6(_0,_1,_2,_3,_4,_5,_6)P_DECLARE_BITWISE_NAMES_5(_0,_1,_2,_3,_4,_5),#_6
162 #define P_DECLARE_BITWISE_NAMES_7(_0,_1,_2,_3,_4,_5,_6,_7)P_DECLARE_BITWISE_NAMES_6(_0,_1,_2,_3,_4,_5,_6),#_7
163 #define P_DECLARE_BITWISE_NAMES_8(_0,_1,_2,_3,_4,_5,_6,_7,_8)P_DECLARE_BITWISE_NAMES_7(_0,_1,_2,_3,_4,_5,_6,_7),#_8
164 #define P_DECLARE_BITWISE_NAMES_9(_0,_1,_2,_3,_4,_5,_6,_7,_8,_9)P_DECLARE_BITWISE_NAMES_8(_0,_1,_2,_3,_4,_5,_6,_7,_8),#_9
166 #define P_DECLARE_BITWISE_ENUM_FRIENDS(name) \
167 __inline friend name##_Bits operator+(name##_Bits lhs, name##_Bits rhs) \
168 { return static_cast<name##_Bits>(static_cast<unsigned>(lhs) | static_cast<unsigned>(rhs)); } \
169 __inline friend name##_Bits operator|(name##_Bits lhs, name##_Bits rhs) \
170 { return static_cast<name##_Bits>(static_cast<unsigned>(lhs) | static_cast<unsigned>(rhs)); } \
171 __inline friend name##_Bits operator-(name##_Bits lhs, name##_Bits rhs) \
172 { return static_cast<name##_Bits>(static_cast<unsigned>(lhs) & ~static_cast<unsigned>(rhs)); }
174 #define P_DECLARE_BITWISE_ENUM_END(name, count) \
175 P_DECLARE_BITWISE_ENUM_FRIENDS(name) \
176 typedef PBitwiseEnum<name##_Bits, (name##_Bits)(1<<(count-1))> name
189 #define P_DECLARE_BITWISE_ENUM(name, count, values) \
190 enum name##_Bits { P_DECLARE_BITWISE_ENUM_##count values }; \
191 P_DECLARE_BITWISE_ENUM_END(name, count)
204 #define P_DECLARE_BITWISE_ENUM_EX(name, count, values, ...) \
205 enum name##_Bits { P_DECLARE_BITWISE_ENUM_##count values , ##__VA_ARGS__ }; \
206 P_DECLARE_BITWISE_ENUM_END(name, count)
209 extern void PPrintBitwiseEnum(std::ostream & strm,
unsigned bits,
char const *
const * names);
210 extern unsigned PReadBitwiseEnum(std::istream & strm,
char const *
const * names,
bool continueOnError =
false);
213 template <
typename BaseEnum, BaseEnum MaxValue,
typename BaseInt =
unsigned>
252 virtual char const *
const *
Names()
const = 0;
255 #define P_DECLARE_STREAMABLE_BITWISE_ENUM_EX(name, count, values, ...) \
256 enum name##_Bits { P_DECLARE_BITWISE_ENUM_##count values }; \
257 P_DECLARE_BITWISE_ENUM_FRIENDS(name) \
258 class name : public PStreamableBitwiseEnum<name##_Bits, (name##_Bits)(1<<count)>{ \
259 public: typedef PStreamableBitwiseEnum<name##_Bits, (name##_Bits)(1<<count)> BaseClass; \
260 __inline name(BaseClass::Enumeration e = BaseClass::Zero()) : BaseClass(e) { } \
261 __inline name(IteratorBounds b) : BaseClass(b) { } \
262 __inline explicit name(const PString & s) { FromString(s); } \
263 virtual char const * const * Names() const { static char const * const Strings[] = { __VA_ARGS__, NULL }; return Strings; } \
266 #define P_DECLARE_STREAMABLE_BITWISE_ENUM(name, count, values) \
267 P_DECLARE_STREAMABLE_BITWISE_ENUM_EX(name, count, values, P_DECLARE_BITWISE_NAMES_##count values)
270 #endif // PTLIB_BITWISE_ENUM_H