MostlyHarmless 0.0.1
Loading...
Searching...
No Matches
mostlyharmless_DatabasePropertyWatcher.h
Go to the documentation of this file.
1//
2// Created by Syl Morrison on 13/04/2025.
3//
4
5#ifndef MOSTLYHARMLESS_DATABASEPROPERTYWATCHER_H
6#define MOSTLYHARMLESS_DATABASEPROPERTYWATCHER_H
8#include <mostly_harmless/mostlyharmless_Concepts.h>
11namespace mostly_harmless::data {
18 template <DatabaseStorageType T>
19 class DatabasePropertyWatcher final {
20 private:
21 struct Private {};
22
23 public:
27 DatabasePropertyWatcher(Private, DatabaseState&& databaseState, std::string_view name, int pollFrequencyMs, std::function<void(const T&)>&& callback) : m_databaseState(std::move(databaseState)),
28 m_propertyNameToWatch(name),
29 m_pollFrequencyMs(pollFrequencyMs),
30 m_callback(std::move(callback)) {
31 m_proxyThis = utils::Proxy<DatabasePropertyWatcher<T>>::create(this);
32 auto proxyCopy = m_proxyThis;
33 m_timer.action = [this, proxyCopy]() -> void {
34 if (!proxyCopy->isValid()) {
35 return;
36 }
37 timerCallback();
38 };
39 m_timer.run(m_pollFrequencyMs);
40 }
41
45 DatabasePropertyWatcher(const DatabasePropertyWatcher& /*other*/) = delete;
49 DatabasePropertyWatcher(DatabasePropertyWatcher&& /*other*/) noexcept = delete;
50
54 ~DatabasePropertyWatcher() noexcept {
55 m_proxyThis->null();
56 }
57
61 auto operator=(const DatabasePropertyWatcher& /*other*/) -> DatabasePropertyWatcher = delete;
62
66 auto operator=(DatabasePropertyWatcher&& /*other*/) noexcept -> DatabasePropertyWatcher = delete;
67
77 static auto tryCreate(const DatabaseState& databaseState, std::string_view propertyName, int pollFrequencyMs, std::function<void(const T&)>&& callback) -> std::unique_ptr<DatabasePropertyWatcher> {
78 auto databaseCopyOpt = databaseState.duplicate();
79 if (!databaseCopyOpt) {
80 return nullptr;
81 }
82 return std::make_unique<DatabasePropertyWatcher<T>>(Private{}, std::move(*databaseCopyOpt), propertyName, pollFrequencyMs, std::move(callback));
83 }
84
85
86 private:
87 auto timerCallback() -> void {
88 if (!m_callback) {
89 return;
90 }
91 const auto getRes = m_databaseState.get<T>(m_propertyNameToWatch);
92 if (!getRes) {
93 assert(false);
94 return;
95 }
96 const auto hasChanged = m_previousValue != getRes;
97 m_previousValue = *getRes;
98 if (!hasChanged) {
99 return;
100 }
101 m_callback(*getRes);
102 }
103
104 DatabaseState m_databaseState;
105 std::string m_propertyNameToWatch;
106 int m_pollFrequencyMs;
107 std::function<void(const T&)> m_callback;
108 std::shared_ptr<utils::Proxy<DatabasePropertyWatcher<T>>> m_proxyThis;
109 utils::Timer m_timer;
110 std::optional<T> m_previousValue{};
111 };
112} // namespace mostly_harmless::data
113#endif // MOSTLYHARMLESS_DATABASEPROPERTYWATCHER_H
DatabasePropertyWatcher(const DatabasePropertyWatcher &)=delete
static auto tryCreate(const DatabaseState &databaseState, std::string_view propertyName, int pollFrequencyMs, std::function< void(const T &)> &&callback) -> std::unique_ptr< DatabasePropertyWatcher >
Definition mostlyharmless_DatabasePropertyWatcher.h:77
DatabasePropertyWatcher(DatabasePropertyWatcher &&) noexcept=delete
auto operator=(const DatabasePropertyWatcher &) -> DatabasePropertyWatcher=delete
auto operator=(DatabasePropertyWatcher &&) noexcept -> DatabasePropertyWatcher=delete
Represents a connection to a sqlite database.
Definition mostlyharmless_DatabaseState.h:67
auto get(std::string_view name) -> std::optional< T >
Definition mostlyharmless_DatabaseState.h:222
Helper class for managing lifetimes of captured references in a lambda.
Definition mostlyharmless_Proxy.h:72
Contains classes and functions related to data management.
Definition mostlyharmless_DatabaseState.h:16