From a7c061a9fb40be3d74b0e90cf632c4c23144fff9 Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nwpope@utu.fi>
Date: Fri, 17 Jul 2020 22:11:58 +0300
Subject: [PATCH] Add property change wrapper

---
 applications/vision/src/main.cpp                   |  5 +----
 components/common/cpp/include/ftl/configurable.hpp | 13 +++++++++++++
 2 files changed, 14 insertions(+), 4 deletions(-)

diff --git a/applications/vision/src/main.cpp b/applications/vision/src/main.cpp
index 88069323f..c27129e45 100644
--- a/applications/vision/src/main.cpp
+++ b/applications/vision/src/main.cpp
@@ -184,10 +184,7 @@ static void run(ftl::Configurable *root) {
 	float latency = 0.0f;
 	int64_t stats_time = 0;
 
-	quiet = root->value("quiet", false);
-	root->on("quiet", [root](const ftl::config::Event &e) {
-		quiet = root->value("quiet", false);
-	});
+	root->on("quiet", quiet, false);
 
 	auto *pipeline = ftl::config::create<ftl::operators::Graph>(root, "pipeline");
 	pipeline->append<ftl::operators::DetectAndTrack>("facedetection")->value("enabled", false);
diff --git a/components/common/cpp/include/ftl/configurable.hpp b/components/common/cpp/include/ftl/configurable.hpp
index c32b3dce4..c0b8ae802 100644
--- a/components/common/cpp/include/ftl/configurable.hpp
+++ b/components/common/cpp/include/ftl/configurable.hpp
@@ -95,6 +95,19 @@ class Configurable {
 	 */
 	void on(const std::string &prop, std::function<void(const config::Event&)>);
 
+	//void on(const std::string &prop, const std::function<void()> &);
+
+	template <typename T>
+	void on(const std::string &prop, T &v) {
+		on(prop, [&v,this,prop](const config::Event &e) { v = *this->get<T>(prop); });
+	}
+
+	template <typename T>
+	void on(const std::string &prop, T &v, const T &def) {
+		v = this->value(prop, def);
+		on(prop, [&v,this,prop](const config::Event &e) { v = *this->get<T>(prop); });
+	}
+
 	void patchPtr(nlohmann::json &newcfg) { config_ = &newcfg; }
 
 	/**
-- 
GitLab