mirror of
https://github.com/Chatterino/chatterino2.git
synced 2024-11-21 22:24:07 +01:00
60 lines
1.2 KiB
C++
60 lines
1.2 KiB
C++
#pragma once
|
|
|
|
#include <QObject>
|
|
|
|
#include <tuple>
|
|
#include <vector>
|
|
|
|
template <typename... Args>
|
|
class SignalSpy : public QObject
|
|
{
|
|
// Simplify the storage if there's only one argument to the signal handler.
|
|
using Call =
|
|
std::conditional_t<sizeof...(Args) == 1,
|
|
std::tuple_element_t<0, std::tuple<Args...>>,
|
|
std::tuple<Args...>>;
|
|
|
|
public:
|
|
SignalSpy(auto *sender, auto method)
|
|
{
|
|
auto conn =
|
|
QObject::connect(sender, method, this, &SignalSpy<Args...>::slot,
|
|
Qt::DirectConnection);
|
|
assert(conn && "Failed to connect");
|
|
}
|
|
|
|
bool empty() const
|
|
{
|
|
return this->calls_.empty();
|
|
}
|
|
|
|
size_t size() const
|
|
{
|
|
return this->calls_.size();
|
|
}
|
|
|
|
const std::vector<std::tuple<Args...>> &calls() const
|
|
{
|
|
return this->calls_;
|
|
}
|
|
|
|
void clear()
|
|
{
|
|
this->calls_.clear();
|
|
}
|
|
|
|
const Call &last() const
|
|
{
|
|
assert(!this->calls_.empty());
|
|
|
|
return this->calls_.back();
|
|
}
|
|
|
|
private:
|
|
void slot(Args... args)
|
|
{
|
|
this->calls_.emplace_back(std::move(args)...);
|
|
}
|
|
|
|
std::vector<Call> calls_;
|
|
};
|