From 8f47567557ab11f5d4ae328ba695f6cc8a95b730 Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nwpope@utu.fi>
Date: Sat, 2 Nov 2019 21:31:31 +0200
Subject: [PATCH] Operator refactor is working

---
 .../include/ftl/operators/operator.hpp        | 43 ++++++++++++++++---
 components/operators/src/operator.cpp         | 20 ++++++---
 components/renderers/cpp/CMakeLists.txt       |  2 +-
 3 files changed, 52 insertions(+), 13 deletions(-)

diff --git a/components/operators/include/ftl/operators/operator.hpp b/components/operators/include/ftl/operators/operator.hpp
index 4d4359e6d..fe9b52f2c 100644
--- a/components/operators/include/ftl/operators/operator.hpp
+++ b/components/operators/include/ftl/operators/operator.hpp
@@ -53,10 +53,36 @@ class Operator {
 	ftl::Configurable *config_;
 };
 
+namespace detail {
+
+struct ConstructionHelperBase {
+	ConstructionHelperBase(ftl::Configurable *cfg) : config(cfg) {}
+	virtual ~ConstructionHelperBase() {}
+	virtual ftl::operators::Operator *make()=0;
+
+	ftl::Configurable *config;
+};
+
+template <typename T>
+struct ConstructionHelper : public ConstructionHelperBase {
+	ConstructionHelper(ftl::Configurable *cfg) : ConstructionHelperBase(cfg) {}
+	~ConstructionHelper() {}
+	ftl::operators::Operator *make() override {
+		return new T(config);
+	}
+};
+
+struct OperatorNode {
+	ConstructionHelperBase *maker;
+	std::vector<ftl::operators::Operator*> instances;
+};
+
+}
+
 /**
  * Represent a sequential collection of operators. Each operator created is
  * added to an internal list and then applied to a frame in the order they were
- * created.
+ * created. A directed acyclic graph can also be formed.
  */
 class Graph : public ftl::Configurable {
 	public:
@@ -64,23 +90,28 @@ class Graph : public ftl::Configurable {
     ~Graph();
 
 	template <typename T>
-	Operator *append(const std::string &name);
+	ftl::Configurable *append(const std::string &name);
 
 	bool apply(ftl::rgbd::Frame &in, ftl::rgbd::Frame &out, ftl::rgbd::Source *s, cudaStream_t stream=0);
 	bool apply(ftl::rgbd::FrameSet &in, ftl::rgbd::FrameSet &out, cudaStream_t stream=0);
 	bool apply(ftl::rgbd::FrameSet &in, ftl::rgbd::Frame &out, ftl::rgbd::Source *s, cudaStream_t stream=0);
 
 	private:
-	std::list<ftl::operators::Operator*> operators_;
-	Operator *_append(Operator *f);
+	std::list<ftl::operators::detail::OperatorNode> operators_;
+	std::map<std::string, ftl::Configurable*> configs_;
+
+	ftl::Configurable *_append(ftl::operators::detail::ConstructionHelperBase*);
 };
 
 }
 }
 
 template <typename T>
-ftl::operators::Operator *ftl::operators::Graph::append(const std::string &name) {
-	return _append(dynamic_cast<ftl::operators::Operator*>(ftl::create<T>(this, name)));
+ftl::Configurable *ftl::operators::Graph::append(const std::string &name) {
+	if (configs_.find(name) == configs_.end()) {
+		configs_[name] = ftl::create<ftl::Configurable>(this, name);
+	}
+	return _append(new ftl::operators::detail::ConstructionHelper<T>(configs_[name]));
 }
 
 #endif  // _FTL_OPERATORS_OPERATOR_HPP_
diff --git a/components/operators/src/operator.cpp b/components/operators/src/operator.cpp
index 14ae77e35..c877977a9 100644
--- a/components/operators/src/operator.cpp
+++ b/components/operators/src/operator.cpp
@@ -41,16 +41,24 @@ Graph::~Graph() {
 bool Graph::apply(Frame &in, Frame &out, Source *s, cudaStream_t stream) {
 	if (!value("enabled", true)) return false;
 
-	for (auto i : operators_) {
-		if (i->enabled()) {
-			i->apply(in, out, s, stream);
+	for (auto &i : operators_) {
+		// Make sure there are enough instances
+		if (i.instances.size() < 1) {
+			i.instances.push_back(i.maker->make());
+		}
+
+		auto *instance = i.instances[0];
+
+		if (instance->enabled()) {
+			instance->apply(in, out, s, stream);
 		}
 	}
 
 	return true;
 }
 
-Operator *Graph::_append(Operator *f) {
-	operators_.push_back(f);
-	return f;
+ftl::Configurable *Graph::_append(ftl::operators::detail::ConstructionHelperBase *m) {
+	auto &o = operators_.emplace_back();
+	o.maker = m;
+	return m->config;
 }
diff --git a/components/renderers/cpp/CMakeLists.txt b/components/renderers/cpp/CMakeLists.txt
index 89cf408a3..14bacda71 100644
--- a/components/renderers/cpp/CMakeLists.txt
+++ b/components/renderers/cpp/CMakeLists.txt
@@ -18,6 +18,6 @@ target_include_directories(ftlrender PUBLIC
 	$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
 	$<INSTALL_INTERFACE:include>
 	PRIVATE src)
-target_link_libraries(ftlrender ftlrgbd ftlcommon ftlfilter Eigen3::Eigen Threads::Threads ${OpenCV_LIBS})
+target_link_libraries(ftlrender ftlrgbd ftlcommon Eigen3::Eigen Threads::Threads ${OpenCV_LIBS})
 
 #ADD_SUBDIRECTORY(test)
-- 
GitLab