0

我有一个系统(C++14,使用 Visual Studio 2015 和 GCC 4.9.2),其中我们有许多不同类型的“事件”,这些“事件”可以导致回调发生,以及一个类层次结构,用于识别类型事件和特定于该类型事件的其他自定义属性。

有一个可以接收特定事件对象的事件管理器,以及一个在事件发生时调用的侦听器函数(订阅)。

某些事件会将某些参数传递给侦听器回调,但参数的类型取决于事件类型。

有没有一种方法可以验证传入的侦听器参数(可变参数)是否对事件对象有效?

enum class EventKind {
   first_kind, second_kind, third_kind
};

class EventBase {
public:
    virtual EventKind kind() const = 0; // each subclass returns its EventKind identifier
    virtual bool matches(const EventBase&) const = 0;
};

class FirstEvent : public EventBase {
public:
    EventKind kind() const final { return EventKind::first_kind; }
    bool matches(const EventBase&) const final; // compare kind and other properties

    // other properties unique to FirstEvent
};

// other event subclasses ...

class EventManager {
public:
    template<typename... Args>
    void add_listener(const EventBase& event, std::function<void(Args...)> listener) {
        // validation of Args... based on event.kind() goes here...
    }
    
    template<typename... Args>
    trigger(const EventBase& event, Args... args) {
       // called when event occurs
       // internal lookup in the manager is done to find any listeners connected
       // with this event object, and then we call it...
       for (auto s : subscriptions[event.kind()]) {
          if (event.matches(*(s->event))) {
              auto* ss = dynamic_cast<Sub<Args...> *>(s);
              if (ss && ss->listener) { ss->listener(args...); }
          }
       }
    }

private:
    struct SubBase {
        EventBase* event;
    };
    template<typename... Args>
    struct Sub : public SubBase {
        std::function<void(Args...)> listener;
    };

    std::map<EventKind, std::vector<SubBase *>> subscriptions;
};

我已经有工作代码可以存储侦听器函数以供以后回调(类似于上面),但是 Args... 参数包/可变参数的匹配仅在事件中触发事件时完成manager - 当然,如果原始侦听器的参数集不匹配,则不会调用它(并且会出现静默失败)。

能够在添加侦听器时根据事件类型验证这个参数列表(希望以某种方式使用类层次结构)会很棒。有人有想法吗?

注意:我目前仅限于 C++14/Visual Studio 2014/gcc 4.9.2,因此不能使用任何 C++17 构造。

4

2 回答 2

2

在注册时需要具体的事件类型,并让事件提供侦听器类型。

class FirstEvent : public EventBase {
public:
    EventKind kind() const final { return EventKind::first_kind; }
    bool matches(const EventBase&) const final; // compare kind and other properties

    using listener_type = std::function<void(Foo, Bar, Baz)>;
};

template<typename Event>
using listener_t = typename Event::listener_type;

template<typename Event>
void EventManager::add_listener(listener_t<Event> listener) {
    // can only be called with something that accepts the correct args
}
于 2021-08-04T08:51:43.440 回答
0

这是我设法开始工作的解决方案 - 它避免了 Event 类层次结构,但似乎工作正常。

// Validation of event listener args (general case)
template<typename... Args>
inline bool validate_event_listener_args(EventKind)
{ return false; }

// Validation of event listener args (no args case)
template<>
inline bool validate_event_listener_args<>(EventKind kind)
{
    return (kind == EventKind::second_kind || ... );
}

// Validation of event listener args (for FirstEvent)
template<>
inline bool validate_event_listener_args<Foo, Bar, Qux>(EventKind kind)
{
    return (kind == EventKind::first_kind);
}

// ...

然后,如果返回 false,我可以add_listener调用validate_event_listener_args<Args...>(event.kind())并抛出异常。这对于运行时验证很有用。

于 2021-08-05T00:46:01.690 回答