Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bitwise operations for integer_ops #135

Open
stevemk14ebr opened this issue Jun 13, 2022 · 5 comments
Open

Bitwise operations for integer_ops #135

stevemk14ebr opened this issue Jun 13, 2022 · 5 comments

Comments

@stevemk14ebr
Copy link

Cool library!

The type_safe::strong_typedef_op could use the bitwise operations like &, |, ~, etc. For the integer op type these should probably be default.

@stevemk14ebr
Copy link
Author

stevemk14ebr commented Jun 13, 2022

Motivating example. On windows typedef DWORD ACCESS_MASK;. To check the flags one must do bitwise checks like mask & GENERIC_READ etc. This type can't be recreated with the strong_types as is.

@gerboengels
Copy link
Contributor

If you define your type like this

struct my_access_mask : type_safe::strong_typedef<my_access_mask, DWORD>,
                type_safe::strong_typedef_op::bitmask<my_access_mask>
{
    using strong_typedef::strong_typedef;
};

my_access_mask add_read(my_access_mask mask) {
  return mask & my_access_mask{ GENERIC_READ };
}

you get what you want, I think. Godbolt link https://godbolt.org/z/6Knj6sjbq

See Jonathan's blog post/tutorial as well, for some background on how to use this very useful library

@stevemk14ebr
Copy link
Author

I did not notice the bitmask op existed thank you. Perhaps this could be added to the integer ops by default, I had expected them to be present there.

@stevemk14ebr
Copy link
Author

hmm doing mask & MY_ACCESS_MASK{ GENERIC_READ } isn't giving me a bool as I'd like. By default the bitwise mask is giving back another MY_ACCESS_MASK, an automatic bool operator would be nice.

@foonathan
Copy link
Owner

I did not notice the bitmask op existed thank you. Perhaps this could be added to the integer ops by default, I had expected them to be present there.

I'm trying to make an explicit distinction between "integer operations" and "bit operations". In my opinion, integers and bit strings are different types and should be treated differently.

hmm doing mask & MY_ACCESS_MASK{ GENERIC_READ } isn't giving me a bool as I'd like. By default the bitwise mask is giving back another MY_ACCESS_MASK, an automatic bool operator would be nice.

The library explicitly tries to prevent implicit conversions. If you want, you can always write one:

struct my_access_mask : type_safe::strong_typedef<my_access_mask, DWORD>,
                type_safe::strong_typedef_op::bitmask<my_access_mask>
{
    using strong_typedef::strong_typedef;

     operator bool() const
    { return static_cast<DWORD>(*this) != 0; }
};

my_access_mask add_read(my_access_mask mask) {
  return mask & my_access_mask{ GENERIC_READ };
}

You might also be interested in using type_safe::flag_set instead: https://www.foonathan.net/2017/03/implementation-challenge-bitmask/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants