fork download
  1. // For each bitfield type:
  2. // #define BITTYPES SEPERATOR(Red) SEPERATOR(Green) SEPERATOR(Blue)
  3. // #define BITNAME color
  4. // #define BITTYPE unsigned __int64 //This is optional. Defaults to unsigned __int64
  5. // #include "FastBitField.inl"
  6.  
  7. // generates the following class:
  8. // struct colorBitfield {
  9. // enum colorBitNum {RedBitNum=0, GreenBitNum=1, BlueBitNum=2};
  10. // enum colorFlag : unsigned __int64 {NoFlags=0, RedFlag=1, GreenFlag=2, BlueFlag=4, AllFlags=-1};
  11. // union {
  12. // unsigned __int64 BitField;
  13. // struct {
  14. // bool Red : 1;
  15. // bool Green : 1;
  16. // bool Blue : 1;
  17. // };
  18. // };
  19. // inline static const _TCHAR* const ToString(colorBitNum color)
  20. // {
  21. // switch(color) {
  22. // case RedBitNum: return _T("Red");
  23. // case GreenBitNum: return _T("Green");
  24. // case BlueBitNum: return _T("Blue");
  25. // default: return 0;
  26. // }
  27. // }
  28. // };
  29.  
  30. // which can be used as follows:
  31. // colorBitfield Pixel;
  32. // Pixel.BitField = 0; // sets all values to zero;
  33. // Pixel.Green = 1; // activates green;
  34. // cout << Pixel.BitField << std::endl; //this is machine dependant, probably 2. (010 in binary)
  35. // Pixel.BitField |= (colorBitfield::GreenFlag | colorBitfield::BlueFlag); // enables Green and Blue
  36. // cout << (Pixel.BitField & colorBitfield::BlueFlag) << std::endl; // 1, true.
  37.  
  38. // Notes: Only the first 64 bits are accessable with BitField.
  39. // If you have more than 64 values, BitField only has the first 64.
  40.  
  41. #ifndef BITTYPES
  42. #error BITTYPES MUST BE DEFINED FOR THIS FILE TO WORK
  43. #endif
  44. #ifndef BITNAME
  45. #error BITNAME MUST BE DEFINED FOR THIS FILE TO WORK
  46. #endif
  47. #ifndef BITTYPE
  48. #define BITTYPE unsigned __int64
  49. #endif
  50.  
  51. #ifdef SEPERATOR
  52. #define SEPERATORBACKUP SEPERATOR
  53. #undef SEPERATOR
  54. #endif
  55.  
  56. #define gluehelper(a,b) a ## b
  57. #define glue(a,b) gluehelper(a,b)
  58.  
  59. #ifdef _UNICODE
  60. #define _T(x) gluehelper(L,x)
  61. #define _TCHAR wchar_t
  62. #else
  63. #define _T(x) x
  64. #define _TCHAR char
  65. #endif
  66.  
  67. struct glue(BITNAME , Bitfield) {
  68. #define SEPERATOR(x) glue(x,BitNum),
  69. enum glue(BITNAME,BitNum) {BITTYPES};
  70. #undef SEPERATOR
  71. #define SEPERATOR(x) glue(x,Flag)=1LL<<(glue(x,BitNum)),
  72. enum glue(BITNAME,Flag) : BITTYPE {NoFlags=0, BITTYPES AllFlags=BITTYPE(-1)};
  73. union {
  74. BITTYPE BitField;
  75. struct {
  76. #undef SEPERATOR
  77. #define SEPERATOR(x) bool x : 1;
  78. BITTYPES
  79. };
  80. };
  81. inline static const _TCHAR* const ToString(glue(BITNAME,BitNum) BITNAME)
  82. {
  83. switch(BITNAME) {
  84. #undef SEPERATOR
  85. #define SEPERATOR(x) case glue(x,BitNum): return _T(#x);
  86. BITTYPES;
  87. default: return 0;
  88. }
  89. }
  90. };
  91.  
  92. #undef SEPERATOR
  93. #ifdef SEPERATORBACKUP
  94. #define SEPERATOR SEPERATORBACKUP
  95. #endif
  96.  
  97. #undef glue
  98. #undef gluehelper
  99. #undef BITTYPES
  100. #undef BITNAME
  101. #undef BITTYPE
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:42:2: error: #error BITTYPES MUST BE DEFINED FOR THIS FILE TO WORK
prog.cpp:45:2: error: #error BITNAME MUST BE DEFINED FOR THIS FILE TO WORK
prog.cpp:72: error: use of enum ‘BITNAMEFlag’ without previous declaration
prog.cpp:72: error: expected primary-expression before ‘unsigned’
prog.cpp:72: error: expected ‘;’ before ‘unsigned’
prog.cpp:74: error: ‘__int64’ does not name a type
prog.cpp:78: error: ISO C++ forbids declaration of ‘BITTYPES’ with no type
prog.cpp:79: error: expected ‘;’ before ‘}’ token
prog.cpp:79: error: expected `;' before ‘}’ token
prog.cpp: In static member function ‘static const char* const BITNAMEBitfield::ToString(BITNAMEBitfield::BITNAMEBitNum)’:
prog.cpp:86: warning: statement has no effect
stdout
Standard output is empty