2015-12-01 13:27:46 -05:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 Jared Boone, ShareBrained Technology, Inc.
|
|
|
|
*
|
|
|
|
* This file is part of PortaPack.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 51 Franklin Street,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __SIGNAL_H__
|
|
|
|
#define __SIGNAL_H__
|
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
#include <list>
|
|
|
|
#include <functional>
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
#include "utility.hpp"
|
|
|
|
|
|
|
|
/* Tweaked and vastly simplified implementation of Simple::Signal, from
|
|
|
|
* https://testbit.eu/cpp11-signal-system-performance/
|
|
|
|
*
|
|
|
|
* Original license:
|
|
|
|
* CC0 Public Domain
|
|
|
|
* http://creativecommons.org/publicdomain/zero/1.0/
|
|
|
|
*/
|
|
|
|
|
|
|
|
using SignalToken = uint32_t;
|
|
|
|
|
2023-05-18 16:16:05 -04:00
|
|
|
template <class... Args>
|
2015-12-01 13:27:46 -05:00
|
|
|
struct Signal {
|
2023-05-18 16:16:05 -04:00
|
|
|
using Callback = std::function<void(Args...)>;
|
2015-12-01 13:27:46 -05:00
|
|
|
|
2023-05-18 16:16:05 -04:00
|
|
|
SignalToken operator+=(const Callback& callback) {
|
|
|
|
const SignalToken token = next_token++;
|
|
|
|
entries.emplace_back(std::make_unique<CallbackEntry>(callback, token));
|
|
|
|
return token;
|
|
|
|
}
|
2015-12-01 13:27:46 -05:00
|
|
|
|
2023-05-18 16:16:05 -04:00
|
|
|
bool operator-=(const SignalToken token) {
|
|
|
|
entries.remove_if([token](EntryType& entry) {
|
|
|
|
return entry.get()->token == token;
|
|
|
|
});
|
|
|
|
return true;
|
|
|
|
}
|
2015-12-01 13:27:46 -05:00
|
|
|
|
2023-05-18 16:16:05 -04:00
|
|
|
void emit(Args... args) {
|
|
|
|
for (auto& entry : entries) {
|
|
|
|
entry.get()->callback(args...);
|
|
|
|
};
|
|
|
|
}
|
2015-12-01 13:27:46 -05:00
|
|
|
|
2023-05-18 16:16:05 -04:00
|
|
|
private:
|
|
|
|
struct CallbackEntry {
|
|
|
|
const Callback callback;
|
|
|
|
const SignalToken token;
|
2015-12-01 13:27:46 -05:00
|
|
|
|
2023-05-18 16:16:05 -04:00
|
|
|
constexpr CallbackEntry(
|
|
|
|
const Callback& callback,
|
|
|
|
const SignalToken token)
|
|
|
|
: callback{callback},
|
|
|
|
token{token} {
|
|
|
|
}
|
|
|
|
};
|
2015-12-01 13:27:46 -05:00
|
|
|
|
2023-05-18 16:16:05 -04:00
|
|
|
using EntryType = std::unique_ptr<CallbackEntry>;
|
|
|
|
|
|
|
|
std::list<EntryType> entries{};
|
|
|
|
SignalToken next_token = 1;
|
2015-12-01 13:27:46 -05:00
|
|
|
};
|
|
|
|
|
2023-05-18 16:16:05 -04:00
|
|
|
#endif /*__SIGNAL_H__*/
|