fork download
  1. #include <iostream>
  2.  
  3. enum class NumericType {
  4. None = 0,
  5. PadWithZero = 0x01,
  6. NegativeSign = 0x02,
  7. PositiveSign = 0x04,
  8. SpacePrefix = 0x08
  9. };
  10.  
  11. class NumericTypeFlags {
  12. static const NumericTypeFlags all_;
  13. unsigned flags_;
  14. NumericTypeFlags (unsigned f) : flags_(f) {}
  15. public:
  16. NumericTypeFlags () : flags_(0) {}
  17. NumericTypeFlags (NumericType t) : flags_(static_cast<unsigned>(t)) {}
  18. // test
  19. NumericTypeFlags operator & (const NumericTypeFlags &t) const {
  20. return flags_ & t.flags_;
  21. }
  22. NumericTypeFlags operator | (const NumericTypeFlags &t) const {
  23. return flags_ | t.flags_;
  24. }
  25. operator bool () const { return flags_; }
  26. // set
  27. const NumericTypeFlags & operator &= (const NumericTypeFlags &t) {
  28. flags_ &= t.flags_;
  29. return *this;
  30. }
  31. const NumericTypeFlags & operator |= (const NumericTypeFlags &t) {
  32. flags_ |= t.flags_;
  33. return *this;
  34. }
  35. // complement
  36. NumericTypeFlags operator ~ () const {
  37. return all_.flags_ & ~flags_;
  38. }
  39. };
  40.  
  41. NumericTypeFlags operator | (NumericType a, NumericType b)
  42. {
  43. return NumericTypeFlags(a)|b;
  44. }
  45.  
  46. NumericTypeFlags operator ~ (NumericType a)
  47. {
  48. return ~NumericTypeFlags(a);
  49. }
  50.  
  51. const NumericTypeFlags NumericTypeFlags::all_ = (
  52. NumericType::PadWithZero |
  53. NumericType::NegativeSign |
  54. NumericType::PositiveSign |
  55. NumericType::SpacePrefix
  56. );
  57.  
  58. void foo (NumericTypeFlags f) {
  59. if (f) {
  60. if (f & NumericType::PadWithZero) std::cout << "+PWZ";
  61. if (f & NumericType::NegativeSign) std::cout << "+NS";
  62. if (f & NumericType::PositiveSign) std::cout << "+PS";
  63. if (f & NumericType::SpacePrefix) std::cout << "+SP";
  64. } else {
  65. std::cout << "None";
  66. }
  67. std::cout << std::endl;
  68. }
  69.  
  70. int main ()
  71. {
  72. foo(NumericTypeFlags());
  73. foo(NumericType::PadWithZero
  74. |NumericType::SpacePrefix
  75. |NumericType::NegativeSign);
  76. foo(NumericType::PositiveSign);
  77. foo(~NumericType::SpacePrefix);
  78.  
  79. NumericTypeFlags flags(NumericType::PadWithZero
  80. |NumericType::SpacePrefix
  81. |NumericType::NegativeSign);
  82. flags &= ~NumericType::NegativeSign;
  83. foo(flags);
  84. }
  85.  
Success #stdin #stdout 0s 2852KB
stdin
Standard input is empty
stdout
None
+PWZ+NS+SP
+PS
+PWZ+NS+PS
+PWZ+SP