/**************************************************************************** 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 #include #include #include #include #include #include "plugins/bus/BusTypes.h" namespace cc { namespace plugin { struct EventBase { const char *eventName; virtual const char *signature() = 0; }; template struct EventParam : std::conditional::value, std::true_type, std::false_type>::type { constexpr static bool IS_ENUM = std::is_enum::value; using type = typename std::conditional::type; }; template struct Char; template <> struct Char { static constexpr char VALUE{'E'}; }; template <> struct Char { static constexpr char VALUE{'?'}; }; template struct Signature { constexpr static char name() { return Char::value>::VALUE; } }; #define USE_EVENT_PARAMETER_TYPE(tp, sig_name) \ template <> \ struct EventParam : std::true_type { \ using type = tp; \ }; \ template <> \ struct Signature { \ constexpr static char name() { \ return (#sig_name)[0]; \ } \ } #define PARAMETER_TYPES(F) \ F(char, c); \ F(int8_t, b); \ F(uint8_t, B); \ F(uint16_t, S); \ F(int16_t, s); \ F(uint32_t, I); \ F(int32_t, i); \ F(uint64_t, J); \ F(int64_t, j); \ F(float, f); \ F(double, d); \ F(const char *, s); \ F(void *, v); PARAMETER_TYPES(USE_EVENT_PARAMETER_TYPE) template struct ExactSignature; template struct ExactSignature> { constexpr static char SIGNATURE[sizeof...(ARGS) + 1] = {Signature::name()..., '\0'}; }; template struct Event : EventBase { T info; explicit Event(const T &info) : info(info) {} explicit Event(T &&event) : info(std::move(event)) {} const char *signature() override { return ExactSignature::SIGNATURE; } }; template constexpr bool validateParameterType() { static_assert(EventParam::value, "invalidate parameter type"); return true; } template constexpr void validateParameterTypes(const std::tuple * /*unused*/) { std::array result = { validateParameterType()...}; } class EventBus; struct EventCallbackBase; class Listener { public: explicit Listener(BusType type); Listener(BusType type, const char *name); ~Listener(); template void receive(C callback); Listener(const Listener &) = delete; Listener &operator=(const Listener &) = delete; private: explicit Listener(EventBus *bus); EventBus *_bus{nullptr}; std::vector> _handles; friend class EventBus; #if CC_DEBUG bool _callbackAdded{false}; std::string _name; #endif }; class EventBus { public: static EventBus *acquire(BusType type); EventBus() = default; template void send(ARGS &&...args); private: void dispatch(EventBase *event); void addListener(Listener *); void removeListener(Listener *); std::vector _listeners; friend class Listener; }; template void EventBus::send(ARGS &&...args) { using arg_type = std::tuple::type>::type...>; constexpr static arg_type TMP; validateParameterTypes(&TMP); Event event(std::make_tuple::type>::type...>(std::forward(args)...)); dispatch(&event); } template struct EventCallbackImpl; template struct EventCallbackImpl { using arg_tuple_type = std::tuple; using func_type = std::function; static constexpr size_t ARG_N = sizeof...(ARGS); static constexpr char SIGNATURE[ARG_N + 1] = {Signature::name()..., '\0'}; }; template struct EventCallbackImpl> { using arg_tuple_type = std::tuple; using func_type = std::function; static constexpr size_t ARG_N = sizeof...(ARGS); static constexpr char SIGNATURE[ARG_N + 1] = {Signature::name()..., '\0'}; }; template struct EventCallbackImpl { using arg_tuple_type = std::tuple; using func_type = std::function; static constexpr size_t ARG_N = sizeof...(ARGS); static constexpr char SIGNATURE[ARG_N + 1] = {Signature::name()..., '\0'}; }; template struct EventCallbackImpl { using arg_tuple_type = std::tuple; using func_type = std::function; static constexpr size_t ARG_N = sizeof...(ARGS); static constexpr char SIGNATURE[ARG_N + 1] = {Signature::name()..., '\0'}; }; template struct FunctionSignature { using type = decltype(&T::operator()); }; template struct FunctionSignature { using type = R (*)(ARGS...); }; template struct FunctionSignature { using type = R (C::*)(ARGS...); }; template struct FunctionSignature { using type = R (C::*)(ARGS...) const; }; struct EventCallbackBase { virtual void invoke(EventBase *) = 0; virtual const char *signature() = 0; }; template struct EventCallback : EventCallbackBase { using impl = EventCallbackImpl::type>; using fn_type = typename impl::func_type; using arg_tuple_type = typename impl::arg_tuple_type; using wrap_fn_type = std::function; static constexpr size_t ARG_N = impl::ARG_N; wrap_fn_type fun; void invoke(EventBase *base) override { using event_type = Event; auto *event = reinterpret_cast(base); fun(event->info); } const char *signature() override { //return typeid(arg_tuple_type).name(); return impl::SIGNATURE; } }; template void callWithTuple(F func, const std::tuple &args, const std::index_sequence & /*unused*/) { func(std::get(args)...); } template void Listener::receive(C callback) { using CallbackType = EventCallback; using tuple_arg_type = typename CallbackType::arg_tuple_type; typename CallbackType::fn_type func = callback; #if CC_DEBUG assert(!_callbackAdded); // callback should be add only once! _callbackAdded = true; #endif auto wrap = [=](const tuple_arg_type &arg) { callWithTuple(func, arg, std::make_index_sequence{}); }; auto context = std::make_shared(); context->fun = wrap; _handles.emplace_back(std::move(context)); } template void send(cc::plugin::BusType bus, ARGS &&...args) { cc::plugin::EventBus::acquire(bus)->send(std::forward(args)...); } } // namespace plugin } // namespace cc