Dawn/src/dawn/state/StateProperty.hpp
2023-02-28 22:18:26 -08:00

81 lines
2.0 KiB
C++

// 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 {
class IStateProperty {
public:
std::vector<std::function<void()>> _effectListners;
};
template<class V>
class StateProperty : public IStateProperty {
private:
/**
* Method that is invoked every time that the value of this state property
* is updated.
*
* @param val Value that is to be used for this property.
*/
void setInternal(V val) {
if(val == this->_realValue) return;// TODO: can I omit this? kinda bad tbh.
this->previous = this->_realValue;
this->_realValue = val;
// Notify the effect listeners
auto itEffect = this->_effectListners.begin();
while(itEffect != this->_effectListners.end()) {
(*itEffect)();
++itEffect;
}
}
public:
V _realValue;
V previous;
/**
* Creates a new state property and listens for its change.
*/
StateProperty() {}
/**
* Creates a new state property and listens for its change.
* @param initial The initial value of this state.
*/
StateProperty(V initial) : _realValue(initial) {}
const StateProperty& operator += (const V &value) {
this->setInternal(this->_realValue + value);
return *this;
}
const bool_t operator != (const V &value) {
return value != this->_realValue;
}
const bool_t operator == (const V &value) {
return value == this->_realValue;
}
const StateProperty& operator = (const V &val) {
this->setInternal(val);
return *this;
}
operator V() const {
return this->_realValue;
}
/**
* Destructor for StateProperty.
*/
~StateProperty() {}
friend class StateOwner;
};
}