Skip to content

File handler_list.h

File List > endstone > event > handler_list.h

Go to the documentation of this file

// Copyright (c) 2024, The Endstone Project. (https://endstone.dev) All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once

#include <algorithm>
#include <map>
#include <memory>
#include <mutex>
#include <string>
#include <utility>
#include <vector>

#include "endstone/event/event_handler.h"
#include "endstone/event/event_priority.h"

namespace endstone {

class HandlerList {
public:
    explicit HandlerList(std::string event) : event_(std::move(event)) {}

    std::shared_ptr<EventHandler> registerHandler(const std::shared_ptr<EventHandler> &handler)
    {
        if (handler->getEventType() != event_) {
            return nullptr;
        }

        std::lock_guard lock(mtx_);
        valid_ = false;
        handlers_.emplace(handler->getPriority(), std::vector<std::shared_ptr<EventHandler>>{})
            .first->second.push_back(handler);
        return handler;
    }

    [[nodiscard]] std::vector<std::shared_ptr<EventHandler>> unregister(const EventHandler &handler)
    {
        std::vector<std::shared_ptr<EventHandler>> removed;
        std::lock_guard lock(mtx_);
        const auto slot = handlers_.find(handler.getPriority());
        if (slot == handlers_.end()) {
            return removed;
        }
        auto &vector = slot->second;
        const auto it = std::find_if(vector.begin(), vector.end(),
                                     [&](const std::shared_ptr<EventHandler> &h) { return h.get() == &handler; });
        if (it != vector.end()) {
            removed.push_back(*it);
            vector.erase(it);
            dropBaked(removed);
        }
        return removed;
    }

    [[nodiscard]] std::vector<std::shared_ptr<EventHandler>> unregister(const Plugin &plugin)
    {
        std::vector<std::shared_ptr<EventHandler>> removed;
        std::lock_guard lock(mtx_);
        for (auto &[priority, vector] : handlers_) {
            const auto first =
                std::stable_partition(vector.begin(), vector.end(), [&](const std::shared_ptr<EventHandler> &h) {
                    return &h->getPlugin() != &plugin;
                });
            removed.insert(removed.end(), first, vector.end());
            vector.erase(first, vector.end());
        }
        if (!removed.empty()) {
            dropBaked(removed);
        }
        return removed;
    }

    std::vector<std::shared_ptr<EventHandler>> getHandlers() const
    {
        std::lock_guard lock(mtx_);
        if (!valid_) {
            bake();
        }
        return baked_handlers_;
    }

protected:
    void dropBaked(std::vector<std::shared_ptr<EventHandler>> &removed)
    {
        removed.insert(removed.end(), baked_handlers_.begin(), baked_handlers_.end());
        baked_handlers_.clear();
        valid_ = false;
    }

    void bake() const
    {
        if (valid_) {
            return;
        }

        baked_handlers_.clear();
        for (const auto &[priority, vector] : handlers_) {
            for (const auto &handler : vector) {
                baked_handlers_.push_back(handler);
            }
        }
        valid_ = true;
    }

private:
    mutable std::mutex mtx_;
    std::map<EventPriority, std::vector<std::shared_ptr<EventHandler>>> handlers_;
    mutable std::vector<std::shared_ptr<EventHandler>> baked_handlers_;
    mutable bool valid_{false};
    std::string event_;
};

}  // namespace endstone