Hello everybody,
I want to know if some people are interested in a type-safe flagsets. I
define flagsets as a container similar to a bitset, but where each bit has
a meaning.
For example, we could have in some game :
enum Direction : unsigned {
UP = 0b0001,
DOWN = 0b0010,
LEFT = 0b0100,
RIGHT = 0b1000
};
...
unsigned player_dir = UP | RIGHT;
if ((player_dir & (UP|LEFT)) == (UP|LEFT))
player.setAnim("up_left");
player_dir &= ~UP; //removing UP from player_dir
I think there are some issues with this kind of flagset :
- you need to think about power-of-twos when attributing values to your
flags.
- you need to use bitwise operations which can be non-trivial, even for
removing only one flag.
- it is not safe:
for example with another flagset using the flag INIT_VIDEO (in a window
library like SDL),
you can do " UP | INIT_VIDEO " even if the two flagsets have nothing in
common
With a type-safe flagset library, this code would become :
namespace Direction {
struct UP;
struct DOWN;
struct LEFT;
struct RIGHT;
using Type = flagset