Consider introducing an enum-compatible bitset class #14240

Closed
opened 2026-01-31 04:04:48 +00:00 by claunia · 1 comment
Owner

Originally created by @j4james on GitHub (Jun 15, 2021).

Description of the new feature/enhancement

I'm working on something at the moment where I need to manipulate various flag values. Typically we do this with an enum and a bunch of macro helper functions like WI_SetFlag, and WI_IsFlagSet. However, I found some scenarios that didn't work very well with this approach, and I was wondering if it might be better achieved with a std::bitset. Unfortunately a std::bitset isn't that great when working with enum values either, but I figured we could perhaps create a lightweight til template class that extends std::bitset with better enum support.

Proposed technical implementation details (optional)

The main things I want to achieve with this class are:

  1. Being able to use enum class values as parameters without having to cast them to a size_t first. This would require extended versions of set, reset, flip, and test which accept the enum type for the pos parameter.
  2. Being able to initialize the bitset with one or more enum values instead of an integer with the appropriate bits set. This can be achieved with a variadic template constructor.
  3. Being able to set or reset multiple values at the same time. This would require additional methods named something like set_all and reset_all, again using a variadic template to support multiple parameters.
  4. Being able to easily test multiple values at the same time. Something like test_any or just any to test if any of the given values are set, and test_all or all to test if all values are set.

I've got a WIP implementation which I've been using in my own code and also tested as a replacement for the current GridLines implementation in the renderer, and I think it does make the code slightly more readable. Would you be interested in a PR for this?

Originally created by @j4james on GitHub (Jun 15, 2021). # Description of the new feature/enhancement I'm working on something at the moment where I need to manipulate various flag values. Typically we do this with an enum and a bunch of macro helper functions like `WI_SetFlag`, and `WI_IsFlagSet`. However, I found some scenarios that didn't work very well with this approach, and I was wondering if it might be better achieved with a `std::bitset`. Unfortunately a `std::bitset` isn't that great when working with enum values either, but I figured we could perhaps create a lightweight `til` template class that extends `std::bitset` with better enum support. # Proposed technical implementation details (optional) The main things I want to achieve with this class are: 1. Being able to use `enum class` values as parameters without having to cast them to a `size_t` first. This would require extended versions of `set`, `reset`, `flip`, and `test` which accept the enum type for the _pos_ parameter. 2. Being able to initialize the bitset with one or more enum values instead of an integer with the appropriate bits set. This can be achieved with a variadic template constructor. 3. Being able to set or reset multiple values at the same time. This would require additional methods named something like `set_all` and `reset_all`, again using a variadic template to support multiple parameters. 4. Being able to easily test multiple values at the same time. Something like `test_any` or just `any` to test if any of the given values are set, and `test_all` or `all` to test if all values are set. I've got a WIP implementation which I've been using in my own code and also tested as a replacement for the current `GridLines` implementation in the renderer, and I think it does make the code slightly more readable. Would you be interested in a PR for this?
Author
Owner

@DHowett commented on GitHub (Jun 15, 2021):

I'm 100% on board for this. To channel @miniksa, "put as much crap in til as will reasonably fit."

@DHowett commented on GitHub (Jun 15, 2021): I'm 100% on board for this. To channel @miniksa, "put as much crap in `til` as will reasonably fit."
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#14240