move math::enum_to_number to utils::enum_to_underlying

This commit is contained in:
2019-03-26 13:25:35 +07:00
parent 37fb586e27
commit ddc580a727
9 changed files with 55 additions and 54 deletions

View File

@@ -664,15 +664,6 @@ namespace e2d { namespace math
return approximately(v, T(0), precision);
}
//
// enum_to_number
//
template < typename E >
constexpr std::underlying_type_t<E> enum_to_number(E e) noexcept {
return static_cast<std::underlying_type_t<E>>(e);
}
//
// lerp/inverse_lerp
//

View File

@@ -136,4 +136,14 @@ namespace e2d { namespace utils
E2D_ASSERT(begin <= end);
return impl::sdbm_hash_impl(init, begin, end);
}
//
// enum_to_underlying
//
template < typename E
, typename = std::enable_if<std::is_enum<E>::value> >
constexpr std::underlying_type_t<E> enum_to_underlying(E e) noexcept {
return static_cast<std::underlying_type_t<E>>(e);
}
}}

View File

@@ -36,7 +36,7 @@ namespace
void on_keyboard_key(keyboard_key key, u32 scancode, keyboard_key_action act) noexcept final {
E2D_UNUSED(scancode);
auto key_i = math::enum_to_number(key);
auto key_i = utils::enum_to_underlying(key);
if ( key_i < E2D_COUNTOF(io_.KeysDown) ) {
switch ( act ) {
case keyboard_key_action::press:
@@ -210,7 +210,7 @@ namespace e2d
void setup_key_map_(ImGuiIO& io) noexcept {
const auto map_key = [&io](ImGuiKey_ imgui_key, keyboard_key key) noexcept {
E2D_ASSERT(imgui_key < ImGuiKey_COUNT);
io.KeyMap[imgui_key] = math::enum_to_number(key);
io.KeyMap[imgui_key] = utils::enum_to_underlying(key);
};
map_key(ImGuiKey_Tab, keyboard_key::tab);

View File

@@ -18,14 +18,14 @@ namespace e2d
v2f cursor_pos;
v2f scroll_delta;
bitset<
math::enum_to_number(mouse_button::unknown) + 1> pressed;
utils::enum_to_underlying(mouse_button::unknown) + 1> pressed;
bitset<
math::enum_to_number(mouse_button::unknown) + 1> just_pressed;
utils::enum_to_underlying(mouse_button::unknown) + 1> just_pressed;
bitset<
math::enum_to_number(mouse_button::unknown) + 1> just_released;
utils::enum_to_underlying(mouse_button::unknown) + 1> just_released;
public:
std::size_t button_index(mouse_button btn) const noexcept {
const auto index = math::enum_to_number(btn);
const auto index = utils::enum_to_underlying(btn);
E2D_ASSERT(index < pressed.size());
return math::numeric_cast<std::size_t>(index);
}
@@ -81,14 +81,14 @@ namespace e2d
mutable std::mutex mutex;
str32 input_text;
bitset<
math::enum_to_number(keyboard_key::unknown) + 1> pressed;
utils::enum_to_underlying(keyboard_key::unknown) + 1> pressed;
bitset<
math::enum_to_number(keyboard_key::unknown) + 1> just_pressed;
utils::enum_to_underlying(keyboard_key::unknown) + 1> just_pressed;
bitset<
math::enum_to_number(keyboard_key::unknown) + 1> just_released;
utils::enum_to_underlying(keyboard_key::unknown) + 1> just_released;
public:
std::size_t key_index(keyboard_key key) const noexcept {
const auto index = math::enum_to_number(key);
const auto index = utils::enum_to_underlying(key);
E2D_ASSERT(index < pressed.size());
return math::numeric_cast<std::size_t>(index);
}

View File

@@ -47,7 +47,7 @@ namespace
};
const pixel_type_description& get_pixel_type_description(pixel_declaration::pixel_type type) noexcept {
const std::size_t index = math::numeric_cast<std::size_t>(math::enum_to_number(type));
const std::size_t index = math::numeric_cast<std::size_t>(utils::enum_to_underlying(type));
E2D_ASSERT(index < E2D_COUNTOF(pixel_type_descriptions));
const pixel_type_description& desc = pixel_type_descriptions[index];
E2D_ASSERT(desc.type == type);

View File

@@ -802,12 +802,12 @@ namespace e2d
}
bool need_color =
math::enum_to_number(external_texture)
& math::enum_to_number(render_target::external_texture::color);
utils::enum_to_underlying(external_texture)
& utils::enum_to_underlying(render_target::external_texture::color);
bool need_depth =
math::enum_to_number(external_texture)
& math::enum_to_number(render_target::external_texture::depth);
utils::enum_to_underlying(external_texture)
& utils::enum_to_underlying(render_target::external_texture::depth);
texture_ptr color;
texture_ptr depth;
@@ -925,14 +925,14 @@ namespace e2d
E2D_ASSERT(is_in_main_thread());
bool clear_color =
math::enum_to_number(command.clear_buffer())
& math::enum_to_number(clear_command::buffer::color);
utils::enum_to_underlying(command.clear_buffer())
& utils::enum_to_underlying(clear_command::buffer::color);
bool clear_depth =
math::enum_to_number(command.clear_buffer())
& math::enum_to_number(clear_command::buffer::depth);
utils::enum_to_underlying(command.clear_buffer())
& utils::enum_to_underlying(clear_command::buffer::depth);
bool clear_stencil =
math::enum_to_number(command.clear_buffer())
& math::enum_to_number(clear_command::buffer::stencil);
utils::enum_to_underlying(command.clear_buffer())
& utils::enum_to_underlying(clear_command::buffer::stencil);
const render_target_ptr& rt = state_->render_target();
bool has_color = !rt || rt->state().color() || !rt->state().color_rb().empty();

View File

@@ -317,10 +317,10 @@ namespace e2d
convert_blending_equation(bs.rgb_equation()),
convert_blending_equation(bs.alpha_equation())));
GL_CHECK_CODE(debug_, glColorMask(
(math::enum_to_number(bs.color_mask()) & math::enum_to_number(blending_color_mask::r)) != 0,
(math::enum_to_number(bs.color_mask()) & math::enum_to_number(blending_color_mask::g)) != 0,
(math::enum_to_number(bs.color_mask()) & math::enum_to_number(blending_color_mask::b)) != 0,
(math::enum_to_number(bs.color_mask()) & math::enum_to_number(blending_color_mask::a)) != 0));
(utils::enum_to_underlying(bs.color_mask()) & utils::enum_to_underlying(blending_color_mask::r)) != 0,
(utils::enum_to_underlying(bs.color_mask()) & utils::enum_to_underlying(blending_color_mask::g)) != 0,
(utils::enum_to_underlying(bs.color_mask()) & utils::enum_to_underlying(blending_color_mask::b)) != 0,
(utils::enum_to_underlying(bs.color_mask()) & utils::enum_to_underlying(blending_color_mask::a)) != 0));
state_block_.blending(bs);
return *this;

View File

@@ -451,26 +451,6 @@ TEST_CASE("math") {
REQUIRE(math::is_near_zero(0.0001f, 0.001f));
REQUIRE(math::is_near_zero(-0.0001f, 0.001f));
}
{
enum class ee_u8 : u8 {
ee1 = 10,
ee2 = 42
};
enum class ee_i16 : i16 {
ee1 = 10,
ee2 = 42
};
auto e1 = math::enum_to_number(ee_u8::ee1);
auto e2 = math::enum_to_number(ee_i16::ee2);
REQUIRE(e1 == u8(10));
REQUIRE(e2 == i16(42));
static_assert(
std::is_same<u8, decltype(e1)>::value,
"static unit test error");
static_assert(
std::is_same<i16, decltype(e2)>::value,
"static unit test error");
}
{
REQUIRE(math::approximately(math::lerp(1.f, 11.f, 0.f), 1.f));
REQUIRE(math::approximately(math::lerp(1.f, 11.f, 0.5f), 6.f));

View File

@@ -9,6 +9,26 @@
using namespace e2d;
TEST_CASE("utils") {
{
enum class ee_u8 : u8 {
ee1 = 10,
ee2 = 42
};
enum class ee_i16 : i16 {
ee1 = 10,
ee2 = 42
};
auto e1 = utils::enum_to_underlying(ee_u8::ee1);
auto e2 = utils::enum_to_underlying(ee_i16::ee2);
REQUIRE(e1 == u8(10));
REQUIRE(e2 == i16(42));
static_assert(
std::is_same<u8, decltype(e1)>::value,
"static unit test error");
static_assert(
std::is_same<i16, decltype(e2)>::value,
"static unit test error");
}
{
REQUIRE(utils::sdbm_hash("") == 0u);
REQUIRE(utils::sdbm_hash(1u, "") == 1u);