📛 Renames members

This commit is contained in:
Rune Harlyk
2024-11-23 13:00:31 +01:00
parent b37e8706a6
commit a7c5a5f1cf
@@ -59,21 +59,20 @@ template <class T>
class StatefulService { class StatefulService {
public: public:
template <typename... Args> template <typename... Args>
StatefulService(Args &&...args) StatefulService(Args &&...args) : state_(std::forward<Args>(args)...), mutex_(xSemaphoreCreateRecursiveMutex()) {}
: _state(std::forward<Args>(args)...), _accessMutex(xSemaphoreCreateRecursiveMutex()) {}
update_handler_id_t addUpdateHandler(StateUpdateCallback callback, bool allowRemove = true) { update_handler_id_t addUpdateHandler(StateUpdateCallback callback, bool allowRemove = true) {
if (!callback) return 0; if (!callback) return 0;
StateUpdateHandlerInfo_t updateHandler(callback, allowRemove); StateUpdateHandlerInfo_t updateHandler(callback, allowRemove);
_updateHandlers.push_back(updateHandler); updateHandlers_.push_back(updateHandler);
return updateHandler._id; return updateHandler._id;
} }
void removeUpdateHandler(update_handler_id_t id) { void removeUpdateHandler(update_handler_id_t id) {
for (auto i = _updateHandlers.begin(); i != _updateHandlers.end();) { for (auto i = updateHandlers_.begin(); i != updateHandlers_.end();) {
if ((*i)._allowRemove && (*i)._id == id) { if ((*i)._allowRemove && (*i)._id == id) {
i = _updateHandlers.erase(i); i = updateHandlers_.erase(i);
} else { } else {
++i; ++i;
} }
@@ -84,14 +83,14 @@ class StatefulService {
if (!callback) return 0; if (!callback) return 0;
StateHookHandlerInfo_t hookHandler(callback, allowRemove); StateHookHandlerInfo_t hookHandler(callback, allowRemove);
_hookHandlers.push_back(hookHandler); hookHandlers_.push_back(hookHandler);
return hookHandler._id; return hookHandler._id;
} }
void removeHookHandler(hook_handler_id_t id) { void removeHookHandler(hook_handler_id_t id) {
for (auto i = _hookHandlers.begin(); i != _hookHandlers.end();) { for (auto i = hookHandlers_.begin(); i != hookHandlers_.end();) {
if ((*i)._allowRemove && (*i)._id == id) { if ((*i)._allowRemove && (*i)._id == id) {
i = _hookHandlers.erase(i); i = hookHandlers_.erase(i);
} else { } else {
++i; ++i;
} }
@@ -100,7 +99,7 @@ class StatefulService {
StateUpdateResult update(std::function<StateUpdateResult(T &)> stateUpdater, const String &originId) { StateUpdateResult update(std::function<StateUpdateResult(T &)> stateUpdater, const String &originId) {
beginTransaction(); beginTransaction();
StateUpdateResult result = stateUpdater(_state); StateUpdateResult result = stateUpdater(state_);
endTransaction(); endTransaction();
notifyStateChange(originId, result); notifyStateChange(originId, result);
return result; return result;
@@ -108,14 +107,14 @@ class StatefulService {
StateUpdateResult updateWithoutPropagation(std::function<StateUpdateResult(T &)> stateUpdater) { StateUpdateResult updateWithoutPropagation(std::function<StateUpdateResult(T &)> stateUpdater) {
beginTransaction(); beginTransaction();
StateUpdateResult result = stateUpdater(_state); StateUpdateResult result = stateUpdater(state_);
endTransaction(); endTransaction();
return result; return result;
} }
StateUpdateResult update(JsonObject &jsonObject, JsonStateUpdater<T> stateUpdater, const String &originId) { StateUpdateResult update(JsonObject &jsonObject, JsonStateUpdater<T> stateUpdater, const String &originId) {
beginTransaction(); beginTransaction();
StateUpdateResult result = stateUpdater(jsonObject, _state); StateUpdateResult result = stateUpdater(jsonObject, state_);
endTransaction(); endTransaction();
notifyStateChange(originId, result); notifyStateChange(originId, result);
return result; return result;
@@ -123,42 +122,42 @@ class StatefulService {
StateUpdateResult updateWithoutPropagation(JsonObject &jsonObject, JsonStateUpdater<T> stateUpdater) { StateUpdateResult updateWithoutPropagation(JsonObject &jsonObject, JsonStateUpdater<T> stateUpdater) {
beginTransaction(); beginTransaction();
StateUpdateResult result = stateUpdater(jsonObject, _state); StateUpdateResult result = stateUpdater(jsonObject, state_);
endTransaction(); endTransaction();
return result; return result;
} }
void read(std::function<void(T &)> stateReader) { void read(std::function<void(T &)> stateReader) {
beginTransaction(); beginTransaction();
stateReader(_state); stateReader(state_);
endTransaction(); endTransaction();
} }
void read(JsonObject &jsonObject, JsonStateReader<T> stateReader) { void read(JsonObject &jsonObject, JsonStateReader<T> stateReader) {
beginTransaction(); beginTransaction();
stateReader(_state, jsonObject); stateReader(state_, jsonObject);
endTransaction(); endTransaction();
} }
void callUpdateHandlers(const String &originId) { void callUpdateHandlers(const String &originId) {
for (const StateUpdateHandlerInfo_t &updateHandler : _updateHandlers) { for (const StateUpdateHandlerInfo_t &updateHandler : updateHandlers_) {
updateHandler._callback(originId); updateHandler._callback(originId);
} }
} }
void callHookHandlers(const String &originId, StateUpdateResult &result) { void callHookHandlers(const String &originId, StateUpdateResult &result) {
for (const StateHookHandlerInfo_t &hookHandler : _hookHandlers) { for (const StateHookHandlerInfo_t &hookHandler : hookHandlers_) {
hookHandler._callback(originId, result); hookHandler._callback(originId, result);
} }
} }
T &state() { return _state; } T &state() { return state_; }
private: private:
T _state; T state_;
inline void beginTransaction() { xSemaphoreTakeRecursive(_accessMutex, portMAX_DELAY); } inline void beginTransaction() { xSemaphoreTakeRecursive(mutex_, portMAX_DELAY); }
inline void endTransaction() { xSemaphoreGiveRecursive(_accessMutex); } inline void endTransaction() { xSemaphoreGiveRecursive(mutex_); }
void notifyStateChange(const String &originId, StateUpdateResult &result) { void notifyStateChange(const String &originId, StateUpdateResult &result) {
callHookHandlers(originId, result); callHookHandlers(originId, result);
@@ -167,9 +166,9 @@ class StatefulService {
} }
} }
SemaphoreHandle_t _accessMutex; SemaphoreHandle_t mutex_;
std::list<StateUpdateHandlerInfo_t> _updateHandlers; std::list<StateUpdateHandlerInfo_t> updateHandlers_;
std::list<StateHookHandlerInfo_t> _hookHandlers; std::list<StateHookHandlerInfo_t> hookHandlers_;
}; };
#endif // end StatefulService_h #endif // end StatefulService_h