From 1fab6d64308a7abd940d49d03af329473f928526 Mon Sep 17 00:00:00 2001 From: Karn Kaul Date: Thu, 1 Feb 2024 23:07:00 +0530 Subject: [PATCH] Add `EnumFlags`, replace all raw bitsets. --- lib/docs/changelog.md | 4 +-- lib/include/bave/app.hpp | 15 ++++----- lib/include/bave/core/enum_flags.hpp | 33 +++++++++++++++++++ lib/include/bave/core/make_bitset.hpp | 31 ----------------- .../bave/graphics/particle_emitter.hpp | 6 ++-- lib/include/bave/input/gamepad.hpp | 6 ++-- lib/include/bave/input/key_state.hpp | 8 ++--- lib/include/bave/input/mods.hpp | 12 ++----- lib/platform/desktop/desktop_app.cpp | 20 +++++------ lib/src/app.cpp | 4 +-- lib/src/graphics/particle_emitter.cpp | 8 ++--- tools/src/tools/runner.cpp | 2 +- 12 files changed, 70 insertions(+), 79 deletions(-) create mode 100644 lib/include/bave/core/enum_flags.hpp delete mode 100644 lib/include/bave/core/make_bitset.hpp diff --git a/lib/docs/changelog.md b/lib/docs/changelog.md index fd9a4b4..dad3fe8 100644 --- a/lib/docs/changelog.md +++ b/lib/docs/changelog.md @@ -7,8 +7,8 @@ - Added bave::Gamepad support, queried through bave::App::get_gamepad() and bave::App::get_gamepads(). - Not yet supported on Android. - Added bave::KeyState and bave::App::get_key_state(). +- Added bave::EnumFlags. ### Changes -- Replaced `bave::mod::*` with bave::Mod. -- Refactored bave::ParticleEmitter::Modifier into an enum. +- Unified all bit flags to be bave::EnumFlags. diff --git a/lib/include/bave/app.hpp b/lib/include/bave/app.hpp index 6946abe..3db822a 100644 --- a/lib/include/bave/app.hpp +++ b/lib/include/bave/app.hpp @@ -32,16 +32,15 @@ enum struct ErrCode : int { eSuccess = EXIT_SUCCESS, eFailure = EXIT_FAILURE }; class App : public PolyPinned { public: /// \brief Individual feature flags. - struct Feature { - static constexpr std::size_t resizeable{0}; - static constexpr std::size_t has_title{1}; - static constexpr std::size_t has_icon{2}; - static constexpr std::size_t validation_layers{3}; - - static constexpr std::size_t count_v{8}; + enum class Feature : int { + eResizeable, + eHasTitle, + eHasIcon, + eValidationLayers, + eCOUNT_, }; /// \brief Bitset of feature flags. - using FeatureFlags = std::bitset; + using FeatureFlags = EnumFlags; /// \brief Driver Factory. using Bootloader = std::function(App&)>; diff --git a/lib/include/bave/core/enum_flags.hpp b/lib/include/bave/core/enum_flags.hpp new file mode 100644 index 0000000..b20f3c1 --- /dev/null +++ b/lib/include/bave/core/enum_flags.hpp @@ -0,0 +1,33 @@ +#pragma once +#include +#include + +namespace bave { +/// \brief Enum-indexed wrapper around std::bitset. +template +class EnumFlags : public std::bitset { + public: + using std::bitset::bitset; + + /// \brief Constructor taking multiple flags. + /// \param to_set flags to set. + template ... T> + EnumFlags(T const... to_set) { + (this->set(to_set), ...); + } + + /// \brief Test if a flag is set. + /// \param e Flag to test for. + /// \returns true if set. + [[nodiscard]] auto test(E const e) const -> bool { return std::bitset::test(static_cast(e)); } + /// \brief Set a flag. + /// \param e Flag to set. + void set(E const e) { std::bitset::set(static_cast(e)); } + /// \brief Reset a flag. + /// \param e Flag to reset. + void reset(E const e) { std::bitset::reset(static_cast(e)); } + + [[nodiscard]] auto operator[](E const e) -> decltype(auto) { return std::bitset::operator[](static_cast(e)); } + [[nodiscard]] auto operator[](E const e) const -> decltype(auto) { return std::bitset::operator[](static_cast(e)); } +}; +} // namespace bave diff --git a/lib/include/bave/core/make_bitset.hpp b/lib/include/bave/core/make_bitset.hpp deleted file mode 100644 index 59850c5..0000000 --- a/lib/include/bave/core/make_bitset.hpp +++ /dev/null @@ -1,31 +0,0 @@ -#pragma once -#include -#include - -namespace bave { -/// \brief Create a std::bitset with bits set. -/// \param bits bits to set. -/// \returns std::bitset with bits set. -template ... I> -auto make_bitset(I const... bits) -> std::bitset { - auto ret = std::bitset{}; - (ret.set(bits), ...); - return ret; -} - -/// \brief Create a std::bitset with bits set. -/// \param bits bits to set. -/// \returns Ret with bits set. -template ... I> -auto make_bitset(I const... bits) -> Ret { - return make_bitset(bits...); -} - -/// \brief Create a std::bitset with bits set. -/// \param bits bits to set, as enum values. -/// \returns Ret with bits set. -template -auto make_bitset(I const... bits) -> Ret { - return make_bitset(static_cast(bits)...); -} -} // namespace bave diff --git a/lib/include/bave/graphics/particle_emitter.hpp b/lib/include/bave/graphics/particle_emitter.hpp index c520fc9..bc8d826 100644 --- a/lib/include/bave/graphics/particle_emitter.hpp +++ b/lib/include/bave/graphics/particle_emitter.hpp @@ -1,6 +1,6 @@ #pragma once +#include #include -#include #include #include @@ -35,9 +35,9 @@ struct ParticleConfig { class ParticleEmitter : public QuadShape { public: enum class Modifier : int { eTranslate, eRotate, eScale, eTint, eCOUNT_ }; - using Modifiers = std::bitset(Modifier::eCOUNT_)>; + using Modifiers = EnumFlags; - inline static auto const all_modifiers_v = make_bitset(Modifier::eTranslate, Modifier::eRotate, Modifier::eScale, Modifier::eTint); + inline static auto const all_modifiers_v = Modifiers{Modifier::eTranslate, Modifier::eRotate, Modifier::eScale, Modifier::eTint}; using Config = ParticleConfig; diff --git a/lib/include/bave/input/gamepad.hpp b/lib/include/bave/input/gamepad.hpp index 16295c8..809627e 100644 --- a/lib/include/bave/input/gamepad.hpp +++ b/lib/include/bave/input/gamepad.hpp @@ -1,8 +1,8 @@ #pragma once #include +#include #include #include -#include namespace bave { /// \brief Set of gamepad IDs. @@ -66,7 +66,7 @@ struct Gamepad { /// \brief ID of gamepad. Id id{}; /// \brief Button states. - std::bitset button_states{}; + EnumFlags