#pragma once #include template class EventBase { public: EventBase(); ~EventBase(); template void RaiseEvent(TArgs&&... Args); EventBase& operator+=(const Tevent& Rhs); EventBase& operator-=(const Tevent& Rhs); private: // List of event pointers Tevent* EventStack; // Count of event pointers uint8_t StackSize; }; template inline EventBase::EventBase() : EventStack(nullptr), StackSize(0) { } template inline EventBase::~EventBase() { if (EventStack) delete[] EventStack; EventStack = nullptr; StackSize = 0; } template inline EventBase& EventBase::operator+=(const Tevent& Rhs) { auto NewBuffer = new Tevent[StackSize + 1]; if (EventStack != nullptr) { std::memcpy(NewBuffer, EventStack, sizeof(void*) * StackSize); delete[] EventStack; } NewBuffer[StackSize] = Rhs; EventStack = NewBuffer; StackSize++; return *this; } template inline EventBase& EventBase::operator-=(const Tevent& Rhs) { if (EventStack == nullptr) return *this; int32_t Index = -1; for (uint32_t i = 0; i < StackSize; i++) { if (EventStack[i] == Rhs) { Index = i; break; } } if (Index < 0) return *this; else if (StackSize == 1) { delete[] EventStack; EventStack = nullptr; StackSize--; return *this; } auto NewBuffer = new Tevent[StackSize - 1]; std::memcpy(NewBuffer, EventStack, sizeof(void*) * Index); std::memcpy(NewBuffer + Index, EventStack + Index + 1, sizeof(void*) * (StackSize - Index - 1)); delete[] EventStack; EventStack = NewBuffer; StackSize--; return *this; } // Define normal types here using BasicEvent = EventBase; template template inline void EventBase::RaiseEvent(TArgs&&... Args) { if (EventStack == nullptr) return; for (uint32_t i = 0; i < StackSize; i++) EventStack[i](std::forward(Args)...); }