50 lines
1.4 KiB
C++
50 lines
1.4 KiB
C++
// Copyright (c) 2023 Dominic Masters
|
|
//
|
|
// This software is released under the MIT License.
|
|
// https://opensource.org/licenses/MIT
|
|
|
|
#pragma once
|
|
#include "state/State.hpp"
|
|
|
|
namespace Dawn {
|
|
template<typename D, typename...A>
|
|
struct StateListener {
|
|
std::function<void(A...)> callback;
|
|
D data;
|
|
};
|
|
|
|
template<typename D, typename...A>
|
|
struct StateProviderSet {
|
|
public:
|
|
std::vector<StateListener<D,A...>> listeners;
|
|
|
|
/**
|
|
* Attaches an effect listener that is invoked by a provider.
|
|
*
|
|
* @tparam T Context Type, usually the state listener itself.
|
|
* @param callback Callback method to be invoked by the provider.
|
|
* @param data Data to be stored with the StateListener.
|
|
* @param context Context for the state object.
|
|
* @return The unsubscribe method, when invoked, unsubs from the provider.
|
|
*/
|
|
template<class T>
|
|
std::function<void()> addEffect(
|
|
std::function<void(A...)> callback,
|
|
D data,
|
|
T *context
|
|
) {
|
|
struct StateListener<D, A...> l;
|
|
l.callback = callback;
|
|
l.data = data;
|
|
this->listeners.push_back(l);
|
|
|
|
auto unsub = std::bind([&](struct StateListener<D, A...> listener) {
|
|
// Not yet implemented
|
|
assertUnreachable();
|
|
}, l);
|
|
|
|
context->_stateProviderListeners.push_back(unsub);
|
|
return unsub;
|
|
}
|
|
};
|
|
} |