From 6663bdedd1e21259ebca63d07b6f781c15e476c8 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe 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 #include +#include -template class Collection { +template class Collection { public: - using Ptr = std::shared_ptr; + using Ptr = std::conditional_t, std::unique_ptr>; using Objects = std::vector; Objects objects; @@ -14,9 +15,14 @@ public: auto create(Params &&... params) { - auto obj = std::make_shared(std::forward(params)...); - objects.emplace_back(obj); - return obj; + if constexpr (shared) { + auto obj = std::make_shared(std::forward(params)...); + objects.emplace_back(obj); + return obj; + } + else { + return static_cast(objects.emplace_back(std::make_unique(std::forward(params)...)).get()); + } } template -- cgit v1.2.3