/**************************************************************************** Copyright (c) 2022-2023 Xiamen Yaji Software Co., Ltd. http://www.cocos.com Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ****************************************************************************/ #pragma once #include #include #include #include namespace cc { namespace event { namespace intl { template struct hash_calc { // NOLINT static constexpr size_t apply(const char (&str)[N]) { return (hash_calc::apply(str) ^ str[I]) * 16777619U; }; }; template struct hash_calc { // NOLINT static constexpr size_t apply(const char (&/*used*/)[N]) { return 2166136261U; }; }; template constexpr size_t hash(const char (&str)[N]) { return hash_calc::apply(str); } template struct HeadType; template struct HeadType { using head = Head; using remain = HeadType; }; template struct HeadType { using head = Head; using remain = HeadType<>; }; template <> struct HeadType<> { using head = void; using remain = HeadType<>; }; template constexpr bool validateParameters() { return true; } template constexpr bool validateParameters(Head && /*unused*/) { using element_t = std::remove_reference_t>; using head = std::remove_reference_t; constexpr bool assignable = std::is_assignable_v || std::is_convertible_v; constexpr bool ret = assignable; static_assert(ret, "Parameter type incorrect"); return ret; } template constexpr bool validateParameters(Head &&head, ARGS &&...remain) { return validateParameters(std::forward(head)) && validateParameters(std::forward(remain)...); } template struct TupleExtractor { using func_type = void(); }; template struct TupleExtractor> { using func_type = void(ARGS...); using std_func_type = std::function; }; template struct FunctionTrait : public FunctionTrait { constexpr static bool IS_LAMBDA = true; }; template struct FunctionTrait { constexpr static bool IS_LAMBDA = false; using function_type = R (C::*)(ARGS...); }; template struct FunctionTrait { constexpr static bool IS_LAMBDA = false; using function_type = R (C::*)(ARGS...) const; }; template struct FunctionTrait { using function_type = R (*)(ARGS...); constexpr static bool IS_LAMBDA = false; }; template struct FunctionTrait { using function_type = R(ARGS...); constexpr static bool IS_LAMBDA = false; }; template struct FunctionTrait> { using function_type = std::function; constexpr static bool IS_LAMBDA = false; }; template struct RemoveClass { using type = T; }; template struct RemoveClass { using type = std::function; }; template struct RemoveClass { using type = std::function; }; template using function_type_t = typename FunctionTrait::function_type; template using lambda_without_class_t = typename RemoveClass>::type; template inline auto convertLambda(F &&lambda) { if constexpr (FunctionTrait::IS_LAMBDA) { return static_cast>(std::forward(lambda)); } else { return static_cast>(std::forward(lambda)); } } template struct TgtEvtFnTrait; template struct TgtEvtFnTrait { using param0_type = C; using src_func_type = R(C *, ARGS...); constexpr static bool IS_MEMBER_FUNC = false; template static constexpr bool accept() { if constexpr (sizeof...(ARGS) == 0) { using event_type = typename TgtEvent::EventType; return std::is_same_v; } else { return false; } } template static auto wrap(src_func_type func) { using event_type = typename TgtEvent::EventType; using persist_function_type = typename TgtEvent::_persist_function_type; using emitter_type = typename TgtEvent::_emitter_type; persist_function_type ret; if constexpr (accept()) { ret = [&func](emitter_type * /*self*/, event_type *event) { func(event); }; } else { static_assert(std::is_same_v, "mismatch emitter type"); ret = [func](emitter_type *self, event_type *event) { return std::apply([self, func](auto &&...args) { func(self, args...); }, event->args); }; } return ret; } }; template struct TgtEvtFnTrait { using param0_type = C; using src_func_type = R(const C *, ARGS...); constexpr static bool IS_MEMBER_FUNC = false; template static constexpr bool accept() { if constexpr (sizeof...(ARGS) == 0) { using event_type = typename TgtEvent::EventType; return std::is_same_v; } else { return false; } } template static auto wrap(src_func_type func) { using event_type = typename TgtEvent::EventType; using emitter_type = typename TgtEvent::_emitter_type; using persist_function_type = typename TgtEvent::_persist_function_type; persist_function_type ret; if constexpr (accept()) { ret = [func](emitter_type * /*self*/, event_type *event) { func(event); }; } else { static_assert(std::is_same_v, "mismatch emitter type"); ret = [func](emitter_type *self, event_type *event) { return std::apply([self, func](auto &&...args) { func(self, args...); }, event->args); }; } return ret; } }; template struct TgtEvtFnTrait { using param0_type = C; using src_func_type = R (*)(C *, ARGS...); constexpr static bool IS_MEMBER_FUNC = false; template static constexpr bool accept() { if constexpr (sizeof...(ARGS) == 0) { using event_type = typename TgtEvent::EventType; return std::is_same_v; } else { return false; } } template static auto wrap(src_func_type func) { using EventType = typename TgtEvent::EventType; using persist_function_type = typename TgtEvent::_persist_function_type; using emitter_type = typename TgtEvent::_emitter_type; persist_function_type ret; if constexpr (accept()) { auto ret2 = [func](emitter_type * /*self*/, EventType *event) { func(event); }; ret = ret2; } else { static_assert(std::is_same_v, "mismatch emitter type"); ret = [func](emitter_type *self, EventType *event) { return std::apply([self, func](auto &&...args) { func(self, args...); }, event->args); }; } return ret; } }; template struct TgtEvtFnTrait { using src_func_type = R (*)(const C *, ARGS...); using param0_type = C; constexpr static bool IS_MEMBER_FUNC = false; template static constexpr bool accept() { if constexpr (sizeof...(ARGS) == 0) { using event_type = typename TgtEvent::EventType; return std::is_same_v; } else { return false; } } template static auto wrap(src_func_type func) { using event_type = typename TgtEvent::EventType; using emitter_type = typename TgtEvent::_emitter_type; using persist_function_type = typename TgtEvent::_persist_function_type; persist_function_type ret; if constexpr (accept()) { ret = [&func](emitter_type * /*self*/, event_type *event) { func(event); }; return ret; } else { static_assert(std::is_same_v, "mismatch emitter type"); ret = [func](emitter_type *self, event_type *event) { return std::apply([self, func](auto &&...args) { func(self, args...); }, event->args); }; } return ret; } }; template struct TgtEvtFnTrait> { using src_func_type = std::function; using param0_type = C; constexpr static bool IS_MEMBER_FUNC = false; template static constexpr bool accept() { if constexpr (sizeof...(ARGS) == 0) { using event_type = typename TgtEvent::EventType; return std::is_same_v; } else { return false; } } template static auto wrap(src_func_type func) { using event_type = typename TgtEvent::EventType; using emitter_type = typename TgtEvent::_emitter_type; using persist_function_type = typename TgtEvent::_persist_function_type; persist_function_type ret; if constexpr (accept()) { ret = [&func](emitter_type * /*self*/, event_type *event) { func(event); }; } else { static_assert(std::is_same_v, "mismatch emitter type"); ret = [func](emitter_type *self, event_type *event) { return std::apply([self, func](auto &&...args) { func(self, args...); }, event->args); }; } return ret; } }; template struct TgtEvtFnTrait> { using param0_type = C; using src_func_type = std::function; constexpr static bool IS_MEMBER_FUNC = false; template static constexpr bool accept() { if constexpr (sizeof...(ARGS) == 0) { using EventType = typename TgtEvent::EventType; return std::is_same_v; } else { return false; } } template static auto wrap(src_func_type func) { using event_type = typename TgtEvent::EventType; using emitter_type = typename TgtEvent::_emitter_type; using persist_function_type = typename TgtEvent::_persist_function_type; persist_function_type ret; if constexpr (accept()) { ret = [func](emitter_type * /*self*/, event_type *event) { func(event); }; } else { static_assert(std::is_same_v, "mismatch emitter type"); ret = [func](emitter_type *self, event_type *event) { return std::apply([self, func](auto &&...args) { func(self, args...); }, event->args); }; } return ret; } }; template struct TgtEvtFnTrait { using src_func_type = R (C::*)(ARGS...); using context_type = C; using param0_type = typename intl::HeadType::head; constexpr static bool IS_MEMBER_FUNC = true; template static constexpr bool accept() { if constexpr (sizeof...(ARGS) == 1) { using event_type = typename TgtEvent::EventType; return std::is_same_v; } else { return false; } } template static auto wrap(src_func_type func) { using event_type = typename TgtEvent::EventType; using emitter_type = typename TgtEvent::_emitter_type; using persist_function_type = typename TgtEvent::_persist_function_type; persist_function_type ret; static_assert(std::is_same_v, "mismatch emitter type"); if constexpr (accept()) { ret = [&func](emitter_type *self, event_type *event) { (self->*func)(event); }; } else { ret = [func](emitter_type *self, event_type *event) { return std::apply([&self, func](auto &&...args) { (self->*func)(args...); }, event->args); }; } return ret; } template static auto wrapWithContext(src_func_type func, context_type *ctx) { using emitter_type = typename TgtEvent::_emitter_type; using event_type = typename TgtEvent::EventType; using persist_function_type = typename TgtEvent::_persist_function_type; persist_function_type ret; if constexpr (accept()) { ret = [func, ctx](emitter_type * /*self*/, event_type *event) { (ctx->*func)(event); }; } else { ret = [func, ctx](emitter_type * /*self*/, event_type *event) { return std::apply([ctx, func](auto... args) { (ctx->*func)(args...); }, event->args); }; } return ret; } }; template struct TgtEvtFnTrait { using src_func_type = R (C::*)(ARGS...) const; using context_type = C; using param0_type = typename intl::HeadType::head; constexpr static bool IS_MEMBER_FUNC = true; template static constexpr bool accept() { if constexpr (sizeof...(ARGS) == 1) { using event_type = typename TgtEvent::EventType; return std::is_same_v; } else { return false; } } template static auto wrap(src_func_type func) { using EventType = typename TgtEvent::EventType; using persist_function_type = typename TgtEvent::_persist_function_type; using emitter_type = typename TgtEvent::_emitter_type; persist_function_type ret; static_assert(std::is_same_v, "mismatch emitter type"); if constexpr (accept()) { ret = [&func](context_type *self, EventType *event) { (self->*func)(event); }; } else { ret = [&func](context_type *self, EventType *event) { return std::apply([self, func](auto &&...args) { (self->*func)(args...); }, event->args); }; } return ret; } template static auto wrapWithContext(src_func_type func, context_type *ctx) { using emitter_type = typename TgtEvent::_emitter_type; using event_type = typename TgtEvent::EventType; using persist_function_type = typename TgtEvent::_persist_function_type; persist_function_type ret = [func, ctx](emitter_type * /*self*/, event_type *event) { return std::apply([ctx, func](auto &&...args) { (ctx->*func)(args...); }, event->args); }; return ret; } }; template constexpr T addAll(T first) { return first; } template constexpr T addAll(T first, Others... others) { return first + addAll(others...); } template constexpr int TotalEvents = addAll(Parents::getEventCount()...); // NOLINT } // namespace intl } // namespace event } // namespace cc