From 6c7955c0961d907cd660ff55cde65a537904b7f5 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Fri, 28 May 2021 18:27:30 -0400 Subject: Remove static downcasts in C++ bindings --- bindings/cpp/include/.clang-tidy | 1 - bindings/cpp/include/pugl/pugl.hpp | 58 ++++++++++++++++++-------------------- 2 files changed, 28 insertions(+), 31 deletions(-) (limited to 'bindings/cpp/include') diff --git a/bindings/cpp/include/.clang-tidy b/bindings/cpp/include/.clang-tidy index 1c986fe..15651d3 100644 --- a/bindings/cpp/include/.clang-tidy +++ b/bindings/cpp/include/.clang-tidy @@ -4,7 +4,6 @@ Checks: > -clang-diagnostic-unused-macros, -cppcoreguidelines-pro-bounds-pointer-arithmetic, -cppcoreguidelines-pro-type-reinterpret-cast, - -cppcoreguidelines-pro-type-static-cast-downcast, -google-runtime-references, -hicpp-named-parameter, -llvmlibc-*, diff --git a/bindings/cpp/include/pugl/pugl.hpp b/bindings/cpp/include/pugl/pugl.hpp index 69ea0eb..85643ad 100644 --- a/bindings/cpp/include/pugl/pugl.hpp +++ b/bindings/cpp/include/pugl/pugl.hpp @@ -88,8 +88,7 @@ using Rect = PuglRect; ///< @copydoc PuglRect /** A strongly-typed analogue of PuglEvent. - This is bit-for-bit identical to the corresponding PuglEvent, so events are - simply cast to this type to avoid any copying overhead. + This is bit-for-bit identical to the corresponding PuglEvent. @tparam t The `type` field of the corresponding PuglEvent. @@ -103,6 +102,10 @@ struct Event final : Base { /// The `type` field of the corresponding C event structure static constexpr const PuglEventType type = t; + explicit Event(Base base) + : Base{base} + {} + template explicit Event(const PuglEventFlags f, Args... args) : Base{t, f, args...} @@ -652,56 +655,51 @@ private: case PUGL_NOTHING: return Status::success; case PUGL_CREATE: - return target.onEvent(static_cast(event->any)); + return target.onEvent(CreateEvent{event->any}); case PUGL_DESTROY: - return target.onEvent(static_cast(event->any)); + return target.onEvent(DestroyEvent{event->any}); case PUGL_CONFIGURE: - return target.onEvent( - static_cast(event->configure)); + return target.onEvent(ConfigureEvent{event->configure}); case PUGL_MAP: - return target.onEvent(static_cast(event->any)); + return target.onEvent(MapEvent{event->any}); case PUGL_UNMAP: - return target.onEvent(static_cast(event->any)); + return target.onEvent(UnmapEvent{event->any}); case PUGL_UPDATE: - return target.onEvent(static_cast(event->any)); + return target.onEvent(UpdateEvent{event->any}); case PUGL_EXPOSE: - return target.onEvent(static_cast(event->expose)); + return target.onEvent(ExposeEvent{event->expose}); case PUGL_CLOSE: - return target.onEvent(static_cast(event->any)); + return target.onEvent(CloseEvent{event->any}); case PUGL_FOCUS_IN: - return target.onEvent(static_cast(event->focus)); + return target.onEvent(FocusInEvent{event->focus}); case PUGL_FOCUS_OUT: - return target.onEvent(static_cast(event->focus)); + return target.onEvent(FocusOutEvent{event->focus}); case PUGL_KEY_PRESS: - return target.onEvent(static_cast(event->key)); + return target.onEvent(KeyPressEvent{event->key}); case PUGL_KEY_RELEASE: - return target.onEvent(static_cast(event->key)); + return target.onEvent(KeyReleaseEvent{event->key}); case PUGL_TEXT: - return target.onEvent(static_cast(event->text)); + return target.onEvent(TextEvent{event->text}); case PUGL_POINTER_IN: - return target.onEvent( - static_cast(event->crossing)); + return target.onEvent(PointerInEvent{event->crossing}); case PUGL_POINTER_OUT: - return target.onEvent( - static_cast(event->crossing)); + return target.onEvent(PointerOutEvent{event->crossing}); case PUGL_BUTTON_PRESS: - return target.onEvent( - static_cast(event->button)); + return target.onEvent(ButtonPressEvent{event->button}); case PUGL_BUTTON_RELEASE: - return target.onEvent( - static_cast(event->button)); + return target.onEvent(ButtonReleaseEvent{event->button}); case PUGL_MOTION: - return target.onEvent(static_cast(event->motion)); + return target.onEvent(MotionEvent{event->motion}); case PUGL_SCROLL: - return target.onEvent(static_cast(event->scroll)); + return target.onEvent(ScrollEvent{event->scroll}); case PUGL_CLIENT: - return target.onEvent(static_cast(event->client)); + return target.onEvent(ClientEvent{event->client}); case PUGL_TIMER: - return target.onEvent(static_cast(event->timer)); + return target.onEvent(TimerEvent{event->timer}); case PUGL_LOOP_ENTER: - return target.onEvent(static_cast(event->any)); + return target.onEvent(LoopEnterEvent{event->any}); case PUGL_LOOP_LEAVE: - return target.onEvent(static_cast(event->any)); + return target.onEvent(LoopLeaveEvent{event->any}); } return Status::failure; -- cgit v1.2.1