no message

This commit is contained in:
gem
2025-02-18 15:21:31 +08:00
commit 2d133e56d7
1980 changed files with 465595 additions and 0 deletions

55
cocos/core/event/Event.h Normal file
View File

@@ -0,0 +1,55 @@
/****************************************************************************
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
#ifdef SWIGCOCOS
#define IMPL_EVENT_TARGET_WITH_PARENT(...)
#define IMPL_EVENT_TARGET(...)
#define DECLARE_TARGET_EVENT_BEGIN(...)
#define DECLARE_TARGET_EVENT_BEGIN_WITH_PARENTS(...)
#define DECLARE_TARGET_EVENT_END(...)
#define TARGET_EVENT_ARG0(...)
#define TARGET_EVENT_ARG1(...)
#define TARGET_EVENT_ARG2(...)
#define TARGET_EVENT_ARG3(...)
#define TARGET_EVENT_ARG4(...)
#define TARGET_EVENT_ARG5(...)
#define TARGET_EVENT_ARG6(...)
#define TARGET_EVENT_ARG7(...)
#define TARGET_EVENT_ARG8(...)
#define TARGET_EVENT_ARG9(...)
#define TARGET_EVENT_ARG10(...)
#define TARGET_EVENT_ARG11(...)
#define TARGET_EVENT_ARG12(...)
#define TARGET_EVENT_ARG13(...)
#define TARGET_EVENT_ARG14(...)
#define TARGET_EVENT_ARG15(...)
#define TARGET_EVENT_ARG16(...)
#define TARGET_EVENT_ARG17(...)
#define TARGET_EVENT_ARG18(...)
#define TARGET_EVENT_ARG19(...)
#define TARGET_EVENT_ARG20(...)
#else
#include "EventBus.h"
#include "EventTarget.h"
#endif

View File

@@ -0,0 +1,62 @@
/****************************************************************************
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.
****************************************************************************/
#include "EventBus.h"
#include <algorithm>
#include "intl/List.h"
namespace cc {
namespace event {
void BusEventListenerContainer::addListener(BusEventListenerBase *listener) {
if (_isBroadcasting) {
intl::listAppend(&_listenersToAdd, listener->entry);
return;
}
intl::listAppend(&_listenerList, listener->entry);
}
void BusEventListenerContainer::removeListener(BusEventListenerBase *listener) {
if (_isBroadcasting) {
_listenersToRemove.emplace_back(listener->entry);
listener->entry->listener = nullptr;
return;
}
intl::detachFromList(&_listenerList, listener->entry);
delete listener->entry;
}
void BusEventListenerContainer::addOrRemovePendingListeners() {
for (auto &entry : _listenersToRemove) {
intl::detachFromList(&_listenerList, entry);
delete entry;
}
EVENT_LIST_LOOP_REV_BEGIN(curr, _listenersToAdd)
intl::detachFromList(&_listenersToAdd, curr);
intl::listAppend(&_listenerList, curr);
EVENT_LIST_LOOP_REV_END(curr, _listenersToAdd)
_listenersToAdd = nullptr;
_listenersToRemove.clear();
}
} // namespace event
} // namespace cc

238
cocos/core/event/EventBus.h Normal file
View File

@@ -0,0 +1,238 @@
/****************************************************************************
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 <cstddef>
#include <iostream>
#include <memory>
#include <type_traits>
#include "base/Log.h"
#include "base/std/container/vector.h"
#include "core/memop/Pool.h"
#include "intl/EventIntl.h"
#include "intl/List.h"
namespace cc {
namespace event {
class BusEventListenerBase;
template <typename EHandler>
class BusEventListenerDB;
template <typename EHandler, typename... ARGS>
class BusEventBroadcaster;
class BusEventListenerContainer;
struct BusEventListenerEntry {
BusEventListenerEntry *next{nullptr};
BusEventListenerEntry *prev{nullptr};
BusEventListenerBase *listener{nullptr};
};
class BusEventListenerBase {
protected:
BusEventListenerEntry *entry{nullptr}; // NOLINT
friend class BusEventListenerContainer;
};
class BusEventListenerContainer {
public:
template <typename EHandler, typename... ARGS>
bool broadcast(ARGS &&...args);
protected:
BusEventListenerContainer() = default;
virtual ~BusEventListenerContainer() = default;
void addListener(BusEventListenerBase *);
void removeListener(BusEventListenerBase *);
bool hasPendingListeners() const {
return !_listenersToRemove.empty() || _listenersToAdd;
}
void addOrRemovePendingListeners();
// fields
BusEventListenerEntry *_listenerList{nullptr};
BusEventListenerEntry *_listenersToAdd{nullptr};
ccstd::vector<BusEventListenerEntry *> _listenersToRemove;
int _isBroadcasting = 0;
template <typename T>
friend class BusEventListenerDB;
template <typename EHandler, typename... ARGS>
friend class BusEventBroadcaster;
friend class BusEventListenerBase;
template <typename E>
friend class Listener;
};
template <typename EHandler, typename... ARGS>
class BusEventBroadcaster final : public BusEventListenerContainer {
public:
bool doBroadcast(ARGS &&...args);
};
template <typename EHandler, typename... ARGS>
bool BusEventListenerContainer::broadcast(ARGS &&...args) {
using BusType = BusEventBroadcaster<EHandler, ARGS...>;
_isBroadcasting++;
auto ret = static_cast<BusType *>(this)->doBroadcast(std::forward<ARGS>(args)...);
_isBroadcasting--;
if (!_isBroadcasting && hasPendingListeners()) {
addOrRemovePendingListeners();
}
return false;
}
template <typename EHandler>
class BusEventListenerDB final {
public:
static BusEventListenerContainer *container() {
if (ctn == nullptr) {
ctn = new BusEventListenerContainer;
}
return ctn;
}
private:
static BusEventListenerContainer *ctn;
};
template <typename EHandler>
BusEventListenerContainer *BusEventListenerDB<EHandler>::ctn = nullptr;
template <typename BusType, typename R, typename... ARGS>
struct BusEventTrait {
using _bus_type = BusType;
using _return_type = R;
using _argument_tuple_types = std::tuple<ARGS...>;
constexpr static int ARG_COUNT = sizeof...(ARGS);
};
/**
* Bus Event Listener
*/
template <typename EHandler>
class Listener : public BusEventListenerBase {
public:
using _agurment_tuple_types = typename EHandler::_argument_tuple_types;
using _argument_wrapper = typename intl::TupleExtractor<_agurment_tuple_types>;
// using func_type = typename _argument_wrapper::func_type;
using _std_func_type = typename _argument_wrapper::std_func_type;
constexpr static const char *BUS_NAME = EHandler::BUS_NAME;
constexpr static const char *HANDLE_CLASS = EHandler::HANDLE_CLASS;
Listener();
~Listener();
inline bool isEnabled() const { return _enabled; }
inline void enable() { _enabled = true; }
inline void disable() { _enabled = false; }
inline void reset() { _callback = nullptr; }
template <typename Fn>
bool bind(Fn &&func) {
_callback = intl::convertLambda(std::forward<Fn>(func));
return true;
}
template <typename... ARGS>
void invoke(ARGS &&...args) {
if (_callback && _enabled) {
_callback(std::forward<ARGS>(args)...);
} else {
CC_LOG_DEBUG("EventBus[%s] has no listener found!", BUS_NAME);
}
}
const char *getBusName() const { return BUS_NAME; }
const char *getHandlerName() const { return HANDLE_CLASS; }
private:
bool _enabled{true};
_std_func_type _callback;
friend class BusEventListenerContainer;
};
template <typename EHandler>
Listener<EHandler>::Listener() {
entry = new BusEventListenerEntry;
entry->listener = this;
BusEventListenerDB<EHandler>::container()->addListener(this);
}
template <typename EHandler>
Listener<EHandler>::~Listener() {
BusEventListenerDB<EHandler>::container()->removeListener(this);
}
template <typename EHandler, typename... ARGS>
bool BusEventBroadcaster<EHandler, ARGS...>::doBroadcast(ARGS &&...args) {
// broadcast events to all listeners
EVENT_LIST_LOOP_BEGIN(curr, _listenerList)
if (curr->listener) {
static_cast<Listener<EHandler> *>(curr->listener)->invoke(std::forward<ARGS>(args)...);
}
EVENT_LIST_LOOP_END(curr, _listenerList)
return true;
}
template <typename EHandler, typename... ARGS>
void broadcast(ARGS &&...args) {
static_assert(sizeof...(ARGS) == EHandler::ARG_COUNT, "parameter count incorrect");
event::intl::validateParameters<0, EHandler, ARGS...>(std::forward<ARGS>(args)...);
auto *listenerSet = BusEventListenerDB<EHandler>::container();
listenerSet->template broadcast<EHandler, ARGS...>(std::forward<ARGS>(args)...);
}
} // namespace event
} // namespace cc
// NOLINTNEXTLINE
#define EventBusName_(n) n##_ebus
#define DECLARE_EVENT_BUS(EventBusClass) \
struct EventBusClass##_ebus { \
constexpr static const char *BUS_NAME = #EventBusClass; \
};
// NOLINTNEXTLINE
#define _DECLARE_BUS_EVENT_VA(BusEventClass, EventBusClass, ...) \
struct BusEventClass final : cc::event::BusEventTrait<EventBusName_(EventBusClass), void, __VA_ARGS__> { \
using BusType = EventBusName_(EventBusClass); \
using Listener = cc::event::Listener<BusEventClass>; \
constexpr static const char *BUS_NAME = BusType::BUS_NAME; \
constexpr static const char *HANDLE_CLASS = #BusEventClass; \
constexpr static size_t TypeID() { \
return cc::event::intl::hash(#BusEventClass); \
} \
template <typename... ARGS> \
static inline void emit(ARGS &&...args) { \
cc::event::emit<BusEventClass>(std::forward<ARGS>(args)...); \
} \
};
#include "intl/EventBusMacros.h"

View File

@@ -0,0 +1,715 @@
/****************************************************************************
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 <array>
#include <cstring>
#include <memory>
#include "base/Log.h"
#include "base/Macros.h"
#include "base/memory/Memory.h"
#include "intl/EventIntl.h"
#include "intl/List.h"
namespace cc {
namespace event {
class TgtEventTraitClass {};
enum class EventPhaseType {
CAPTUREING_PHASE = 1,
AT_TARGET = 2,
BUBBLING_PHASE = 3,
UNKNOWN = 4,
};
struct TgtEventInfo {
EventPhaseType eventPhase{EventPhaseType::UNKNOWN};
bool bubbles{true};
bool cancelable{true};
void stopPropagation() {
propagationStopped = true;
}
void preventDefault() { /* TODO: */
}
bool propagationStopped{false};
};
template <typename TgtEvent>
struct Event final : TgtEventInfo {
using EmitterType = typename TgtEvent::_emitter_type;
using _argument_tuple_types = typename TgtEvent::_argument_tuple_types;
using _argument_local_types = typename TgtEvent::_argument_local_types;
EmitterType *target{nullptr};
EmitterType *currentTarget{nullptr};
_argument_local_types args;
Event() = default;
explicit Event(const _argument_local_types &argsIn) : args(argsIn) {
}
template <size_t N>
auto get() const {
return std::get<N>(args);
}
template <size_t N>
auto get() {
return std::get<N>(args);
}
template <size_t N, typename A>
void set(A &&value) {
std::get<N>(args) = value;
}
void initEvent(bool canBubbleArg, bool cancelableArg) {
// TODO()
}
template <typename... ARGS>
void update(ARGS &&...values) {
args = std::make_tuple<>(std::forward<ARGS>(values)...);
}
};
template <typename EmitterType, typename... ARGS>
class TgtEventTrait : public TgtEventTraitClass {
public:
using _emitter_type = EmitterType;
using _argument_tuple_types = std::tuple<ARGS...>;
using _argument_local_types = std::tuple<std::remove_reference_t<std::remove_cv_t<ARGS>>...>;
constexpr static int ARG_COUNT = sizeof...(ARGS);
};
class TgtMemberFnCmp {
public:
virtual ~TgtMemberFnCmp() = default;
virtual void *getContext() = 0;
virtual bool equals(TgtMemberFnCmp *) const = 0;
};
template <typename Fn>
class TgtMemberHandleFn final : public TgtMemberFnCmp {
public:
~TgtMemberHandleFn() override = default;
void *getContext() override { return context; }
bool equals(TgtMemberFnCmp *other) const override {
auto *fake = reinterpret_cast<TgtMemberHandleFn *>(other);
return context == fake->context && func == fake->func;
}
Fn func;
void *context;
};
class TargetEventListenerBase {
public:
enum class RunState {
NORMAL,
PENDING_ONCE,
ONCE_DONE,
};
virtual ~TargetEventListenerBase() = default;
virtual void *getContext() const = 0;
virtual const char *getEventName() const { return nullptr; }
bool isEnabled() const { return _enabled; }
void setOnce() { _state = RunState::PENDING_ONCE; }
inline size_t getEventTypeID() const { return _eventTypeID; }
int32_t id{-1};
TargetEventListenerBase *next{nullptr};
TargetEventListenerBase *prev{nullptr};
protected:
bool _enabled = true;
RunState _state{RunState::NORMAL};
size_t _eventTypeID;
};
template <typename TgtEvent>
class TargetEventListener final : public TargetEventListenerBase {
public:
using _emitter_type = typename TgtEvent::_emitter_type;
using EventType = typename TgtEvent::EventType;
using _persist_function_type = typename TgtEvent::_persist_function_type;
explicit TargetEventListener(_persist_function_type func) : _func(func) {
_eventTypeID = TgtEvent::TYPE_ID;
}
~TargetEventListener() override {
delete _fnCmptor;
}
template <typename Fn>
void setMemberFuncAddr(Fn func, void *context) {
auto fctx = new TgtMemberHandleFn<Fn>;
fctx->func = func;
fctx->context = context;
_fnCmptor = fctx;
}
void *getContext() const override {
if (_fnCmptor) return _fnCmptor->getContext();
return nullptr;
}
const char *getEventName() const override {
return TgtEvent::EVENT_NAME;
}
void apply(_emitter_type *self, EventType *evobj) {
switch (_state) {
case RunState::ONCE_DONE:
return;
case RunState::PENDING_ONCE:
_state = RunState::ONCE_DONE;
break;
default:
break;
}
_func(self, evobj);
}
protected:
_persist_function_type _func;
TgtMemberFnCmp *_fnCmptor{nullptr};
};
using TargetEventIdType = int32_t;
template <typename TgtEvent>
class TargetEventID final {
public:
using HandleType = TgtEvent;
using IdType = TargetEventIdType;
TargetEventID() = default;
TargetEventID(IdType eventId) : _eventId(eventId) {} // NOLINT
TargetEventID(const TargetEventID &) = default;
TargetEventID(TargetEventID &&) noexcept = default;
TargetEventID &operator=(const TargetEventID &) = default;
TargetEventID &operator=(TargetEventID &&) noexcept = default;
IdType value() { return _eventId; }
private:
IdType _eventId{};
};
template <size_t N>
struct TargetListenerContainer final {
std::array<TargetEventListenerBase *, N> data{nullptr};
inline TargetEventListenerBase *&operator[](size_t index) { return data[index]; }
void clear() {
for (size_t i = 0; i < N; i++) {
auto &handlers = data[i];
EVENT_LIST_LOOP_REV_BEGIN(handle, handlers)
delete handle;
EVENT_LIST_LOOP_REV_END(handle, handlers)
data[i] = nullptr;
}
}
~TargetListenerContainer() {
clear();
}
};
template <typename Self, size_t EventCount, bool hasParent>
class EventTargetImpl final {
public:
constexpr static bool HAS_PARENT = hasParent;
protected:
template <typename TgtEvent, typename Fn>
TargetEventID<TgtEvent> addEventListener(Fn &&func, bool useCapture, bool once) {
/* CC_ASSERT(!_emittingEvent); */
using func_type = std::conditional_t<intl::FunctionTrait<Fn>::IS_LAMBDA,
typename intl::lambda_without_class_t<Fn>, Fn>;
using wrap_type = intl::TgtEvtFnTrait<func_type>;
auto stdfn = wrap_type::template wrap<TgtEvent>(intl::convertLambda(std::forward<Fn>(func)));
auto *newHandler = new TargetEventListener<TgtEvent>(stdfn);
auto newId = ++_handlerId;
newHandler->id = newId;
if (once) {
newHandler->setOnce();
}
if constexpr (wrap_type::IS_MEMBER_FUNC) {
newHandler->setMemberFuncAddr(std::forward<Fn>(func), nullptr);
}
if (useCapture) {
intl::listAppend<TargetEventListenerBase>(&_capturingHandlers[TgtEvent::TYPE_ID], newHandler);
} else {
intl::listAppend<TargetEventListenerBase>(&_bubblingHandlers[TgtEvent::TYPE_ID], newHandler);
}
return TargetEventID<TgtEvent>(newId);
}
template <typename TgtEvent, typename Fn, typename O>
TargetEventID<TgtEvent> addEventListener(Fn &&func, O *ctx, bool useCapture, bool once) {
/* CC_ASSERT(!_emittingEvent);*/
using wrap_type = intl::TgtEvtFnTrait<Fn>;
auto stdfn = wrap_type::template wrapWithContext<TgtEvent>(std::forward<Fn>(func), ctx);
auto *newHandler = new TargetEventListener<TgtEvent>(stdfn);
auto newId = ++_handlerId;
newHandler->id = newId;
if (once) {
newHandler->setOnce();
}
if constexpr (wrap_type::IS_MEMBER_FUNC) {
newHandler->setMemberFuncAddr(std::forward<Fn>(func), ctx);
}
if (useCapture) {
intl::listAppend<TargetEventListenerBase>(&_capturingHandlers[TgtEvent::TYPE_ID], newHandler);
} else {
intl::listAppend<TargetEventListenerBase>(&_bubblingHandlers[TgtEvent::TYPE_ID], newHandler);
}
return TargetEventID<TgtEvent>(newId);
}
public:
template <typename TgtEvent, typename Fn>
TargetEventID<TgtEvent> once(Fn &&func, bool useCapture) {
return this->template addEventListener(std::forward<Fn>(func), useCapture, true);
}
template <typename TgtEvent, typename Fn, typename O>
TargetEventID<TgtEvent> once(Fn &&func, O *ctx) {
return this->template addEventListener(std::forward<Fn>(func), ctx, true);
}
template <typename TgtEvent, typename Fn>
TargetEventID<TgtEvent> on(Fn &&func, bool useCapture = false) {
static_assert(std::is_base_of<typename TgtEvent::_emitter_type, Self>::value, "mismatch target type");
return this->template addEventListener<TgtEvent, Fn>(std::forward<Fn>(func), useCapture, false);
}
template <typename TgtEvent, typename Fn, typename O>
TargetEventID<TgtEvent> on(Fn &&func, O *ctx, bool useCapture = false) {
return this->template addEventListener<TgtEvent, Fn, O>(std::forward<Fn>(func), ctx, useCapture, false);
}
template <typename TgtEvent>
bool off(TargetEventID<TgtEvent> eventId) {
CC_ASSERT(!_emittingEvent[TgtEvent::TYPE_ID]);
TargetEventListenerBase *&bubblingHandlers = _bubblingHandlers[TgtEvent::TYPE_ID];
EVENT_LIST_LOOP_REV_BEGIN(handle, bubblingHandlers)
if (handle && handle->id == eventId.value()) {
CC_ASSERT(handle->getEventTypeID() == TgtEvent::TYPE_ID);
intl::detachFromList(&bubblingHandlers, handle);
delete handle;
return true;
}
EVENT_LIST_LOOP_REV_END(handle, bubblingHandlers)
TargetEventListenerBase *&capturingHandlers = _capturingHandlers[TgtEvent::TYPE_ID];
EVENT_LIST_LOOP_REV_BEGIN(handle, capturingHandlers)
if (handle && handle->id == eventId.value()) {
CC_ASSERT(handle->getEventTypeID() == TgtEvent::TYPE_ID);
intl::detachFromList(&capturingHandlers, handle);
delete handle;
return true;
}
EVENT_LIST_LOOP_REV_END(handle, capturingHandlers)
return false;
}
void offAll() {
#if CC_DEBUG
for (auto &itr : _emittingEvent) {
CC_ASSERT(!itr);
}
#endif
_bubblingHandlers.clear();
_capturingHandlers.clear();
}
template <typename TgtEvent>
void off() {
static_assert(std::is_base_of_v<TgtEventTraitClass, TgtEvent>, "incorrect template argument");
CC_ASSERT(!_emittingEvent[TgtEvent::TYPE_ID]);
TargetEventListenerBase *&bubblingHandlers = _bubblingHandlers[TgtEvent::TYPE_ID];
EVENT_LIST_LOOP_REV_BEGIN(handle, bubblingHandlers)
if (handle) {
intl::detachFromList(&bubblingHandlers, handle);
delete handle;
}
EVENT_LIST_LOOP_REV_END(handle, bubblingHandlers)
TargetEventListenerBase *&capturingHandlers = _capturingHandlers[TgtEvent::TYPE_ID];
EVENT_LIST_LOOP_REV_BEGIN(handle, capturingHandlers)
if (handle) {
intl::detachFromList(&capturingHandlers, handle);
delete handle;
}
EVENT_LIST_LOOP_REV_END(handle, capturingHandlers)
}
template <typename TgtEvent, typename... ARGS>
void emit(Self *self, ARGS &&...args) { /* TODO() : statistics */
using _handler_function_type = TargetEventListener<TgtEvent>;
using EventType = typename TgtEvent::EventType;
static_assert(sizeof...(ARGS) == TgtEvent::ARG_COUNT, "Parameter count incorrect for function EventTarget::emit");
if (_bubblingHandlers[TgtEvent::TYPE_ID] == nullptr) return;
intl::validateParameters<0, TgtEvent, ARGS...>(std::forward<ARGS>(args)...);
EventType eventObj(std::make_tuple<ARGS...>(std::forward<ARGS>(args)...));
eventObj.target = self;
eventObj.currentTarget = self;
emitEvtObj<false, TgtEvent>(self, &eventObj);
}
template <bool useCapture, typename TgtEvent, typename EvtObj>
void emitEvtObj(Self *self, EvtObj *eventObj) {
using EventType = typename TgtEvent::EventType;
using _handler_function_type = TargetEventListener<TgtEvent>;
static_assert(std::is_same_v<EventType, EvtObj>, "Event type mismatch");
_emittingEvent[TgtEvent::TYPE_ID]++;
if constexpr (useCapture) {
TargetEventListenerBase *&handlers = _capturingHandlers[TgtEvent::TYPE_ID];
EVENT_LIST_LOOP_BEGIN(handle, handlers)
if (handle->isEnabled()) {
#if CC_DEBUG
bool sameAddr = handle->getEventName() == TgtEvent::EVENT_NAME;
if (!sameAddr && strcmp(handle->getEventName(), TgtEvent::EVENT_NAME)) {
// different event should not shared a same typeid.
CC_LOG_ERROR("Event '%s' and '%s' shared the same TypeID(), for event declaration of subclasses, please use DECLARE_TARGET_EVENT_BEGIN_OFFSET()", TgtEvent::EVENT_NAME, handle->getEventName());
CC_ABORT();
}
#endif
static_cast<_handler_function_type *>(handle)->apply(self, eventObj);
}
EVENT_LIST_LOOP_END(handle, handlers);
} else {
TargetEventListenerBase *&handlers = _bubblingHandlers[TgtEvent::TYPE_ID];
EVENT_LIST_LOOP_BEGIN(handle, handlers)
if (handle->isEnabled()) {
#if CC_DEBUG
bool sameAddr = handle->getEventName() == TgtEvent::EVENT_NAME;
if (!sameAddr && strcmp(handle->getEventName(), TgtEvent::EVENT_NAME)) {
// different event should not shared a same typeid.
CC_LOG_ERROR("Event '%s' and '%s' shared the same TypeID(), for event declaration of subclasses, please use DECLARE_TARGET_EVENT_BEGIN_OFFSET()", TgtEvent::EVENT_NAME, handle->getEventName());
CC_ABORT();
}
#endif
static_cast<_handler_function_type *>(handle)->apply(self, eventObj);
}
EVENT_LIST_LOOP_END(handle, handlers);
}
_emittingEvent[TgtEvent::TYPE_ID]--;
}
template <typename TgtEvent, typename EvtType>
std::enable_if_t<std::is_same_v<typename TgtEvent::EventType, std::decay_t<EvtType>>, void>
dispatchEvent(Self *self, EvtType &eventObj) {
if constexpr (HAS_PARENT) {
std::vector<Self *> parents;
Self *curr = self->evGetParent();
while (curr) {
if (curr->getEventTarget().template hasEventHandler<TgtEvent>()) {
parents.emplace_back(curr);
}
curr = curr->evGetParent();
}
for (auto itr = parents.rbegin(); itr != parents.rend(); itr++) {
eventObj.currentTarget = *itr;
(*itr)->getEventTarget().template emitEvtObj<true, TgtEvent>(*itr, &eventObj);
if (eventObj.propagationStopped) {
return;
}
}
}
eventObj.eventPhase = EventPhaseType::AT_TARGET;
eventObj.currentTarget = self;
emitEvtObj<true, TgtEvent>(self, &eventObj);
if (!eventObj.propagationStopped) {
emitEvtObj<false, TgtEvent>(self, &eventObj);
}
if constexpr (HAS_PARENT) {
if (!eventObj.propagationStopped && eventObj.bubbles) {
auto *curr = self->evGetParent();
std::vector<Self *> parents;
while (curr) {
if (curr->getEventTarget().template hasEventHandler<TgtEvent>()) {
parents.emplace_back(curr);
}
curr = curr->evGetParent();
}
for (auto itr = parents.begin(); itr != parents.end(); itr++) {
eventObj.currentTarget = *itr;
(*itr)->getEventTarget().template emitEvtObj<false, TgtEvent>(*itr, &eventObj);
if (eventObj.propagationStopped) {
return;
}
}
}
}
}
template <typename TgtEvent, typename... ARGS>
std::enable_if_t<sizeof...(ARGS) != 1 ||
(sizeof...(ARGS) == 1 &&
!std::is_same_v<typename TgtEvent::EventType,
std::remove_pointer_t<typename intl::HeadType<ARGS...>::head>>),
void>
dispatchEvent(Self *self, ARGS &&...args) {
using _handler_function_type = TargetEventListener<TgtEvent>;
using EventType = typename TgtEvent::EventType;
static_assert(sizeof...(ARGS) == TgtEvent::ARG_COUNT, "Parameter count incorrect for function EventTarget::emit");
intl::validateParameters<0, TgtEvent, ARGS...>(std::forward<ARGS>(args)...);
EventType eventObj(std::make_tuple<ARGS...>(std::forward<ARGS>(args)...));
eventObj.target = self;
eventObj.currentTarget = self;
eventObj.eventPhase = EventPhaseType::CAPTUREING_PHASE;
dispatchEvent<TgtEvent, EventType>(self, eventObj);
}
template <typename TgtEvent>
void dispatchEvent(Self *self) {
using _handler_function_type = TargetEventListener<TgtEvent>;
using EventType = typename TgtEvent::EventType;
static_assert(0 == TgtEvent::ARG_COUNT, "Parameter count incorrect for function EventTarget::emit");
EventType eventObj;
eventObj.target = self;
eventObj.currentTarget = self;
eventObj.eventPhase = EventPhaseType::CAPTUREING_PHASE;
dispatchEvent<TgtEvent, EventType>(self, eventObj);
}
template <typename TgtEvent>
bool hasEventHandler() {
TargetEventListenerBase *&bubblingHandlers = _bubblingHandlers[TgtEvent::TYPE_ID];
EVENT_LIST_LOOP_BEGIN(handle, bubblingHandlers)
if (handle->isEnabled()) {
return true;
}
EVENT_LIST_LOOP_END(handle, bubblingHandlers);
TargetEventListenerBase *&capturingHandlers = _capturingHandlers[TgtEvent::TYPE_ID];
EVENT_LIST_LOOP_BEGIN(handle, capturingHandlers)
if (handle->isEnabled()) {
return true;
}
EVENT_LIST_LOOP_END(handle, capturingHandlers);
return false;
}
template <typename TgtEvent, typename Fn, typename C>
bool hasEventHandler(Fn func, C *target) {
using wrap_type = intl::TgtEvtFnTrait<Fn>;
using _handler_function_type = event::TargetEventListener<TgtEvent>;
static_assert(std::is_same<typename wrap_type::target_type, C>::value, "member function type mismatch");
TargetEventListenerBase *&bubblingHandlers = _bubblingHandlers[TgtEvent::TYPE_ID];
EVENT_LIST_LOOP_BEGIN(handle, bubblingHandlers)
if (handle->isEnabled() && handle->getContext() == target) {
auto *ptr = static_cast<_handler_function_type *>(handle);
return ptr->getMemberFuncAddr() == func;
}
EVENT_LIST_LOOP_END(handle, bubblingHandlers);
TargetEventListenerBase *&capturingHandlers = _capturingHandlers[TgtEvent::TYPE_ID];
EVENT_LIST_LOOP_BEGIN(handle, capturingHandlers)
if (handle->isEnabled() && handle->getContext() == target) {
auto *ptr = static_cast<_handler_function_type *>(handle);
return ptr->getMemberFuncAddr() == func;
}
EVENT_LIST_LOOP_END(handle, capturingHandlers);
return false;
}
protected:
TargetListenerContainer<EventCount> _bubblingHandlers;
TargetListenerContainer<EventCount> _capturingHandlers;
TargetEventIdType _handlerId{1};
std::array<int, EventCount> _emittingEvent{0};
};
template <typename T>
class HeapObject final {
public:
HeapObject() {
_data = ccnew T;
}
~HeapObject() {
delete _data;
}
inline T &get() {
return *_data;
}
private:
T *_data{nullptr};
};
template <typename Self, size_t EventCnt, bool HAS_PARENT>
using EventTargetGuard = HeapObject<EventTargetImpl<Self, EventCnt, HAS_PARENT>>;
} // namespace event
} // namespace cc
#define TARGET_EVENT_ARG0(EventTypeClass) \
class EventTypeClass final : public cc::event::TgtEventTrait<Self> { \
public: \
using BaseType = cc::event::TgtEventTrait<Self>; \
using EventType = cc::event::Event<EventTypeClass>; \
using EventID = cc::event::TargetEventID<EventTypeClass>; \
using _persist_function_type = std::function<void(Self *, EventType *)>; \
using _handler_function_type = std::function<void(EventType *)>; \
constexpr static const char *EVENT_NAME = #EventTypeClass; \
constexpr static size_t TYPE_ID = __COUNTER__ - __counter_start__ - 1 + __counter_offset__; \
constexpr static size_t TypeHash() { \
return cc::event::intl::hash(#EventTypeClass); \
} \
};
// NOLINTNEXTLINE
#define _DECLARE_TARGET_EVENT_INTER(EventTypeClass, ...) \
class EventTypeClass final : public cc::event::TgtEventTrait<Self, __VA_ARGS__> { \
public: \
using BaseType = cc::event::TgtEventTrait<Self, __VA_ARGS__>; \
using EventType = cc::event::Event<EventTypeClass>; \
using EventID = cc::event::TargetEventID<EventTypeClass>; \
using _persist_function_type = std::function<void(Self *, EventType *)>; \
using _handler_function_type = std::function<void(EventType *)>; \
constexpr static const char *EVENT_NAME = #EventTypeClass; \
constexpr static size_t TYPE_ID = __COUNTER__ - __counter_start__ - 1 + __counter_offset__; \
constexpr static size_t TypeHash() { \
return cc::event::intl::hash(#EventTypeClass); \
} \
};
// NOLINTNEXTLINE
#define _IMPL_EVENT_TARGET_(Self) \
public: \
template <typename TgtEvent, typename Fn> \
cc::event::TargetEventID<TgtEvent> once(Fn &&func, bool useCapture) { \
return _eventTargetImpl.get().once<TgtEvent>(std::forward<Fn>(func), useCapture); \
} \
\
template <typename TgtEvent, typename Fn, typename O> \
cc::event::TargetEventID<TgtEvent> once(Fn &&func, O *ctx) { \
return _eventTargetImpl.get().once<TgtEvent>(std::forward<Fn>(func), ctx); \
} \
template <typename TgtEvent, typename Fn> \
cc::event::TargetEventID<TgtEvent> on(Fn &&func, bool useCapture = false) { \
static_assert(std::is_base_of<typename TgtEvent::_emitter_type, Self>::value, "mismatch target type"); \
return _eventTargetImpl.get().on<TgtEvent, Fn>(std::forward<Fn>(func), useCapture); \
} \
template <typename TgtEvent, typename Fn, typename O> \
cc::event::TargetEventID<TgtEvent> on(Fn &&func, O *ctx, bool useCapture = false) { \
return _eventTargetImpl.get().on<TgtEvent, Fn, O>(std::forward<Fn>(func), ctx, useCapture); \
} \
template <typename TgtEvent> \
bool off(cc::event::TargetEventID<TgtEvent> eventId) { \
return _eventTargetImpl.get().off(eventId); \
} \
void offAll() { \
_eventTargetImpl.get().offAll(); \
} \
template <typename TgtEvent> \
void off() { \
_eventTargetImpl.get().off<TgtEvent>(); \
} \
template <typename TgtEvent, typename... ARGS> \
void emit(ARGS &&...args) { \
_eventTargetImpl.get().emit<TgtEvent>(this, std::forward<ARGS>(args)...); \
} \
template <typename TgtEvent, typename... ARGS> \
void dispatchEvent(ARGS &&...args) { \
_eventTargetImpl.get().dispatchEvent<TgtEvent, ARGS...>(this, std::forward<ARGS>(args)...); \
}
#define IMPL_EVENT_TARGET(TargetClass) \
public: \
static constexpr bool HAS_PARENT = false; \
inline TargetClass *evGetParent() { return nullptr; } \
_IMPL_EVENT_TARGET_(TargetClass)
#define IMPL_EVENT_TARGET_WITH_PARENT(TargetClass, getParentMethod) \
public: \
static constexpr bool HAS_PARENT = true; \
inline auto evGetParent() { \
return getParentMethod(); \
} \
_IMPL_EVENT_TARGET_(TargetClass)
// NOLINTNEXTLINE
#define _DECLARE_TARGET_EVENT_BEGIN(TargetClass) \
using Self = TargetClass; \
\
private: \
constexpr static int __counter_start__ = __COUNTER__; \
\
public:
#define DECLARE_TARGET_EVENT_BEGIN(TargetClass) \
constexpr static int __counter_offset__ = 0; \
_DECLARE_TARGET_EVENT_BEGIN(TargetClass)
#define DECLARE_TARGET_EVENT_BEGIN_WITH_PARENTS(TargetClass, ...) \
constexpr static int __counter_offset__ = cc::event::intl::TotalEvents<__VA_ARGS__>; \
_DECLARE_TARGET_EVENT_BEGIN(TargetClass)
#define DECLARE_TARGET_EVENT_END() \
private: \
constexpr static int __counter_stop__ = __COUNTER__; \
constexpr static int __event_count__ = __counter_stop__ - __counter_start__ + __counter_offset__; \
cc::event::EventTargetGuard<Self, __event_count__, HAS_PARENT> _eventTargetImpl; \
\
public: \
constexpr static int getEventCount() { return __event_count__; } \
auto &getEventTarget() { return _eventTargetImpl.get(); }
#include "intl/EventTargetMacros.h"

View File

@@ -0,0 +1,295 @@
// generated code
#define DECLARE_BUS_EVENT_ARG0(BusEventClass, EventBusClass) \
struct BusEventClass final : cc::event::BusEventTrait<EventBusName_(EventBusClass), void> { \
using BusType = EventBusName_(EventBusClass); \
using Listener = cc::event::Listener<BusEventClass>; \
constexpr static const char *BUS_NAME = EventBusName_(EventBusClass)::BUS_NAME; \
constexpr static const char *HANDLE_CLASS = #BusEventClass; \
constexpr static size_t TypeID() { \
return cc::event::intl::hash(#BusEventClass); \
} \
static inline void broadcast() { \
cc::event::broadcast<BusEventClass>(); \
} \
};
#define DECLARE_BUS_EVENT_ARG1(BusEventClass, EventBusClass, ArgType0) \
struct BusEventClass final : cc::event::BusEventTrait<EventBusName_(EventBusClass), void, ArgType0> { \
using BusType = EventBusName_(EventBusClass); \
using Listener = cc::event::Listener<BusEventClass>; \
constexpr static const char *BUS_NAME = EventBusName_(EventBusClass)::BUS_NAME; \
constexpr static const char *HANDLE_CLASS = #BusEventClass; \
constexpr static size_t TypeID() { \
return cc::event::intl::hash(#BusEventClass); \
} \
static inline void broadcast(ArgType0 arg0) { \
cc::event::broadcast<BusEventClass>(arg0); \
} \
};
#define DECLARE_BUS_EVENT_ARG2(BusEventClass, EventBusClass, ArgType0, ArgType1) \
struct BusEventClass final : cc::event::BusEventTrait<EventBusName_(EventBusClass), void, ArgType0, ArgType1> { \
using BusType = EventBusName_(EventBusClass); \
using Listener = cc::event::Listener<BusEventClass>; \
constexpr static const char *BUS_NAME = EventBusName_(EventBusClass)::BUS_NAME; \
constexpr static const char *HANDLE_CLASS = #BusEventClass; \
constexpr static size_t TypeID() { \
return cc::event::intl::hash(#BusEventClass); \
} \
static inline void broadcast(ArgType0 arg0, ArgType1 arg1) { \
cc::event::broadcast<BusEventClass>(arg0, arg1); \
} \
};
#define DECLARE_BUS_EVENT_ARG3(BusEventClass, EventBusClass, ArgType0, ArgType1, ArgType2) \
struct BusEventClass final : cc::event::BusEventTrait<EventBusName_(EventBusClass), void, ArgType0, ArgType1, ArgType2> { \
using BusType = EventBusName_(EventBusClass); \
using Listener = cc::event::Listener<BusEventClass>; \
constexpr static const char *BUS_NAME = EventBusName_(EventBusClass)::BUS_NAME; \
constexpr static const char *HANDLE_CLASS = #BusEventClass; \
constexpr static size_t TypeID() { \
return cc::event::intl::hash(#BusEventClass); \
} \
static inline void broadcast(ArgType0 arg0, ArgType1 arg1, ArgType2 arg2) { \
cc::event::broadcast<BusEventClass>(arg0, arg1, arg2); \
} \
};
#define DECLARE_BUS_EVENT_ARG4(BusEventClass, EventBusClass, ArgType0, ArgType1, ArgType2, ArgType3) \
struct BusEventClass final : cc::event::BusEventTrait<EventBusName_(EventBusClass), void, ArgType0, ArgType1, ArgType2, ArgType3> { \
using BusType = EventBusName_(EventBusClass); \
using Listener = cc::event::Listener<BusEventClass>; \
constexpr static const char *BUS_NAME = EventBusName_(EventBusClass)::BUS_NAME; \
constexpr static const char *HANDLE_CLASS = #BusEventClass; \
constexpr static size_t TypeID() { \
return cc::event::intl::hash(#BusEventClass); \
} \
static inline void broadcast(ArgType0 arg0, ArgType1 arg1, ArgType2 arg2, ArgType3 arg3) { \
cc::event::broadcast<BusEventClass>(arg0, arg1, arg2, arg3); \
} \
};
#define DECLARE_BUS_EVENT_ARG5(BusEventClass, EventBusClass, ArgType0, ArgType1, ArgType2, ArgType3, ArgType4) \
struct BusEventClass final : cc::event::BusEventTrait<EventBusName_(EventBusClass), void, ArgType0, ArgType1, ArgType2, ArgType3, ArgType4> { \
using BusType = EventBusName_(EventBusClass); \
using Listener = cc::event::Listener<BusEventClass>; \
constexpr static const char *BUS_NAME = EventBusName_(EventBusClass)::BUS_NAME; \
constexpr static const char *HANDLE_CLASS = #BusEventClass; \
constexpr static size_t TypeID() { \
return cc::event::intl::hash(#BusEventClass); \
} \
static inline void broadcast(ArgType0 arg0, ArgType1 arg1, ArgType2 arg2, ArgType3 arg3, ArgType4 arg4) { \
cc::event::broadcast<BusEventClass>(arg0, arg1, arg2, arg3, arg4); \
} \
};
#define DECLARE_BUS_EVENT_ARG6(BusEventClass, EventBusClass, ArgType0, ArgType1, ArgType2, ArgType3, ArgType4, ArgType5) \
struct BusEventClass final : cc::event::BusEventTrait<EventBusName_(EventBusClass), void, ArgType0, ArgType1, ArgType2, ArgType3, ArgType4, ArgType5> { \
using BusType = EventBusName_(EventBusClass); \
using Listener = cc::event::Listener<BusEventClass>; \
constexpr static const char *BUS_NAME = EventBusName_(EventBusClass)::BUS_NAME; \
constexpr static const char *HANDLE_CLASS = #BusEventClass; \
constexpr static size_t TypeID() { \
return cc::event::intl::hash(#BusEventClass); \
} \
static inline void broadcast(ArgType0 arg0, ArgType1 arg1, ArgType2 arg2, ArgType3 arg3, ArgType4 arg4, ArgType5 arg5) { \
cc::event::broadcast<BusEventClass>(arg0, arg1, arg2, arg3, arg4, arg5); \
} \
};
#define DECLARE_BUS_EVENT_ARG7(BusEventClass, EventBusClass, ArgType0, ArgType1, ArgType2, ArgType3, ArgType4, ArgType5, ArgType6) \
struct BusEventClass final : cc::event::BusEventTrait<EventBusName_(EventBusClass), void, ArgType0, ArgType1, ArgType2, ArgType3, ArgType4, ArgType5, ArgType6> { \
using BusType = EventBusName_(EventBusClass); \
using Listener = cc::event::Listener<BusEventClass>; \
constexpr static const char *BUS_NAME = EventBusName_(EventBusClass)::BUS_NAME; \
constexpr static const char *HANDLE_CLASS = #BusEventClass; \
constexpr static size_t TypeID() { \
return cc::event::intl::hash(#BusEventClass); \
} \
static inline void broadcast(ArgType0 arg0, ArgType1 arg1, ArgType2 arg2, ArgType3 arg3, ArgType4 arg4, ArgType5 arg5, ArgType6 arg6) { \
cc::event::broadcast<BusEventClass>(arg0, arg1, arg2, arg3, arg4, arg5, arg6); \
} \
};
#define DECLARE_BUS_EVENT_ARG8(BusEventClass, EventBusClass, ArgType0, ArgType1, ArgType2, ArgType3, ArgType4, ArgType5, ArgType6, ArgType7) \
struct BusEventClass final : cc::event::BusEventTrait<EventBusName_(EventBusClass), void, ArgType0, ArgType1, ArgType2, ArgType3, ArgType4, ArgType5, ArgType6, ArgType7> { \
using BusType = EventBusName_(EventBusClass); \
using Listener = cc::event::Listener<BusEventClass>; \
constexpr static const char *BUS_NAME = EventBusName_(EventBusClass)::BUS_NAME; \
constexpr static const char *HANDLE_CLASS = #BusEventClass; \
constexpr static size_t TypeID() { \
return cc::event::intl::hash(#BusEventClass); \
} \
static inline void broadcast(ArgType0 arg0, ArgType1 arg1, ArgType2 arg2, ArgType3 arg3, ArgType4 arg4, ArgType5 arg5, ArgType6 arg6, ArgType7 arg7) { \
cc::event::broadcast<BusEventClass>(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7); \
} \
};
#define DECLARE_BUS_EVENT_ARG9(BusEventClass, EventBusClass, ArgType0, ArgType1, ArgType2, ArgType3, ArgType4, ArgType5, ArgType6, ArgType7, ArgType8) \
struct BusEventClass final : cc::event::BusEventTrait<EventBusName_(EventBusClass), void, ArgType0, ArgType1, ArgType2, ArgType3, ArgType4, ArgType5, ArgType6, ArgType7, ArgType8> { \
using BusType = EventBusName_(EventBusClass); \
using Listener = cc::event::Listener<BusEventClass>; \
constexpr static const char *BUS_NAME = EventBusName_(EventBusClass)::BUS_NAME; \
constexpr static const char *HANDLE_CLASS = #BusEventClass; \
constexpr static size_t TypeID() { \
return cc::event::intl::hash(#BusEventClass); \
} \
static inline void broadcast(ArgType0 arg0, ArgType1 arg1, ArgType2 arg2, ArgType3 arg3, ArgType4 arg4, ArgType5 arg5, ArgType6 arg6, ArgType7 arg7, ArgType8 arg8) { \
cc::event::broadcast<BusEventClass>(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8); \
} \
};
#define DECLARE_BUS_EVENT_ARG10(BusEventClass, EventBusClass, ArgType0, ArgType1, ArgType2, ArgType3, ArgType4, ArgType5, ArgType6, ArgType7, ArgType8, ArgType9) \
struct BusEventClass final : cc::event::BusEventTrait<EventBusName_(EventBusClass), void, ArgType0, ArgType1, ArgType2, ArgType3, ArgType4, ArgType5, ArgType6, ArgType7, ArgType8, ArgType9> { \
using BusType = EventBusName_(EventBusClass); \
using Listener = cc::event::Listener<BusEventClass>; \
constexpr static const char *BUS_NAME = EventBusName_(EventBusClass)::BUS_NAME; \
constexpr static const char *HANDLE_CLASS = #BusEventClass; \
constexpr static size_t TypeID() { \
return cc::event::intl::hash(#BusEventClass); \
} \
static inline void broadcast(ArgType0 arg0, ArgType1 arg1, ArgType2 arg2, ArgType3 arg3, ArgType4 arg4, ArgType5 arg5, ArgType6 arg6, ArgType7 arg7, ArgType8 arg8, ArgType9 arg9) { \
cc::event::broadcast<BusEventClass>(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9); \
} \
};
#define DECLARE_BUS_EVENT_ARG11(BusEventClass, EventBusClass, ArgType0, ArgType1, ArgType2, ArgType3, ArgType4, ArgType5, ArgType6, ArgType7, ArgType8, ArgType9, ArgType10) \
struct BusEventClass final : cc::event::BusEventTrait<EventBusName_(EventBusClass), void, ArgType0, ArgType1, ArgType2, ArgType3, ArgType4, ArgType5, ArgType6, ArgType7, ArgType8, ArgType9, ArgType10> { \
using BusType = EventBusName_(EventBusClass); \
using Listener = cc::event::Listener<BusEventClass>; \
constexpr static const char *BUS_NAME = EventBusName_(EventBusClass)::BUS_NAME; \
constexpr static const char *HANDLE_CLASS = #BusEventClass; \
constexpr static size_t TypeID() { \
return cc::event::intl::hash(#BusEventClass); \
} \
static inline void broadcast(ArgType0 arg0, ArgType1 arg1, ArgType2 arg2, ArgType3 arg3, ArgType4 arg4, ArgType5 arg5, ArgType6 arg6, ArgType7 arg7, ArgType8 arg8, ArgType9 arg9, ArgType10 arg10) { \
cc::event::broadcast<BusEventClass>(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10); \
} \
};
#define DECLARE_BUS_EVENT_ARG12(BusEventClass, EventBusClass, ArgType0, ArgType1, ArgType2, ArgType3, ArgType4, ArgType5, ArgType6, ArgType7, ArgType8, ArgType9, ArgType10, ArgType11) \
struct BusEventClass final : cc::event::BusEventTrait<EventBusName_(EventBusClass), void, ArgType0, ArgType1, ArgType2, ArgType3, ArgType4, ArgType5, ArgType6, ArgType7, ArgType8, ArgType9, ArgType10, ArgType11> { \
using BusType = EventBusName_(EventBusClass); \
using Listener = cc::event::Listener<BusEventClass>; \
constexpr static const char *BUS_NAME = EventBusName_(EventBusClass)::BUS_NAME; \
constexpr static const char *HANDLE_CLASS = #BusEventClass; \
constexpr static size_t TypeID() { \
return cc::event::intl::hash(#BusEventClass); \
} \
static inline void broadcast(ArgType0 arg0, ArgType1 arg1, ArgType2 arg2, ArgType3 arg3, ArgType4 arg4, ArgType5 arg5, ArgType6 arg6, ArgType7 arg7, ArgType8 arg8, ArgType9 arg9, ArgType10 arg10, ArgType11 arg11) { \
cc::event::broadcast<BusEventClass>(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11); \
} \
};
#define DECLARE_BUS_EVENT_ARG13(BusEventClass, EventBusClass, ArgType0, ArgType1, ArgType2, ArgType3, ArgType4, ArgType5, ArgType6, ArgType7, ArgType8, ArgType9, ArgType10, ArgType11, ArgType12) \
struct BusEventClass final : cc::event::BusEventTrait<EventBusName_(EventBusClass), void, ArgType0, ArgType1, ArgType2, ArgType3, ArgType4, ArgType5, ArgType6, ArgType7, ArgType8, ArgType9, ArgType10, ArgType11, ArgType12> { \
using BusType = EventBusName_(EventBusClass); \
using Listener = cc::event::Listener<BusEventClass>; \
constexpr static const char *BUS_NAME = EventBusName_(EventBusClass)::BUS_NAME; \
constexpr static const char *HANDLE_CLASS = #BusEventClass; \
constexpr static size_t TypeID() { \
return cc::event::intl::hash(#BusEventClass); \
} \
static inline void broadcast(ArgType0 arg0, ArgType1 arg1, ArgType2 arg2, ArgType3 arg3, ArgType4 arg4, ArgType5 arg5, ArgType6 arg6, ArgType7 arg7, ArgType8 arg8, ArgType9 arg9, ArgType10 arg10, ArgType11 arg11, ArgType12 arg12) { \
cc::event::broadcast<BusEventClass>(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12); \
} \
};
#define DECLARE_BUS_EVENT_ARG14(BusEventClass, EventBusClass, ArgType0, ArgType1, ArgType2, ArgType3, ArgType4, ArgType5, ArgType6, ArgType7, ArgType8, ArgType9, ArgType10, ArgType11, ArgType12, ArgType13) \
struct BusEventClass final : cc::event::BusEventTrait<EventBusName_(EventBusClass), void, ArgType0, ArgType1, ArgType2, ArgType3, ArgType4, ArgType5, ArgType6, ArgType7, ArgType8, ArgType9, ArgType10, ArgType11, ArgType12, ArgType13> { \
using BusType = EventBusName_(EventBusClass); \
using Listener = cc::event::Listener<BusEventClass>; \
constexpr static const char *BUS_NAME = EventBusName_(EventBusClass)::BUS_NAME; \
constexpr static const char *HANDLE_CLASS = #BusEventClass; \
constexpr static size_t TypeID() { \
return cc::event::intl::hash(#BusEventClass); \
} \
static inline void broadcast(ArgType0 arg0, ArgType1 arg1, ArgType2 arg2, ArgType3 arg3, ArgType4 arg4, ArgType5 arg5, ArgType6 arg6, ArgType7 arg7, ArgType8 arg8, ArgType9 arg9, ArgType10 arg10, ArgType11 arg11, ArgType12 arg12, ArgType13 arg13) { \
cc::event::broadcast<BusEventClass>(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13); \
} \
};
#define DECLARE_BUS_EVENT_ARG15(BusEventClass, EventBusClass, ArgType0, ArgType1, ArgType2, ArgType3, ArgType4, ArgType5, ArgType6, ArgType7, ArgType8, ArgType9, ArgType10, ArgType11, ArgType12, ArgType13, ArgType14) \
struct BusEventClass final : cc::event::BusEventTrait<EventBusName_(EventBusClass), void, ArgType0, ArgType1, ArgType2, ArgType3, ArgType4, ArgType5, ArgType6, ArgType7, ArgType8, ArgType9, ArgType10, ArgType11, ArgType12, ArgType13, ArgType14> { \
using BusType = EventBusName_(EventBusClass); \
using Listener = cc::event::Listener<BusEventClass>; \
constexpr static const char *BUS_NAME = EventBusName_(EventBusClass)::BUS_NAME; \
constexpr static const char *HANDLE_CLASS = #BusEventClass; \
constexpr static size_t TypeID() { \
return cc::event::intl::hash(#BusEventClass); \
} \
static inline void broadcast(ArgType0 arg0, ArgType1 arg1, ArgType2 arg2, ArgType3 arg3, ArgType4 arg4, ArgType5 arg5, ArgType6 arg6, ArgType7 arg7, ArgType8 arg8, ArgType9 arg9, ArgType10 arg10, ArgType11 arg11, ArgType12 arg12, ArgType13 arg13, ArgType14 arg14) { \
cc::event::broadcast<BusEventClass>(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14); \
} \
};
#define DECLARE_BUS_EVENT_ARG16(BusEventClass, EventBusClass, ArgType0, ArgType1, ArgType2, ArgType3, ArgType4, ArgType5, ArgType6, ArgType7, ArgType8, ArgType9, ArgType10, ArgType11, ArgType12, ArgType13, ArgType14, ArgType15) \
struct BusEventClass final : cc::event::BusEventTrait<EventBusName_(EventBusClass), void, ArgType0, ArgType1, ArgType2, ArgType3, ArgType4, ArgType5, ArgType6, ArgType7, ArgType8, ArgType9, ArgType10, ArgType11, ArgType12, ArgType13, ArgType14, ArgType15> { \
using BusType = EventBusName_(EventBusClass); \
using Listener = cc::event::Listener<BusEventClass>; \
constexpr static const char *BUS_NAME = EventBusName_(EventBusClass)::BUS_NAME; \
constexpr static const char *HANDLE_CLASS = #BusEventClass; \
constexpr static size_t TypeID() { \
return cc::event::intl::hash(#BusEventClass); \
} \
static inline void broadcast(ArgType0 arg0, ArgType1 arg1, ArgType2 arg2, ArgType3 arg3, ArgType4 arg4, ArgType5 arg5, ArgType6 arg6, ArgType7 arg7, ArgType8 arg8, ArgType9 arg9, ArgType10 arg10, ArgType11 arg11, ArgType12 arg12, ArgType13 arg13, ArgType14 arg14, ArgType15 arg15) { \
cc::event::broadcast<BusEventClass>(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15); \
} \
};
#define DECLARE_BUS_EVENT_ARG17(BusEventClass, EventBusClass, ArgType0, ArgType1, ArgType2, ArgType3, ArgType4, ArgType5, ArgType6, ArgType7, ArgType8, ArgType9, ArgType10, ArgType11, ArgType12, ArgType13, ArgType14, ArgType15, ArgType16) \
struct BusEventClass final : cc::event::BusEventTrait<EventBusName_(EventBusClass), void, ArgType0, ArgType1, ArgType2, ArgType3, ArgType4, ArgType5, ArgType6, ArgType7, ArgType8, ArgType9, ArgType10, ArgType11, ArgType12, ArgType13, ArgType14, ArgType15, ArgType16> { \
using BusType = EventBusName_(EventBusClass); \
using Listener = cc::event::Listener<BusEventClass>; \
constexpr static const char *BUS_NAME = EventBusName_(EventBusClass)::BUS_NAME; \
constexpr static const char *HANDLE_CLASS = #BusEventClass; \
constexpr static size_t TypeID() { \
return cc::event::intl::hash(#BusEventClass); \
} \
static inline void broadcast(ArgType0 arg0, ArgType1 arg1, ArgType2 arg2, ArgType3 arg3, ArgType4 arg4, ArgType5 arg5, ArgType6 arg6, ArgType7 arg7, ArgType8 arg8, ArgType9 arg9, ArgType10 arg10, ArgType11 arg11, ArgType12 arg12, ArgType13 arg13, ArgType14 arg14, ArgType15 arg15, ArgType16 arg16) { \
cc::event::broadcast<BusEventClass>(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15, arg16); \
} \
};
#define DECLARE_BUS_EVENT_ARG18(BusEventClass, EventBusClass, ArgType0, ArgType1, ArgType2, ArgType3, ArgType4, ArgType5, ArgType6, ArgType7, ArgType8, ArgType9, ArgType10, ArgType11, ArgType12, ArgType13, ArgType14, ArgType15, ArgType16, ArgType17) \
struct BusEventClass final : cc::event::BusEventTrait<EventBusName_(EventBusClass), void, ArgType0, ArgType1, ArgType2, ArgType3, ArgType4, ArgType5, ArgType6, ArgType7, ArgType8, ArgType9, ArgType10, ArgType11, ArgType12, ArgType13, ArgType14, ArgType15, ArgType16, ArgType17> { \
using BusType = EventBusName_(EventBusClass); \
using Listener = cc::event::Listener<BusEventClass>; \
constexpr static const char *BUS_NAME = EventBusName_(EventBusClass)::BUS_NAME; \
constexpr static const char *HANDLE_CLASS = #BusEventClass; \
constexpr static size_t TypeID() { \
return cc::event::intl::hash(#BusEventClass); \
} \
static inline void broadcast(ArgType0 arg0, ArgType1 arg1, ArgType2 arg2, ArgType3 arg3, ArgType4 arg4, ArgType5 arg5, ArgType6 arg6, ArgType7 arg7, ArgType8 arg8, ArgType9 arg9, ArgType10 arg10, ArgType11 arg11, ArgType12 arg12, ArgType13 arg13, ArgType14 arg14, ArgType15 arg15, ArgType16 arg16, ArgType17 arg17) { \
cc::event::broadcast<BusEventClass>(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15, arg16, arg17); \
} \
};
#define DECLARE_BUS_EVENT_ARG19(BusEventClass, EventBusClass, ArgType0, ArgType1, ArgType2, ArgType3, ArgType4, ArgType5, ArgType6, ArgType7, ArgType8, ArgType9, ArgType10, ArgType11, ArgType12, ArgType13, ArgType14, ArgType15, ArgType16, ArgType17, ArgType18) \
struct BusEventClass final : cc::event::BusEventTrait<EventBusName_(EventBusClass), void, ArgType0, ArgType1, ArgType2, ArgType3, ArgType4, ArgType5, ArgType6, ArgType7, ArgType8, ArgType9, ArgType10, ArgType11, ArgType12, ArgType13, ArgType14, ArgType15, ArgType16, ArgType17, ArgType18> { \
using BusType = EventBusName_(EventBusClass); \
using Listener = cc::event::Listener<BusEventClass>; \
constexpr static const char *BUS_NAME = EventBusName_(EventBusClass)::BUS_NAME; \
constexpr static const char *HANDLE_CLASS = #BusEventClass; \
constexpr static size_t TypeID() { \
return cc::event::intl::hash(#BusEventClass); \
} \
static inline void broadcast(ArgType0 arg0, ArgType1 arg1, ArgType2 arg2, ArgType3 arg3, ArgType4 arg4, ArgType5 arg5, ArgType6 arg6, ArgType7 arg7, ArgType8 arg8, ArgType9 arg9, ArgType10 arg10, ArgType11 arg11, ArgType12 arg12, ArgType13 arg13, ArgType14 arg14, ArgType15 arg15, ArgType16 arg16, ArgType17 arg17, ArgType18 arg18) { \
cc::event::broadcast<BusEventClass>(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15, arg16, arg17, arg18); \
} \
};
#define DECLARE_BUS_EVENT_ARG20(BusEventClass, EventBusClass, ArgType0, ArgType1, ArgType2, ArgType3, ArgType4, ArgType5, ArgType6, ArgType7, ArgType8, ArgType9, ArgType10, ArgType11, ArgType12, ArgType13, ArgType14, ArgType15, ArgType16, ArgType17, ArgType18, ArgType19) \
struct BusEventClass final : cc::event::BusEventTrait<EventBusName_(EventBusClass), void, ArgType0, ArgType1, ArgType2, ArgType3, ArgType4, ArgType5, ArgType6, ArgType7, ArgType8, ArgType9, ArgType10, ArgType11, ArgType12, ArgType13, ArgType14, ArgType15, ArgType16, ArgType17, ArgType18, ArgType19> { \
using BusType = EventBusName_(EventBusClass); \
using Listener = cc::event::Listener<BusEventClass>; \
constexpr static const char *BUS_NAME = EventBusName_(EventBusClass)::BUS_NAME; \
constexpr static const char *HANDLE_CLASS = #BusEventClass; \
constexpr static size_t TypeID() { \
return cc::event::intl::hash(#BusEventClass); \
} \
static inline void broadcast(ArgType0 arg0, ArgType1 arg1, ArgType2 arg2, ArgType3 arg3, ArgType4 arg4, ArgType5 arg5, ArgType6 arg6, ArgType7 arg7, ArgType8 arg8, ArgType9 arg9, ArgType10 arg10, ArgType11 arg11, ArgType12 arg12, ArgType13 arg13, ArgType14 arg14, ArgType15 arg15, ArgType16 arg16, ArgType17 arg17, ArgType18 arg18, ArgType19 arg19) { \
cc::event::broadcast<BusEventClass>(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15, arg16, arg17, arg18, arg19); \
} \
};

View File

@@ -0,0 +1,481 @@
/****************************************************************************
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 <cstddef>
#include <cstdint>
#include <functional>
#include <type_traits>
namespace cc {
namespace event {
namespace intl {
template <size_t N, size_t I = 0>
struct hash_calc { // NOLINT
static constexpr size_t apply(const char (&str)[N]) {
return (hash_calc<N, I + 1>::apply(str) ^ str[I]) * 16777619U;
};
};
template <size_t N>
struct hash_calc<N, N> { // NOLINT
static constexpr size_t apply(const char (&/*used*/)[N]) {
return 2166136261U;
};
};
template <size_t N>
constexpr size_t hash(const char (&str)[N]) {
return hash_calc<N>::apply(str);
}
template <typename... TYPES>
struct HeadType;
template <typename Head, typename... Tails>
struct HeadType<Head, Tails...> {
using head = Head;
using remain = HeadType<Tails...>;
};
template <typename Head>
struct HeadType<Head> {
using head = Head;
using remain = HeadType<>;
};
template <>
struct HeadType<> {
using head = void;
using remain = HeadType<>;
};
template <int idx, typename EHandler>
constexpr bool validateParameters() {
return true;
}
template <int idx, typename EHandler, typename Head>
constexpr bool validateParameters(Head && /*unused*/) {
using element_t = std::remove_reference_t<std::tuple_element_t<idx, typename EHandler::_argument_tuple_types>>;
using head = std::remove_reference_t<Head>;
constexpr bool assignable = std::is_assignable_v<head, element_t> || std::is_convertible_v<head, element_t>;
constexpr bool ret = assignable;
static_assert(ret, "Parameter type incorrect");
return ret;
}
template <int idx, typename EHandler, typename Head, typename... ARGS>
constexpr bool validateParameters(Head &&head, ARGS &&...remain) {
return validateParameters<idx, EHandler, Head>(std::forward<Head>(head)) && validateParameters<idx + 1, EHandler, ARGS...>(std::forward<ARGS>(remain)...);
}
template <typename T>
struct TupleExtractor {
using func_type = void();
};
template <typename... ARGS>
struct TupleExtractor<std::tuple<ARGS...>> {
using func_type = void(ARGS...);
using std_func_type = std::function<void(ARGS...)>;
};
template <typename F>
struct FunctionTrait : public FunctionTrait<decltype(&F::operator())> {
constexpr static bool IS_LAMBDA = true;
};
template <typename R, typename C, typename... ARGS>
struct FunctionTrait<R (C::*)(ARGS...)> {
constexpr static bool IS_LAMBDA = false;
using function_type = R (C::*)(ARGS...);
};
template <typename R, typename C, typename... ARGS>
struct FunctionTrait<R (C::*)(ARGS...) const> {
constexpr static bool IS_LAMBDA = false;
using function_type = R (C::*)(ARGS...) const;
};
template <typename R, typename... ARGS>
struct FunctionTrait<R (*)(ARGS...)> {
using function_type = R (*)(ARGS...);
constexpr static bool IS_LAMBDA = false;
};
template <typename R, typename... ARGS>
struct FunctionTrait<R(ARGS...)> {
using function_type = R(ARGS...);
constexpr static bool IS_LAMBDA = false;
};
template <typename R, typename... ARGS>
struct FunctionTrait<std::function<R(ARGS...)>> {
using function_type = std::function<R(ARGS...)>;
constexpr static bool IS_LAMBDA = false;
};
template <typename T>
struct RemoveClass {
using type = T;
};
template <typename C, typename R, typename... ARGS>
struct RemoveClass<R (C::*)(ARGS...)> {
using type = std::function<R(ARGS...)>;
};
template <typename C, typename R, typename... ARGS>
struct RemoveClass<R (C::*)(ARGS...) const> {
using type = std::function<R(ARGS...)>;
};
template <typename F>
using function_type_t = typename FunctionTrait<F>::function_type;
template <typename F>
using lambda_without_class_t = typename RemoveClass<function_type_t<F>>::type;
template <typename F>
inline auto convertLambda(F &&lambda) {
if constexpr (FunctionTrait<F>::IS_LAMBDA) {
return static_cast<lambda_without_class_t<F>>(std::forward<F>(lambda));
} else {
return static_cast<function_type_t<F>>(std::forward<F>(lambda));
}
}
template <typename T>
struct TgtEvtFnTrait;
template <typename C, typename R, typename... ARGS>
struct TgtEvtFnTrait<R(C *, ARGS...)> {
using param0_type = C;
using src_func_type = R(C *, ARGS...);
constexpr static bool IS_MEMBER_FUNC = false;
template <typename TgtEvent>
static constexpr bool accept() {
if constexpr (sizeof...(ARGS) == 0) {
using event_type = typename TgtEvent::EventType;
return std::is_same_v<param0_type, event_type>;
} else {
return false;
}
}
template <typename TgtEvent>
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<TgtEvent>()) {
ret = [&func](emitter_type * /*self*/, event_type *event) {
func(event);
};
} else {
static_assert(std::is_same_v<param0_type, emitter_type>, "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 <typename C, typename R, typename... ARGS>
struct TgtEvtFnTrait<R(const C *, ARGS...)> {
using param0_type = C;
using src_func_type = R(const C *, ARGS...);
constexpr static bool IS_MEMBER_FUNC = false;
template <typename TgtEvent>
static constexpr bool accept() {
if constexpr (sizeof...(ARGS) == 0) {
using event_type = typename TgtEvent::EventType;
return std::is_same_v<param0_type, event_type>;
} else {
return false;
}
}
template <typename TgtEvent>
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<TgtEvent>()) {
ret = [func](emitter_type * /*self*/, event_type *event) {
func(event);
};
} else {
static_assert(std::is_same_v<param0_type, emitter_type>, "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 <typename C, typename R, typename... ARGS>
struct TgtEvtFnTrait<R (*)(C *, ARGS...)> {
using param0_type = C;
using src_func_type = R (*)(C *, ARGS...);
constexpr static bool IS_MEMBER_FUNC = false;
template <typename TgtEvent>
static constexpr bool accept() {
if constexpr (sizeof...(ARGS) == 0) {
using event_type = typename TgtEvent::EventType;
return std::is_same_v<param0_type, event_type>;
} else {
return false;
}
}
template <typename TgtEvent>
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<TgtEvent>()) {
auto ret2 = [func](emitter_type * /*self*/, EventType *event) {
func(event);
};
ret = ret2;
} else {
static_assert(std::is_same_v<emitter_type, param0_type>, "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 <typename C, typename R, typename... ARGS>
struct TgtEvtFnTrait<R (*)(const C *, ARGS...)> {
using src_func_type = R (*)(const C *, ARGS...);
using param0_type = C;
constexpr static bool IS_MEMBER_FUNC = false;
template <typename TgtEvent>
static constexpr bool accept() {
if constexpr (sizeof...(ARGS) == 0) {
using event_type = typename TgtEvent::EventType;
return std::is_same_v<param0_type, event_type>;
} else {
return false;
}
}
template <typename TgtEvent>
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<TgtEvent>()) {
ret = [&func](emitter_type * /*self*/, event_type *event) {
func(event);
};
return ret;
} else {
static_assert(std::is_same_v<emitter_type, param0_type>, "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 <typename C, typename R, typename... ARGS>
struct TgtEvtFnTrait<std::function<R(const C *, ARGS...)>> {
using src_func_type = std::function<R(const C *, ARGS...)>;
using param0_type = C;
constexpr static bool IS_MEMBER_FUNC = false;
template <typename TgtEvent>
static constexpr bool accept() {
if constexpr (sizeof...(ARGS) == 0) {
using event_type = typename TgtEvent::EventType;
return std::is_same_v<param0_type, event_type>;
} else {
return false;
}
}
template <typename TgtEvent>
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<TgtEvent>()) {
ret = [&func](emitter_type * /*self*/, event_type *event) {
func(event);
};
} else {
static_assert(std::is_same_v<emitter_type, param0_type>, "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 <typename C, typename R, typename... ARGS>
struct TgtEvtFnTrait<std::function<R(C *, ARGS...)>> {
using param0_type = C;
using src_func_type = std::function<R(C *, ARGS...)>;
constexpr static bool IS_MEMBER_FUNC = false;
template <typename TgtEvent>
static constexpr bool accept() {
if constexpr (sizeof...(ARGS) == 0) {
using EventType = typename TgtEvent::EventType;
return std::is_same_v<param0_type, EventType>;
} else {
return false;
}
}
template <typename TgtEvent>
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<TgtEvent>()) {
ret = [func](emitter_type * /*self*/, event_type *event) {
func(event);
};
} else {
static_assert(std::is_same_v<emitter_type, param0_type>, "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 <typename C, typename R, typename... ARGS>
struct TgtEvtFnTrait<R (C::*)(ARGS...)> {
using src_func_type = R (C::*)(ARGS...);
using context_type = C;
using param0_type = typename intl::HeadType<ARGS...>::head;
constexpr static bool IS_MEMBER_FUNC = true;
template <typename TgtEvent>
static constexpr bool accept() {
if constexpr (sizeof...(ARGS) == 1) {
using event_type = typename TgtEvent::EventType;
return std::is_same_v<param0_type, event_type *>;
} else {
return false;
}
}
template <typename TgtEvent>
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<emitter_type, context_type>, "mismatch emitter type");
if constexpr (accept<TgtEvent>()) {
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 <typename TgtEvent>
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<TgtEvent>()) {
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 <typename C, typename R, typename... ARGS>
struct TgtEvtFnTrait<R (C::*)(ARGS...) const> {
using src_func_type = R (C::*)(ARGS...) const;
using context_type = C;
using param0_type = typename intl::HeadType<ARGS...>::head;
constexpr static bool IS_MEMBER_FUNC = true;
template <typename TgtEvent>
static constexpr bool accept() {
if constexpr (sizeof...(ARGS) == 1) {
using event_type = typename TgtEvent::EventType;
return std::is_same_v<param0_type, event_type *>;
} else {
return false;
}
}
template <typename TgtEvent>
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<emitter_type, context_type>, "mismatch emitter type");
if constexpr (accept<TgtEvent>()) {
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 <typename TgtEvent>
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 <typename T>
constexpr T addAll(T first) {
return first;
}
template <typename T, typename... Others>
constexpr T addAll(T first, Others... others) {
return first + addAll(others...);
}
template <typename... Parents>
constexpr int TotalEvents = addAll(Parents::getEventCount()...); // NOLINT
} // namespace intl
} // namespace event
} // namespace cc

View File

@@ -0,0 +1,64 @@
// generated code
#define TARGET_EVENT_ARG1(EventType, ArgType0) \
_DECLARE_TARGET_EVENT_INTER(EventType, ArgType0)
#define TARGET_EVENT_ARG2(EventType, ArgType0, ArgType1) \
_DECLARE_TARGET_EVENT_INTER(EventType, ArgType0, ArgType1)
#define TARGET_EVENT_ARG3(EventType, ArgType0, ArgType1, ArgType2) \
_DECLARE_TARGET_EVENT_INTER(EventType, ArgType0, ArgType1, ArgType2)
#define TARGET_EVENT_ARG4(EventType, ArgType0, ArgType1, ArgType2, ArgType3) \
_DECLARE_TARGET_EVENT_INTER(EventType, ArgType0, ArgType1, ArgType2, ArgType3)
#define TARGET_EVENT_ARG5(EventType, ArgType0, ArgType1, ArgType2, ArgType3, ArgType4) \
_DECLARE_TARGET_EVENT_INTER(EventType, ArgType0, ArgType1, ArgType2, ArgType3, ArgType4)
#define TARGET_EVENT_ARG6(EventType, ArgType0, ArgType1, ArgType2, ArgType3, ArgType4, ArgType5) \
_DECLARE_TARGET_EVENT_INTER(EventType, ArgType0, ArgType1, ArgType2, ArgType3, ArgType4, ArgType5)
#define TARGET_EVENT_ARG7(EventType, ArgType0, ArgType1, ArgType2, ArgType3, ArgType4, ArgType5, ArgType6) \
_DECLARE_TARGET_EVENT_INTER(EventType, ArgType0, ArgType1, ArgType2, ArgType3, ArgType4, ArgType5, ArgType6)
#define TARGET_EVENT_ARG8(EventType, ArgType0, ArgType1, ArgType2, ArgType3, ArgType4, ArgType5, ArgType6, ArgType7) \
_DECLARE_TARGET_EVENT_INTER(EventType, ArgType0, ArgType1, ArgType2, ArgType3, ArgType4, ArgType5, ArgType6, ArgType7)
#define TARGET_EVENT_ARG9(EventType, ArgType0, ArgType1, ArgType2, ArgType3, ArgType4, ArgType5, ArgType6, ArgType7, ArgType8) \
_DECLARE_TARGET_EVENT_INTER(EventType, ArgType0, ArgType1, ArgType2, ArgType3, ArgType4, ArgType5, ArgType6, ArgType7, ArgType8)
#define TARGET_EVENT_ARG10(EventType, ArgType0, ArgType1, ArgType2, ArgType3, ArgType4, ArgType5, ArgType6, ArgType7, ArgType8, ArgType9) \
_DECLARE_TARGET_EVENT_INTER(EventType, ArgType0, ArgType1, ArgType2, ArgType3, ArgType4, ArgType5, ArgType6, ArgType7, ArgType8, ArgType9)
#define TARGET_EVENT_ARG11(EventType, ArgType0, ArgType1, ArgType2, ArgType3, ArgType4, ArgType5, ArgType6, ArgType7, ArgType8, ArgType9, ArgType10) \
_DECLARE_TARGET_EVENT_INTER(EventType, ArgType0, ArgType1, ArgType2, ArgType3, ArgType4, ArgType5, ArgType6, ArgType7, ArgType8, ArgType9, ArgType10)
#define TARGET_EVENT_ARG12(EventType, ArgType0, ArgType1, ArgType2, ArgType3, ArgType4, ArgType5, ArgType6, ArgType7, ArgType8, ArgType9, ArgType10, ArgType11) \
_DECLARE_TARGET_EVENT_INTER(EventType, ArgType0, ArgType1, ArgType2, ArgType3, ArgType4, ArgType5, ArgType6, ArgType7, ArgType8, ArgType9, ArgType10, ArgType11)
#define TARGET_EVENT_ARG13(EventType, ArgType0, ArgType1, ArgType2, ArgType3, ArgType4, ArgType5, ArgType6, ArgType7, ArgType8, ArgType9, ArgType10, ArgType11, ArgType12) \
_DECLARE_TARGET_EVENT_INTER(EventType, ArgType0, ArgType1, ArgType2, ArgType3, ArgType4, ArgType5, ArgType6, ArgType7, ArgType8, ArgType9, ArgType10, ArgType11, ArgType12)
#define TARGET_EVENT_ARG14(EventType, ArgType0, ArgType1, ArgType2, ArgType3, ArgType4, ArgType5, ArgType6, ArgType7, ArgType8, ArgType9, ArgType10, ArgType11, ArgType12, ArgType13) \
_DECLARE_TARGET_EVENT_INTER(EventType, ArgType0, ArgType1, ArgType2, ArgType3, ArgType4, ArgType5, ArgType6, ArgType7, ArgType8, ArgType9, ArgType10, ArgType11, ArgType12, ArgType13)
#define TARGET_EVENT_ARG15(EventType, ArgType0, ArgType1, ArgType2, ArgType3, ArgType4, ArgType5, ArgType6, ArgType7, ArgType8, ArgType9, ArgType10, ArgType11, ArgType12, ArgType13, ArgType14) \
_DECLARE_TARGET_EVENT_INTER(EventType, ArgType0, ArgType1, ArgType2, ArgType3, ArgType4, ArgType5, ArgType6, ArgType7, ArgType8, ArgType9, ArgType10, ArgType11, ArgType12, ArgType13, ArgType14)
#define TARGET_EVENT_ARG16(EventType, ArgType0, ArgType1, ArgType2, ArgType3, ArgType4, ArgType5, ArgType6, ArgType7, ArgType8, ArgType9, ArgType10, ArgType11, ArgType12, ArgType13, ArgType14, ArgType15) \
_DECLARE_TARGET_EVENT_INTER(EventType, ArgType0, ArgType1, ArgType2, ArgType3, ArgType4, ArgType5, ArgType6, ArgType7, ArgType8, ArgType9, ArgType10, ArgType11, ArgType12, ArgType13, ArgType14, ArgType15)
#define TARGET_EVENT_ARG17(EventType, ArgType0, ArgType1, ArgType2, ArgType3, ArgType4, ArgType5, ArgType6, ArgType7, ArgType8, ArgType9, ArgType10, ArgType11, ArgType12, ArgType13, ArgType14, ArgType15, ArgType16) \
_DECLARE_TARGET_EVENT_INTER(EventType, ArgType0, ArgType1, ArgType2, ArgType3, ArgType4, ArgType5, ArgType6, ArgType7, ArgType8, ArgType9, ArgType10, ArgType11, ArgType12, ArgType13, ArgType14, ArgType15, ArgType16)
#define TARGET_EVENT_ARG18(EventType, ArgType0, ArgType1, ArgType2, ArgType3, ArgType4, ArgType5, ArgType6, ArgType7, ArgType8, ArgType9, ArgType10, ArgType11, ArgType12, ArgType13, ArgType14, ArgType15, ArgType16, ArgType17) \
_DECLARE_TARGET_EVENT_INTER(EventType, ArgType0, ArgType1, ArgType2, ArgType3, ArgType4, ArgType5, ArgType6, ArgType7, ArgType8, ArgType9, ArgType10, ArgType11, ArgType12, ArgType13, ArgType14, ArgType15, ArgType16, ArgType17)
#define TARGET_EVENT_ARG19(EventType, ArgType0, ArgType1, ArgType2, ArgType3, ArgType4, ArgType5, ArgType6, ArgType7, ArgType8, ArgType9, ArgType10, ArgType11, ArgType12, ArgType13, ArgType14, ArgType15, ArgType16, ArgType17, ArgType18) \
_DECLARE_TARGET_EVENT_INTER(EventType, ArgType0, ArgType1, ArgType2, ArgType3, ArgType4, ArgType5, ArgType6, ArgType7, ArgType8, ArgType9, ArgType10, ArgType11, ArgType12, ArgType13, ArgType14, ArgType15, ArgType16, ArgType17, ArgType18)
#define TARGET_EVENT_ARG20(EventType, ArgType0, ArgType1, ArgType2, ArgType3, ArgType4, ArgType5, ArgType6, ArgType7, ArgType8, ArgType9, ArgType10, ArgType11, ArgType12, ArgType13, ArgType14, ArgType15, ArgType16, ArgType17, ArgType18, ArgType19) \
_DECLARE_TARGET_EVENT_INTER(EventType, ArgType0, ArgType1, ArgType2, ArgType3, ArgType4, ArgType5, ArgType6, ArgType7, ArgType8, ArgType9, ArgType10, ArgType11, ArgType12, ArgType13, ArgType14, ArgType15, ArgType16, ArgType17, ArgType18, ArgType19)
#define TARGET_EVENT_ARG21(EventType, ArgType0, ArgType1, ArgType2, ArgType3, ArgType4, ArgType5, ArgType6, ArgType7, ArgType8, ArgType9, ArgType10, ArgType11, ArgType12, ArgType13, ArgType14, ArgType15, ArgType16, ArgType17, ArgType18, ArgType19, ArgType20) \
_DECLARE_TARGET_EVENT_INTER(EventType, ArgType0, ArgType1, ArgType2, ArgType3, ArgType4, ArgType5, ArgType6, ArgType7, ArgType8, ArgType9, ArgType10, ArgType11, ArgType12, ArgType13, ArgType14, ArgType15, ArgType16, ArgType17, ArgType18, ArgType19, ArgType20)

View File

@@ -0,0 +1,105 @@
/****************************************************************************
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 "base/Macros.h"
namespace cc {
namespace event {
namespace intl {
template <typename ListNode>
bool listAppend(ListNode **head, ListNode *newNode) {
if (newNode->next != nullptr || newNode->prev != nullptr) {
CC_ABORT();
return false;
}
if (*head == nullptr) {
newNode->next = newNode;
newNode->prev = newNode;
*head = newNode;
} else {
auto *first = *head;
auto *last = (*head)->prev;
newNode->prev = last;
newNode->next = first;
first->prev = newNode;
last->next = newNode;
}
return true;
}
template <typename ListNode>
bool detachFromList(ListNode **head, ListNode *node) {
if (*head == nullptr || node->prev == nullptr || node->next == nullptr) {
CC_ABORT();
return false;
}
if (node->prev == node && node->next == node) { // the only node
CC_ASSERT(node == *head); // should be the first
*head = nullptr;
} else {
auto *nextNode = node->next;
auto *prevNode = node->prev;
nextNode->prev = prevNode;
prevNode->next = nextNode;
if (node == *head) {
*head = nextNode;
}
}
node->prev = nullptr;
node->next = nullptr;
return true;
}
} // namespace intl
} // namespace event
} // namespace cc
#define EVENT_LIST_LOOP_BEGIN(tempVar, list) \
if (list) { \
auto *tempVar = list; \
do { \
auto *nextCopy = tempVar->next;
#define EVENT_LIST_LOOP_END(tempVar, list) \
tempVar = nextCopy; \
} \
while (tempVar != list) \
; \
}
#define EVENT_LIST_LOOP_REV_BEGIN(tempVar, list) \
if (list) { \
auto *tempVar = list->prev; \
bool isLastListNode = false; \
do { \
auto *nextCopy = tempVar->prev; \
isLastListNode = tempVar == list;
#define EVENT_LIST_LOOP_REV_END(tempVar, list) \
tempVar = nextCopy; \
} \
while (!isLastListNode) \
; \
}