use static_assert instead of template meta programming
This commit is contained in:
		
							parent
							
								
									54b8ab0a4d
								
							
						
					
					
						commit
						6a10575860
					
				|  | @ -51,9 +51,10 @@ public: | ||||||
|     /// @brief 添加监听器
 |     /// @brief 添加监听器
 | ||||||
|     /// @tparam _EventTy 事件类型
 |     /// @tparam _EventTy 事件类型
 | ||||||
|     /// @param callback 回调函数
 |     /// @param callback 回调函数
 | ||||||
|     template <typename _EventTy, typename = typename std::enable_if<IsEvent<_EventTy>::value, int>::type> |     template <typename _EventTy> | ||||||
|     EventListener* AddListener(EventListener::Callback callback) |     EventListener* AddListener(EventListener::Callback callback) | ||||||
|     { |     { | ||||||
|  |         static_assert(kiwano::IsEvent<_EventTy>::value, "_EventTy is not an event type."); | ||||||
|         return AddListener(KGE_EVENT(_EventTy), callback); |         return AddListener(KGE_EVENT(_EventTy), callback); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  | @ -62,9 +63,10 @@ public: | ||||||
|     /// @tparam _EventTy 事件类型
 |     /// @tparam _EventTy 事件类型
 | ||||||
|     /// @param name 监听器名称
 |     /// @param name 监听器名称
 | ||||||
|     /// @param callback 回调函数
 |     /// @param callback 回调函数
 | ||||||
|     template <typename _EventTy, typename = typename std::enable_if<IsEvent<_EventTy>::value, int>> |     template <typename _EventTy> | ||||||
|     EventListener* AddListener(const String& name, EventListener::Callback callback) |     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); |         return AddListener(name, KGE_EVENT(_EventTy), callback); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -71,9 +71,10 @@ public: | ||||||
|     /// @brief 创建监听器
 |     /// @brief 创建监听器
 | ||||||
|     /// @tparam _EventTy 事件类型
 |     /// @tparam _EventTy 事件类型
 | ||||||
|     /// @param callback 回调函数
 |     /// @param callback 回调函数
 | ||||||
|     template <typename _EventTy, typename = typename std::enable_if<IsEvent<_EventTy>::value, int>::type> |     template <typename _EventTy> | ||||||
|     static inline EventListenerPtr Create(const Callback& callback) |     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); |         return EventListener::Create(KGE_EVENT(_EventTy), callback); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  | @ -82,9 +83,10 @@ public: | ||||||
|     /// @tparam _EventTy 事件类型
 |     /// @tparam _EventTy 事件类型
 | ||||||
|     /// @param name 监听器名称
 |     /// @param name 监听器名称
 | ||||||
|     /// @param callback 回调函数
 |     /// @param callback 回调函数
 | ||||||
|     template <typename _EventTy, typename = typename std::enable_if<IsEvent<_EventTy>::value, int>::type> |     template <typename _EventTy> | ||||||
|     static inline EventListenerPtr Create(const String& name, const Callback& callback) |     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); |         return EventListener::Create(name, KGE_EVENT(_EventTy), callback); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -55,19 +55,19 @@ public: | ||||||
|     /// \~chinese
 |     /// \~chinese
 | ||||||
|     /// @brief 判断事件类型
 |     /// @brief 判断事件类型
 | ||||||
|     /// @return 是否是指定事件类型
 |     /// @return 是否是指定事件类型
 | ||||||
|     template <typename _Ty, typename = typename std::enable_if<std::is_base_of<Event, _Ty>::value, int>::type> |     template <typename _Ty> | ||||||
|     bool IsType() const; |     bool IsType() const; | ||||||
| 
 | 
 | ||||||
|     /// \~chinese
 |     /// \~chinese
 | ||||||
|     /// @brief 安全转换为其他类型事件
 |     /// @brief 安全转换为其他类型事件
 | ||||||
|     /// @throw std::bad_cast 类型无法转换时抛出
 |     /// @throw std::bad_cast 类型无法转换时抛出
 | ||||||
|     template <typename _Ty, typename = typename std::enable_if<std::is_base_of<Event, _Ty>::value, int>::type> |     template <typename _Ty> | ||||||
|     const _Ty* SafeCast() const; |     const _Ty* SafeCast() const; | ||||||
| 
 | 
 | ||||||
|     /// \~chinese
 |     /// \~chinese
 | ||||||
|     /// @brief 安全转换为其他类型事件
 |     /// @brief 安全转换为其他类型事件
 | ||||||
|     /// @throw std::bad_cast 类型无法转换时抛出
 |     /// @throw std::bad_cast 类型无法转换时抛出
 | ||||||
|     template <typename _Ty, typename = typename std::enable_if<std::is_base_of<Event, _Ty>::value, int>::type> |     template <typename _Ty> | ||||||
|     _Ty* SafeCast(); |     _Ty* SafeCast(); | ||||||
| 
 | 
 | ||||||
| private: | private: | ||||||
|  | @ -99,22 +99,23 @@ inline const EventType& Event::GetType() const | ||||||
|     return type_; |     return type_; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| template <typename _Ty, typename> | template <typename _Ty> | ||||||
| inline bool Event::IsType() const | inline bool Event::IsType() const | ||||||
| { | { | ||||||
|  |     static_assert(kiwano::IsEvent<_Ty>::value, "_Ty is not an event type."); | ||||||
|     return kiwano::IsEventType<_Ty>()(this); |     return kiwano::IsEventType<_Ty>()(this); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| template <typename _Ty, typename> | template <typename _Ty> | ||||||
| inline const _Ty* Event::SafeCast() const | inline const _Ty* Event::SafeCast() const | ||||||
| { | { | ||||||
|     return const_cast<Event*>(this)->SafeCast<_Ty>(); |     return const_cast<Event*>(this)->SafeCast<_Ty>(); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| template <typename _Ty, typename> | template <typename _Ty> | ||||||
| inline _Ty* Event::SafeCast() | inline _Ty* Event::SafeCast() | ||||||
| { | { | ||||||
|     if (!IsType<_Ty>()) |     if (!this->IsType<_Ty>()) | ||||||
|         throw std::bad_cast(); |         throw std::bad_cast(); | ||||||
|     return dynamic_cast<_Ty*>(this); |     return dynamic_cast<_Ty*>(this); | ||||||
| } | } | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue