From a9589259de476ddabdf901683575cf99b81560d2 Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nwpope@utu.fi>
Date: Wed, 22 May 2019 09:36:44 +0300
Subject: [PATCH] Fix static init bug in factories

---
 components/rgbd-sources/include/ftl/rgbd_source.hpp |  4 +++-
 components/rgbd-sources/src/disparity.cpp           |  9 +++++----
 components/rgbd-sources/src/disparity.hpp           |  2 +-
 components/rgbd-sources/src/rgbd_source.cpp         | 10 +++++-----
 4 files changed, 14 insertions(+), 11 deletions(-)

diff --git a/components/rgbd-sources/include/ftl/rgbd_source.hpp b/components/rgbd-sources/include/ftl/rgbd_source.hpp
index c43a1aaf2..43cce5090 100644
--- a/components/rgbd-sources/include/ftl/rgbd_source.hpp
+++ b/components/rgbd-sources/include/ftl/rgbd_source.hpp
@@ -66,6 +66,8 @@ class RGBDSource : public ftl::Configurable {
 	 * used as the instance name to construct.
 	 */
 	static RGBDSource *create(nlohmann::json &config, ftl::net::Universe *net);
+
+	static void init();
 	
 	protected:
 	static void _register(const std::string &n, std::function<RGBDSource*(nlohmann::json&,ftl::net::Universe*)> f);
@@ -78,7 +80,7 @@ class RGBDSource : public ftl::Configurable {
 	Eigen::Matrix4f pose_;
 
 	private:
-	static std::map<std::string,std::function<RGBDSource*(nlohmann::json&,ftl::net::Universe*)>> sources__;
+	static std::map<std::string,std::function<RGBDSource*(nlohmann::json&,ftl::net::Universe*)>> *sources__;
 };
 
 };
diff --git a/components/rgbd-sources/src/disparity.cpp b/components/rgbd-sources/src/disparity.cpp
index 76558a133..5141d438f 100644
--- a/components/rgbd-sources/src/disparity.cpp
+++ b/components/rgbd-sources/src/disparity.cpp
@@ -9,7 +9,7 @@
 using ftl::Disparity;
 
 std::map<std::string, std::function<Disparity*(nlohmann::json&)>>
-		Disparity::algorithms__;
+		*Disparity::algorithms__ = nullptr;
 
 Disparity::Disparity(nlohmann::json &config)
 	: 	config_(config),
@@ -17,14 +17,15 @@ Disparity::Disparity(nlohmann::json &config)
 		max_disp_(config["maximum"]) {}
 
 Disparity *Disparity::create(nlohmann::json &config) {
-	if (algorithms__.count(config["algorithm"]) != 1) return nullptr;
-	return algorithms__[config["algorithm"]](config);
+	if (algorithms__->count(config["algorithm"]) != 1) return nullptr;
+	return (*algorithms__)[config["algorithm"]](config);
 }
 
 void Disparity::_register(const std::string &n,
 		std::function<Disparity*(nlohmann::json&)> f) {
+	if (!algorithms__) algorithms__ = new std::map<std::string, std::function<Disparity*(nlohmann::json&)>>;
 	LOG(INFO) << "Register disparity algorithm: " << n;
-	algorithms__[n] = f;
+	(*algorithms__)[n] = f;
 }
 
 // TODO(Nick) Add remaining algorithms
diff --git a/components/rgbd-sources/src/disparity.hpp b/components/rgbd-sources/src/disparity.hpp
index ed7f6b70f..d7f24d583 100644
--- a/components/rgbd-sources/src/disparity.hpp
+++ b/components/rgbd-sources/src/disparity.hpp
@@ -54,7 +54,7 @@ class Disparity {
 	size_t max_disp_;
 	
 	private:
-	static std::map<std::string,std::function<Disparity*(nlohmann::json&)>> algorithms__;
+	static std::map<std::string,std::function<Disparity*(nlohmann::json&)>> *algorithms__;
 };
 };
 
diff --git a/components/rgbd-sources/src/rgbd_source.cpp b/components/rgbd-sources/src/rgbd_source.cpp
index 542360028..9738c313e 100644
--- a/components/rgbd-sources/src/rgbd_source.cpp
+++ b/components/rgbd-sources/src/rgbd_source.cpp
@@ -3,8 +3,7 @@
 using ftl::rgbd::RGBDSource;
 using ftl::Configurable;
 
-std::map<std::string, std::function<RGBDSource*(nlohmann::json&,ftl::net::Universe*)>>
-		RGBDSource::sources__;
+std::map<std::string, std::function<RGBDSource*(nlohmann::json&,ftl::net::Universe*)>> *RGBDSource::sources__ = nullptr;
 
 RGBDSource::RGBDSource(nlohmann::json &config) : Configurable(config), net_(nullptr) {
 
@@ -36,14 +35,15 @@ bool RGBDSource::snapshot(const std::string &fileprefix) {
 
 RGBDSource *RGBDSource::create(nlohmann::json &config, ftl::net::Universe *net) {
 	if (config["type"].type_name() != "string") return nullptr;
-	if (sources__.count(config["type"]) != 1) return nullptr;
-	return sources__[config["type"]](config, net);
+	if (sources__->count(config["type"]) != 1) return nullptr;
+	return (*sources__)[config["type"]](config, net);
 }
 
 void RGBDSource::_register(const std::string &n,
 		std::function<RGBDSource*(nlohmann::json&,ftl::net::Universe*)> f) {
+	if (!sources__) sources__ = new std::map<std::string, std::function<RGBDSource*(nlohmann::json&,ftl::net::Universe*)>>;
 	LOG(INFO) << "Register RGB-D Source: " << n;
-	sources__[n] = f;
+	(*sources__)[n] = f;
 }
 
 #include <ftl/net_source.hpp>
-- 
GitLab