2017-11-25 6 views
2

게임용 이벤트 시스템을 작성했습니다. 그것은 잘 작동하지만 하나의 큰 결함이있다 - 형식화되어 있지 않으므로 구독 된 콜백 함수가 수신 된 기본 이벤트를 수동으로 전송해야한다. 자, 템플릿을 사용하여 typesafe 버전을 구현하려고합니다. 나는 주제에 관해서는 일반적으로 괜찮지만 전문가는 분명하지 않다.이벤트 시스템의 유형 보증 구현

// Derived Events 
class ClickEvent : public Event 
{ 
public: 
    float x; 
    float y; 
}; 

class RenderNodeCreatedEvent : public Event 
{ 
public: 
    unsigned long long int renderNodeId; 
}; 

그들을 만들기 파생 된 이벤트를 정의하고 (메인 프로그램에서 테스트 목적 등)을 사용

:

첫째, 여기에 내가 이벤트를 사용하는 방법을 보여주는 몇 가지 코드입니다 여기

// Create the on event functions 
std::function<void(const ClickEvent &)> onClickFunction = [](const ClickEvent & event) 
{ 
    std::cout << std::endl << "Mouse clicked at position: " << event.x << event.y; 
}; 

std::function<void(const RenderNodeCreatedEvent &)> onRenderNodeCreatedFunction = [](const RenderNodeCreatedEvent & event) 
{ 
    std::cout << std::endl << "Render node created with id: " << event.renderNodeId; 
}; 

// Create the events 
ClickEvent clickEvent; 
clickEvent.x = 300.f; 
clickEvent.y = 255.5f; 

RenderNodeCreatedEvent renderNodeCreatedEvent; 
renderNodeCreatedEvent.renderNodeId = 26234628374324; 

// Create the event manager and subscribe the event functions 
EventManager eventManager; 
eventManager.Subscribe(onClickFunction); 
eventManager.Subscribe(onRenderNodeCreatedFunction); 

// Raise the events 
eventManager.Raise(clickEvent); 
eventManager.Raise(renderNodeCreatedEvent); 

내가 각 파생 된 이벤트 클래스 유형 std::size_t의 고유 ID를 생성하기 위해 노력했다 무엇 :

class BaseEvent 
{ 
public: 
    typedef std::size_t ID; 

    BaseEvent() = default; 
    virtual ~BaseEvent() = default; 

protected: 
    static ID GetNextID() 
    { 
     static ID id = 0; 
     return id++; 
    } 
}; 

class Event : public BaseEvent 
{ 
public: 
    Event() = default; 
    virtual ~Event() = default; 

    // Sets a unique id for the event the first time this function is called 
    ID GetID() 
    { 
     static ID id = BaseEvent::GetNextID(); 
     return id; 
    } 
}; 

마지막으로 위에서 설명한 기능을 제공 할 수있는 이벤트 관리자에서 시도합니다. 올바른 캐스팅을 관리하고 /하거나 여러 유형의 콜백 함수를 저장하기 위해 정말 고심하고 있습니다. 콜백 래퍼가 작동하지 않습니다.이 문제를 해결할 수있는 기본적인 아이디어이므로 게시에 포함 시켰습니다.

class EventManager 
{ 
public: 
    // Define the template callback type 
    template <class DerivedEvent> 
    using TCallback = std::function<void(const DerivedEvent &)>; 

public: 
    EventManager() = default; 
    ~EventManager() = default; 

    template<class DerivedEvent> 
    void Subscribe(TCallback<DerivedEvent> callback) 
    { 
     // Get the index of the callback list this callback will be added to 
     Event::ID id = DerivedEvent::GetID(); 

     // This won't work sinve TCallback is a different type than TCallback<Event> 
     callbackListList[id].push_back(callback); 
    } 

    template <class DerivedEvent> 
    void Raise(DerivedEvent event) 
    { 
     // Get the type of the event and therefore the index in the callbackListList 
     Event::ID = DerivedEvent::GetID(); 

     /// How to cast the events back 
     // Get the respective list of callback functions 
     std::vector<TCallback<DerivedEvent>> /*&*/ callbackList; 

     // Create a callback wrapper of with the type 'derived event' ????? 
     CallbackWrapper<DerivedEvent> callbackWrapper(/*derived callback*/); 
     // Call the callback wrapper using the base event ????? 
    } 

    template <typename DerivedEvent> 
    class CallbackWrapper 
    { 
    public: 
     CallbackWrapper(TCallback<DerivedEvent> callback) : callback(callback) {} 

     void operator() (const Event & event) 
     { 
      callback(static_cast<const Event<DerivedEvent> &>(event).event); 
     } 

    private: 
     TCallback<DerivedEvent> callback; 
    }; 

private: 
    std::vector<std::vector<TCallback<Event>>> callbackListList; 
}; 

나는 이것이 많은 코드라는 것을 알고 있지만, 이것이 내가 말하는 것을 보여주기위한 가장 쉬운 방법이라고 느꼈다.

편집 아드리안 도와

감사 : Event 클래스는 것 파생 유형에 대한 고유 ID를 얻을 수있는 템플릿으로 선언합니다.

template <class DerivedEvent> 
class Event : public BaseEvent 

이제 이벤트에서 상속 할 때 :

class ClickEvent : public Event<ClickEvent> 
class RenderNodeCreatedEvent : public Event<RenderNodeCreatedEvent> 

마지막을의 개의 EventManager는 일부 이상 연구 한 유형 의 콜백의 벡터의 벡터 BaseEvent

private: 
    std::vector<std::vector<TCallback<BaseEvent>>> callbackListList; 
+0

아마 관련 : https://stackoverflow.com/questions/47337029/handling-function-pointer-with-covariant-types-uniformly-how-to-call-callbacks은 분명히 – geza

+0

, 당신은 사용할 수 없습니다 ** BaseEvent ** 유형의 콜백 벡터의 벡터입니다. 기본 객체 유형에 객체를 저장하면 분할이 수행되고 파생 객체와 해당 데이터의 유형이 느슨해집니다. 그런데 콜백 유형으로 관리자를 갖는 것이 더 쉽습니다. 즉, EventManager는 특정 유형의 콜백을 관리하는 템플릿이어야합니다. 그렇게하면 유형 안전을 얻는 것이 쉽습니다. – Phil1970

+0

@ Phil1970 예, 목록에 포인터를 저장해야합니다. 이벤트 매니저를 템플릿으로 만드는 것에 대해 : 모든 시스템이 통신하는 데 사용할 수있는 단일 이벤트 시스템 (원할 경우 메시지 시스템)이 내가 원했던 것이기 때문에 이것은 내 목적을 저지합니다. –

답변