Skip to content

Commit 67ccc49

Browse files
authored
Merge pull request #66 from SLM-Audio/syl/refactor-database-property-listener
rename to DatabasePropertyWatcher, return unique_ptr instead of optional
2 parents 2d64e72 + 0456d5d commit 67ccc49

File tree

6 files changed

+51
-35
lines changed

6 files changed

+51
-35
lines changed

include/mostly_harmless/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@ set(MOSTLYHARMLESS_HEADERS
2727
${CMAKE_CURRENT_SOURCE_DIR}/utils/mostlyharmless_Proxy.h
2828
${CMAKE_CURRENT_SOURCE_DIR}/utils/mostlyharmless_NoDenormals.h
2929
${CMAKE_CURRENT_SOURCE_DIR}/data/mostlyharmless_DatabaseState.h
30-
${CMAKE_CURRENT_SOURCE_DIR}/data/mostlyharmless_DatabasePropertyListener.h
30+
${CMAKE_CURRENT_SOURCE_DIR}/data/mostlyharmless_DatabasePropertyWatcher.h
3131
${PLATFORM_HEADERS}
3232
PARENT_SCOPE)

include/mostly_harmless/data/mostlyharmless_DatabasePropertyListener.h renamed to include/mostly_harmless/data/mostlyharmless_DatabasePropertyWatcher.h

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// Created by Syl Morrison on 13/04/2025.
33
//
44

5-
#ifndef MOSTLYHARMLESS_DATABASEPROPERTYLISTENER_H
6-
#define MOSTLYHARMLESS_DATABASEPROPERTYLISTENER_H
5+
#ifndef MOSTLYHARMLESS_DATABASEPROPERTYWATCHER_H
6+
#define MOSTLYHARMLESS_DATABASEPROPERTYWATCHER_H
77
#include <mostly_harmless/data/mostlyharmless_DatabaseState.h>
88
#include <mostly_harmless/mostlyharmless_Concepts.h>
99
#include <mostly_harmless/utils/mostlyharmless_Timer.h>
@@ -16,50 +16,70 @@ namespace mostly_harmless::data {
1616
* @tparam T Any type satisfying DatabaseStorageType
1717
*/
1818
template <DatabaseStorageType T>
19-
class DatabasePropertyListener final {
19+
class DatabasePropertyWatcher final {
2020
private:
2121
struct Private {};
2222

2323
public:
2424
/**
2525
* @private
2626
*/
27-
DatabasePropertyListener(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_callback(callback) {
30-
m_proxyThis = utils::Proxy<DatabasePropertyListener<T>>::create(this);
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);
3132
auto proxyCopy = m_proxyThis;
3233
m_timer.action = [this, proxyCopy]() -> void {
3334
if (!proxyCopy->isValid()) {
3435
return;
3536
}
3637
timerCallback();
3738
};
38-
m_timer.run(pollFrequencyMs);
39+
m_timer.run(m_pollFrequencyMs);
3940
}
4041

42+
/**
43+
* Non copyable
44+
*/
45+
DatabasePropertyWatcher(const DatabasePropertyWatcher& /*other*/) = delete;
46+
/**
47+
* Non moveable
48+
*/
49+
DatabasePropertyWatcher(DatabasePropertyWatcher&& /*other*/) noexcept = delete;
50+
4151
/**
4252
* @private
4353
*/
44-
~DatabasePropertyListener() noexcept {
54+
~DatabasePropertyWatcher() noexcept {
4555
m_proxyThis->null();
4656
}
4757

4858
/**
49-
* Attempts to create a DatabasePropertyListener for the given property name.
59+
* Non copyable
60+
*/
61+
auto operator=(const DatabasePropertyWatcher& /*other*/) -> DatabasePropertyWatcher = delete;
62+
63+
/**
64+
* Non moveable
65+
*/
66+
auto operator=(DatabasePropertyWatcher&& /*other*/) noexcept -> DatabasePropertyWatcher = delete;
67+
68+
/**
69+
* Attempts to create a DatabasePropertyWatcher for the given property name.
5070
* As sqlite requires database access across different processes to each have their own unique connection, we first call `DatabaseState::duplicate` to create a new connection specifically for this thread.
51-
* @param databaseState A const reference to the database connection to duplicate for this PropertyListener.
71+
* @param databaseState A const reference to the database connection to duplicate for this PropertyWatcher.
5272
* @param propertyName The name of the property to watch.
5373
* @param pollFrequencyMs The interval in milliseconds to poll the database for changes at.
5474
* @param callback A lambda to be invoked from a background timer thread on property value change.
55-
* @return A DatabasePropertyListener for the given property name if successful, nullopt otherwise.
75+
* @return A DatabasePropertyWatcher for the given property name if successful, nullptr otherwise.
5676
*/
57-
static auto tryCreate(const DatabaseState& databaseState, std::string_view propertyName, int pollFrequencyMs, std::function<void(const T&)>&& callback) -> std::optional<DatabasePropertyListener> {
77+
static auto tryCreate(const DatabaseState& databaseState, std::string_view propertyName, int pollFrequencyMs, std::function<void(const T&)>&& callback) -> std::unique_ptr<DatabasePropertyWatcher> {
5878
auto databaseCopyOpt = databaseState.duplicate();
5979
if (!databaseCopyOpt) {
60-
return {};
80+
return nullptr;
6181
}
62-
return std::make_optional<DatabasePropertyListener<T>>(Private{}, std::move(*databaseCopyOpt), propertyName, pollFrequencyMs, std::move(callback));
82+
return std::make_unique<DatabasePropertyWatcher<T>>(Private{}, std::move(*databaseCopyOpt), propertyName, pollFrequencyMs, std::move(callback));
6383
}
6484

6585

@@ -73,12 +93,7 @@ namespace mostly_harmless::data {
7393
assert(false);
7494
return;
7595
}
76-
auto hasChanged{ false };
77-
if (!m_previousValue.has_value()) {
78-
hasChanged = true;
79-
} else {
80-
hasChanged = *m_previousValue != *getRes;
81-
}
96+
const auto hasChanged = m_previousValue != getRes;
8297
m_previousValue = *getRes;
8398
if (!hasChanged) {
8499
return;
@@ -88,10 +103,11 @@ namespace mostly_harmless::data {
88103

89104
DatabaseState m_databaseState;
90105
std::string m_propertyNameToWatch;
106+
int m_pollFrequencyMs;
91107
std::function<void(const T&)> m_callback;
92-
std::shared_ptr<utils::Proxy<DatabasePropertyListener<T>>> m_proxyThis;
108+
std::shared_ptr<utils::Proxy<DatabasePropertyWatcher<T>>> m_proxyThis;
93109
utils::Timer m_timer;
94110
std::optional<T> m_previousValue{};
95111
};
96112
} // namespace mostly_harmless::data
97-
#endif // MOSTLYHARMLESS_DATABASEPROPERTYLISTENER_H
113+
#endif // MOSTLYHARMLESS_DATABASEPROPERTYWATCHER_H

source/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ set(MOSTLYHARMLESS_SOURCES
3333
${CMAKE_CURRENT_SOURCE_DIR}/utils/mostlyharmless_Hash.cpp
3434
${CMAKE_CURRENT_SOURCE_DIR}/utils/mostlyharmless_Macros.cpp
3535
${CMAKE_CURRENT_SOURCE_DIR}/data/mostlyharmless_DatabaseState.cpp
36-
${CMAKE_CURRENT_SOURCE_DIR}/data/mostlyharmless_DatabasePropertyListener.cpp
36+
${CMAKE_CURRENT_SOURCE_DIR}/data/mostlyharmless_DatabasePropertyWatcher.cpp
3737
${PLATFORM_SOURCES}
3838
PARENT_SCOPE)
3939
message(STATUS ${MOSTLYHARMLESS_PLATFORM_SOURCES})

source/data/mostlyharmless_DatabasePropertyListener.cpp

Lines changed: 0 additions & 4 deletions
This file was deleted.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
//
2+
// Created by Syl Morrison on 13/04/2025.
3+
//
4+
#include <mostly_harmless/data/mostlyharmless_DatabasePropertyWatcher.h>

tests/data/mostlyharmless_DatabaseStateTests.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//
44
#include <mostly_harmless/utils/mostlyharmless_Directories.h>
55
#include <mostly_harmless/data/mostlyharmless_DatabaseState.h>
6-
#include <mostly_harmless/data/mostlyharmless_DatabasePropertyListener.h>
6+
#include <mostly_harmless/data/mostlyharmless_DatabasePropertyWatcher.h>
77
#include <catch2/catch_test_macros.hpp>
88
#include <catch2/matchers/catch_matchers_floating_point.hpp>
99

@@ -59,9 +59,9 @@ namespace mostly_harmless::testing {
5959
std::filesystem::remove(dbFile);
6060
}
6161

62-
// SECTION("Test Invalid Location") {
63-
// tryCreateDatabase<false>("INVALID LOCATION", {});
64-
// }
62+
SECTION("Test Invalid Location") {
63+
tryCreateDatabase<false>("INVALID LOCATION", {});
64+
}
6565

6666
SECTION("Test In-Memory") {
6767
tryCreateDatabase<true>(":memory:", {});
@@ -81,7 +81,7 @@ namespace mostly_harmless::testing {
8181
std::filesystem::remove(dbFile);
8282
}
8383

84-
SECTION("Test DatabasePropertyListener") {
84+
SECTION("Test DatabasePropertyWatcher") {
8585
for (auto i = 0; i < 100; ++i) {
8686
{
8787
auto databaseOpt = tryCreateDatabase<true>(dbFile, { { "test", 0 } });
@@ -93,7 +93,7 @@ namespace mostly_harmless::testing {
9393
wasPropertyChanged.store(true);
9494
newValue = x;
9595
};
96-
auto listener = data::DatabasePropertyListener<int>::tryCreate(database, "test", 1, std::move(onPropertyChanged));
96+
auto listener = data::DatabasePropertyWatcher<int>::tryCreate(database, "test", 1, std::move(onPropertyChanged));
9797
REQUIRE(listener);
9898
database.set<int>("test", 10);
9999
std::this_thread::sleep_for(std::chrono::milliseconds(50));

0 commit comments

Comments
 (0)