From 6663bdedd1e21259ebca63d07b6f781c15e476c8 Mon Sep 17 00:00:00 2001
From: Dan Goodliffe <dan@randomdan.homeip.net>
Date: Sun, 31 Jan 2021 13:52:55 +0000
Subject: Collection can be shared/unique pointers

---
 utility/collection.hpp | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)

(limited to 'utility')

diff --git a/utility/collection.hpp b/utility/collection.hpp
index 48eae0e..79c331a 100644
--- a/utility/collection.hpp
+++ b/utility/collection.hpp
@@ -3,10 +3,11 @@
 
 #include <algorithm>
 #include <memory>
+#include <type_traits>
 
-template<typename Object> class Collection {
+template<typename Object, bool shared = true> class Collection {
 public:
-	using Ptr = std::shared_ptr<Object>;
+	using Ptr = std::conditional_t<shared, std::shared_ptr<Object>, std::unique_ptr<Object>>;
 	using Objects = std::vector<Ptr>;
 	Objects objects;
 
@@ -14,9 +15,14 @@ public:
 	auto
 	create(Params &&... params)
 	{
-		auto obj = std::make_shared<T>(std::forward<Params>(params)...);
-		objects.emplace_back(obj);
-		return obj;
+		if constexpr (shared) {
+			auto obj = std::make_shared<T>(std::forward<Params>(params)...);
+			objects.emplace_back(obj);
+			return obj;
+		}
+		else {
+			return static_cast<T *>(objects.emplace_back(std::make_unique<T>(std::forward<Params>(params)...)).get());
+		}
 	}
 
 	template<typename T = Object, typename M = void, typename... Params>
-- 
cgit v1.2.3