Sha256: b7d67205fbfd4801b0f0e778bb032cfa7baa9e033f26c86ef718fd7abf837d83

Contents?: true

Size: 1.19 KB

Versions: 2

Compression:

Stored size: 1.19 KB

Contents

#pragma once

#include <functional>
#include <vector>

namespace discord {

template <typename... Args>
class Event final {
public:
    using Token = int;

    Event() { slots_.reserve(4); }

    Event(Event const&) = default;
    Event(Event&&) = default;
    ~Event() = default;

    Event& operator=(Event const&) = default;
    Event& operator=(Event&&) = default;

    template <typename EventHandler>
    Token Connect(EventHandler slot)
    {
        slots_.emplace_back(Slot{nextToken_, std::move(slot)});
        return nextToken_++;
    }

    void Disconnect(Token token)
    {
        for (auto& slot : slots_) {
            if (slot.token == token) {
                slot = slots_.back();
                slots_.pop_back();
                break;
            }
        }
    }

    void DisconnectAll() { slots_ = {}; }

    void operator()(Args... args)
    {
        for (auto const& slot : slots_) {
            slot.fn(std::forward<Args>(args)...);
        }
    }

private:
    struct Slot {
        Token token;
        std::function<void(Args...)> fn;
    };

    Token nextToken_{};
    std::vector<Slot> slots_{};
};

} // namespace discord

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rdiscord_sdk-1.1.1 third-party/include/event.h
rdiscord_sdk-1.1.0 third-party/include/event.h