mirror of
https://github.com/zoriya/Bomberman.git
synced 2026-06-03 18:31:17 +00:00
Creating a callback type
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
//
|
||||
// Created by Zoe Roux on 5/21/21.
|
||||
//
|
||||
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <functional>
|
||||
|
||||
namespace WAL
|
||||
{
|
||||
//! @brief A callback where you can subscribe to and emit it.
|
||||
template<typename ...Types>
|
||||
class Callback
|
||||
{
|
||||
private:
|
||||
int _nextID = 0;
|
||||
//! @brief The list of functions to call.
|
||||
std::unordered_map<int, std::function<void (Types...)>> _functions = {};
|
||||
|
||||
public:
|
||||
//! @brief Add a method to be called when this callback is invoked.
|
||||
//! @param callback The list of arguments of the callback method
|
||||
//! @return A unique ID for this callback. That can be used to remove the callback later.
|
||||
int addCallback(std::function<R (Types...)> callback)
|
||||
{
|
||||
int id = this->_nextID++;
|
||||
this->_functions.emplace_back(id, std::move(callback));
|
||||
return id;
|
||||
}
|
||||
|
||||
//! @brief Remove a function from this callback.
|
||||
//! @param id The ID of the function.
|
||||
void removeCallback(int id)
|
||||
{
|
||||
this->_functions.erase(id);
|
||||
}
|
||||
|
||||
void operator()(Types ...args) const
|
||||
{
|
||||
for (auto &callback : this->_functions)
|
||||
callback(args...);
|
||||
}
|
||||
|
||||
//! @brief A default constructor
|
||||
Callback() = default;
|
||||
//! @brief A default copy constructor
|
||||
Callback(const Callback &) = default;
|
||||
//! @brief A default destructor
|
||||
~Callback() = default;
|
||||
//! @brief A default assignment operator
|
||||
Callback &operator=(const Callback &) = default;
|
||||
};
|
||||
}
|
||||
@@ -23,12 +23,12 @@ namespace WAL
|
||||
T z;
|
||||
|
||||
//! @brief Create a new nil vector3.
|
||||
Vector3<T>()
|
||||
Vector3()
|
||||
: x(0), y(0), z(0)
|
||||
{}
|
||||
|
||||
//! @brief Create a new vector3 representing a specific coordinate.
|
||||
Vector3<T>(T x, T y, T z)
|
||||
Vector3(T x, T y, T z)
|
||||
: x(x), y(y), z(z)
|
||||
{}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user