#include <iostream>
enum class NumericType {
None = 0,
PadWithZero = 0x01,
NegativeSign = 0x02,
PositiveSign = 0x04,
SpacePrefix = 0x08
};
class NumericTypeFlags {
static const NumericTypeFlags all_;
unsigned flags_;
NumericTypeFlags (unsigned f) : flags_(f) {}
public:
NumericTypeFlags () : flags_(0) {}
NumericTypeFlags (NumericType t) : flags_(static_cast<unsigned>(t)) {}
// test
NumericTypeFlags operator & (const NumericTypeFlags &t) const {
return flags_ & t.flags_;
}
NumericTypeFlags operator | (const NumericTypeFlags &t) const {
return flags_ | t.flags_;
}
operator bool () const { return flags_; }
// set
const NumericTypeFlags & operator &= (const NumericTypeFlags &t) {
flags_ &= t.flags_;
return *this;
}
const NumericTypeFlags & operator |= (const NumericTypeFlags &t) {
flags_ |= t.flags_;
return *this;
}
// complement
NumericTypeFlags operator ~ () const {
return all_.flags_ & ~flags_;
}
};
NumericTypeFlags operator | (NumericType a, NumericType b)
{
return NumericTypeFlags(a)|b;
}
NumericTypeFlags operator ~ (NumericType a)
{
return ~NumericTypeFlags(a);
}
const NumericTypeFlags NumericTypeFlags::all_ = (
NumericType::PadWithZero |
NumericType::NegativeSign |
NumericType::PositiveSign |
NumericType::SpacePrefix
);
void foo (NumericTypeFlags f) {
if (f) {
if (f & NumericType::PadWithZero) std::cout << "+PWZ";
if (f & NumericType::NegativeSign) std::cout << "+NS";
if (f & NumericType::PositiveSign) std::cout << "+PS";
if (f & NumericType::SpacePrefix) std::cout << "+SP";
} else {
std::cout << "None";
}
std::cout << std::endl;
}
int main ()
{
foo(NumericTypeFlags());
foo(NumericType::PadWithZero
|NumericType::SpacePrefix
|NumericType::NegativeSign);
foo(NumericType::PositiveSign);
foo(~NumericType::SpacePrefix);
NumericTypeFlags flags(NumericType::PadWithZero
|NumericType::SpacePrefix
|NumericType::NegativeSign);
flags &= ~NumericType::NegativeSign;
foo(flags);
}