From 6a10575860cb5c966829f13db2d43a302d6bad70 Mon Sep 17 00:00:00 2001 From: Nomango Date: Wed, 15 Apr 2020 16:37:28 +0800 Subject: [PATCH] use static_assert instead of template meta programming --- src/kiwano/core/EventDispatcher.h | 6 ++++-- src/kiwano/core/EventListener.h | 6 ++++-- src/kiwano/core/event/Event.h | 15 ++++++++------- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/kiwano/core/EventDispatcher.h b/src/kiwano/core/EventDispatcher.h index 741ac100..2eff0bf2 100644 --- a/src/kiwano/core/EventDispatcher.h +++ b/src/kiwano/core/EventDispatcher.h @@ -51,9 +51,10 @@ public: /// @brief 添加监听器 /// @tparam _EventTy 事件类型 /// @param callback 回调函数 - template ::value, int>::type> + template EventListener* AddListener(EventListener::Callback callback) { + static_assert(kiwano::IsEvent<_EventTy>::value, "_EventTy is not an event type."); return AddListener(KGE_EVENT(_EventTy), callback); } @@ -62,9 +63,10 @@ public: /// @tparam _EventTy 事件类型 /// @param name 监听器名称 /// @param callback 回调函数 - template ::value, int>> + template EventListener* AddListener(const String& name, EventListener::Callback callback) { + static_assert(kiwano::IsEvent<_EventTy>::value, "_EventTy is not an event type."); return AddListener(name, KGE_EVENT(_EventTy), callback); } diff --git a/src/kiwano/core/EventListener.h b/src/kiwano/core/EventListener.h index bdbcece7..2aee0e19 100644 --- a/src/kiwano/core/EventListener.h +++ b/src/kiwano/core/EventListener.h @@ -71,9 +71,10 @@ public: /// @brief 创建监听器 /// @tparam _EventTy 事件类型 /// @param callback 回调函数 - template ::value, int>::type> + template static inline EventListenerPtr Create(const Callback& callback) { + static_assert(kiwano::IsEvent<_EventTy>::value, "_EventTy is not an event type."); return EventListener::Create(KGE_EVENT(_EventTy), callback); } @@ -82,9 +83,10 @@ public: /// @tparam _EventTy 事件类型 /// @param name 监听器名称 /// @param callback 回调函数 - template ::value, int>::type> + template static inline EventListenerPtr Create(const String& name, const Callback& callback) { + static_assert(kiwano::IsEvent<_EventTy>::value, "_EventTy is not an event type."); return EventListener::Create(name, KGE_EVENT(_EventTy), callback); } diff --git a/src/kiwano/core/event/Event.h b/src/kiwano/core/event/Event.h index 9288133e..09d182e4 100644 --- a/src/kiwano/core/event/Event.h +++ b/src/kiwano/core/event/Event.h @@ -55,19 +55,19 @@ public: /// \~chinese /// @brief 判断事件类型 /// @return 是否是指定事件类型 - template ::value, int>::type> + template bool IsType() const; /// \~chinese /// @brief 安全转换为其他类型事件 /// @throw std::bad_cast 类型无法转换时抛出 - template ::value, int>::type> + template const _Ty* SafeCast() const; /// \~chinese /// @brief 安全转换为其他类型事件 /// @throw std::bad_cast 类型无法转换时抛出 - template ::value, int>::type> + template _Ty* SafeCast(); private: @@ -99,22 +99,23 @@ inline const EventType& Event::GetType() const return type_; } -template +template inline bool Event::IsType() const { + static_assert(kiwano::IsEvent<_Ty>::value, "_Ty is not an event type."); return kiwano::IsEventType<_Ty>()(this); } -template +template inline const _Ty* Event::SafeCast() const { return const_cast(this)->SafeCast<_Ty>(); } -template +template inline _Ty* Event::SafeCast() { - if (!IsType<_Ty>()) + if (!this->IsType<_Ty>()) throw std::bad_cast(); return dynamic_cast<_Ty*>(this); }