I just made C++ God and it's scary
This commit is contained in:
73
src/dawn/state/StateOwner.hpp
Normal file
73
src/dawn/state/StateOwner.hpp
Normal file
@ -0,0 +1,73 @@
|
||||
// Copyright (c) 2023 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "assert/assert.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
template<class V>
|
||||
class StateProperty;
|
||||
|
||||
class StateOwner {
|
||||
private:
|
||||
std::map<void*, std::vector<std::function<void()>>> effects;
|
||||
|
||||
public:
|
||||
/**
|
||||
* Invoked event listener for the subclass that listens for all changes
|
||||
* to any part of this state updated.
|
||||
*/
|
||||
virtual void onStateUpdate() = 0;
|
||||
|
||||
/**
|
||||
* Creates a new state property and listens for its change.
|
||||
*
|
||||
* @tparam V The type of the state that is held.
|
||||
* @param initial The initial value of this state.
|
||||
* @return The state that can then be listened for.
|
||||
*/
|
||||
template<class V>
|
||||
StateProperty<V> useState(V initial) {
|
||||
auto property = StateProperty<V>();
|
||||
property.value = initial;
|
||||
property.owner = this;
|
||||
return property;
|
||||
}
|
||||
|
||||
/**
|
||||
* Listen for changees to a state property and involke the provided func
|
||||
* when the value is changed.
|
||||
*
|
||||
* @tparam V The type of the state property.
|
||||
* @param property Property to listen for affect changees to.
|
||||
* @param fn The callback to be invoked when the state value changes.
|
||||
*/
|
||||
template<class V>
|
||||
void useEffect(StateProperty<V> &property, const std::function<void()> &fn) {
|
||||
this->effects[(void*)&property].push_back(fn);
|
||||
}
|
||||
|
||||
/**
|
||||
* Internal method (that has to be exposed) to listen for changes for when
|
||||
* a state property that belongs to this state owner is updated.
|
||||
*
|
||||
* @tparam V The value type of the state property.
|
||||
* @param prop The property that has its value changed in question.
|
||||
* @param n The new, current value of the property.
|
||||
* @param o The old, previous value of the property.
|
||||
*/
|
||||
template<class V>
|
||||
void onStatePropertyUpdated(StateProperty<V> *prop, V n, V o) {
|
||||
this->onStateUpdate();
|
||||
|
||||
auto eff = &this->effects[prop];
|
||||
auto itEff = eff->begin();
|
||||
while(itEff != eff->end()) {
|
||||
(*itEff)();
|
||||
++itEff;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user