diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2021-01-31 13:52:55 +0000 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2021-02-04 19:29:49 +0000 |
commit | 6663bdedd1e21259ebca63d07b6f781c15e476c8 (patch) | |
tree | 6b23e4359140dd48d81dde43372e5f06313147d8 /utility/collection.hpp | |
parent | Restructure to allow a resource path and testing (diff) | |
download | ilt-6663bdedd1e21259ebca63d07b6f781c15e476c8.tar.bz2 ilt-6663bdedd1e21259ebca63d07b6f781c15e476c8.tar.xz ilt-6663bdedd1e21259ebca63d07b6f781c15e476c8.zip |
Collection can be shared/unique pointers
Diffstat (limited to 'utility/collection.hpp')
-rw-r--r-- | utility/collection.hpp | 16 |
1 files changed, 11 insertions, 5 deletions
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> |