From 796cc3a4d6ce7fd4d857e0985392efccb388dfb1 Mon Sep 17 00:00:00 2001 From: Mikhail Pozdnyakov Date: Tue, 10 Sep 2019 16:18:54 +0300 Subject: [core] Bitmask operations for enums --- include/mbgl/storage/resource.hpp | 18 ++--------------- include/mbgl/util/bitmask_operations.hpp | 33 ++++++++++++++++++++++++++++++++ src/core-files.json | 1 + src/mbgl/layout/symbol_instance.hpp | 15 +-------------- src/mbgl/renderer/render_layer.cpp | 2 +- src/mbgl/renderer/render_pass.hpp | 15 +-------------- src/mbgl/text/glyph.hpp | 21 +------------------- 7 files changed, 40 insertions(+), 65 deletions(-) create mode 100644 include/mbgl/util/bitmask_operations.hpp diff --git a/include/mbgl/storage/resource.hpp b/include/mbgl/storage/resource.hpp index 40c4c836b1..d00f336669 100644 --- a/include/mbgl/storage/resource.hpp +++ b/include/mbgl/storage/resource.hpp @@ -1,11 +1,10 @@ #pragma once #include +#include #include #include #include -#include -#include #include @@ -98,21 +97,8 @@ public: std::shared_ptr priorData; }; - -MBGL_CONSTEXPR Resource::LoadingMethod operator|(Resource::LoadingMethod a, Resource::LoadingMethod b) { - return Resource::LoadingMethod(mbgl::underlying_type(a) | mbgl::underlying_type(b)); -} - -MBGL_CONSTEXPR Resource::LoadingMethod& operator|=(Resource::LoadingMethod& a, Resource::LoadingMethod b) { - return (a = a | b); -} - -MBGL_CONSTEXPR Resource::LoadingMethod operator&(Resource::LoadingMethod a, Resource::LoadingMethod b) { - return Resource::LoadingMethod(mbgl::underlying_type(a) & mbgl::underlying_type(b)); -} - inline bool Resource::hasLoadingMethod(Resource::LoadingMethod method) { - return (loadingMethod & method) != Resource::LoadingMethod::None; + return (loadingMethod & method); } } // namespace mbgl diff --git a/include/mbgl/util/bitmask_operations.hpp b/include/mbgl/util/bitmask_operations.hpp new file mode 100644 index 0000000000..014fabdea2 --- /dev/null +++ b/include/mbgl/util/bitmask_operations.hpp @@ -0,0 +1,33 @@ +#pragma once + +#include +#include + +namespace mbgl { + +template +MBGL_CONSTEXPR Enum operator|(Enum a, Enum b) { + static_assert(std::is_enum::value, "Enum must be an enum type"); + return Enum(mbgl::underlying_type(a) | mbgl::underlying_type(b)); +} + +template +MBGL_CONSTEXPR Enum& operator|=(Enum& a, Enum b) { + static_assert(std::is_enum::value, "Enum must be an enum type"); + return (a = a | b); +} + +template +MBGL_CONSTEXPR bool operator&(Enum a, Enum b) { + static_assert(std::is_enum::value, "Enum must be an enum type"); + return bool(mbgl::underlying_type(a) & mbgl::underlying_type(b)); +} + +template +MBGL_CONSTEXPR Enum operator~(Enum value) { + static_assert(std::is_enum::value, "Enum must be an enum type"); + return Enum(~mbgl::underlying_type(value)); +} + + +} // namespace mbgl \ No newline at end of file diff --git a/src/core-files.json b/src/core-files.json index 628ab5e3a7..9453e504bb 100644 --- a/src/core-files.json +++ b/src/core-files.json @@ -456,6 +456,7 @@ "mbgl/tile/tile_necessity.hpp": "include/mbgl/tile/tile_necessity.hpp", "mbgl/util/async_request.hpp": "include/mbgl/util/async_request.hpp", "mbgl/util/async_task.hpp": "include/mbgl/util/async_task.hpp", + "mbgl/util/bitmask_operations.hpp": "include/mbgl/util/bitmask_operations.hpp", "mbgl/util/char_array_buffer.hpp": "include/mbgl/util/char_array_buffer.hpp", "mbgl/util/chrono.hpp": "include/mbgl/util/chrono.hpp", "mbgl/util/color.hpp": "include/mbgl/util/color.hpp", diff --git a/src/mbgl/layout/symbol_instance.hpp b/src/mbgl/layout/symbol_instance.hpp index 3e2ab5f8b3..4a57b527f7 100644 --- a/src/mbgl/layout/symbol_instance.hpp +++ b/src/mbgl/layout/symbol_instance.hpp @@ -4,8 +4,7 @@ #include #include #include -#include -#include +#include namespace mbgl { @@ -50,18 +49,6 @@ enum class SymbolContent : uint8_t { IconSDF = 1 << 2 }; -MBGL_CONSTEXPR SymbolContent operator|(SymbolContent a, SymbolContent b) { - return SymbolContent(mbgl::underlying_type(a) | mbgl::underlying_type(b)); -} - -MBGL_CONSTEXPR SymbolContent& operator|=(SymbolContent& a, SymbolContent b) { - return (a = a | b); -} - -MBGL_CONSTEXPR SymbolContent operator&(SymbolContent a, SymbolContent b) { - return SymbolContent(mbgl::underlying_type(a) & mbgl::underlying_type(b)); -} - class SymbolInstance { public: SymbolInstance(Anchor& anchor_, diff --git a/src/mbgl/renderer/render_layer.cpp b/src/mbgl/renderer/render_layer.cpp index c1ca1fd017..fe1b151b58 100644 --- a/src/mbgl/renderer/render_layer.cpp +++ b/src/mbgl/renderer/render_layer.cpp @@ -32,7 +32,7 @@ const std::string& RenderLayer::getID() const { } bool RenderLayer::hasRenderPass(RenderPass pass) const { - return bool(passes & pass); + return passes & pass; } bool RenderLayer::needsRendering() const { diff --git a/src/mbgl/renderer/render_pass.hpp b/src/mbgl/renderer/render_pass.hpp index 5d18304129..4d1b1f91f9 100644 --- a/src/mbgl/renderer/render_pass.hpp +++ b/src/mbgl/renderer/render_pass.hpp @@ -1,7 +1,6 @@ #pragma once -#include -#include +#include #include @@ -14,18 +13,6 @@ enum class RenderPass : uint8_t { Pass3D = 1 << 2, }; -MBGL_CONSTEXPR RenderPass operator|(RenderPass a, RenderPass b) { - return RenderPass(mbgl::underlying_type(a) | mbgl::underlying_type(b)); -} - -MBGL_CONSTEXPR RenderPass& operator|=(RenderPass& a, RenderPass b) { - return (a = a | b); -} - -MBGL_CONSTEXPR RenderPass operator&(RenderPass a, RenderPass b) { - return RenderPass(mbgl::underlying_type(a) & mbgl::underlying_type(b)); -} - // Defines whether the overdraw shaders should be used instead of the regular shaders. enum class PaintMode : bool { Regular = false, diff --git a/src/mbgl/text/glyph.hpp b/src/mbgl/text/glyph.hpp index 234f718975..ba9c521f77 100644 --- a/src/mbgl/text/glyph.hpp +++ b/src/mbgl/text/glyph.hpp @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include #include @@ -99,26 +100,6 @@ enum class WritingModeType : uint8_t { Vertical = 1 << 1, }; -MBGL_CONSTEXPR WritingModeType operator|(WritingModeType a, WritingModeType b) { - return WritingModeType(mbgl::underlying_type(a) | mbgl::underlying_type(b)); -} - -MBGL_CONSTEXPR WritingModeType& operator|=(WritingModeType& a, WritingModeType b) { - return (a = a | b); -} - -MBGL_CONSTEXPR bool operator&(WritingModeType lhs, WritingModeType rhs) { - return mbgl::underlying_type(lhs) & mbgl::underlying_type(rhs); -} - -MBGL_CONSTEXPR WritingModeType& operator&=(WritingModeType& lhs, WritingModeType rhs) { - return (lhs = WritingModeType(mbgl::underlying_type(lhs) & mbgl::underlying_type(rhs))); -} - -MBGL_CONSTEXPR WritingModeType operator~(WritingModeType value) { - return WritingModeType(~mbgl::underlying_type(value)); -} - using GlyphDependencies = std::map; using GlyphRangeDependencies = std::map>; -- cgit v1.2.1