diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2025-03-22 12:17:57 +0000 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2025-03-22 12:17:57 +0000 |
commit | fcca8bc835db65ac170d1148d52a815df8838d53 (patch) | |
tree | c28d017961cb05f6360042e7a089f10641f174b4 /lib | |
parent | Add ManyPtr which tracks specified subclasses (diff) | |
download | ilt-fcca8bc835db65ac170d1148d52a815df8838d53.tar.bz2 ilt-fcca8bc835db65ac170d1148d52a815df8838d53.tar.xz ilt-fcca8bc835db65ac170d1148d52a815df8838d53.zip |
Invert how shared/unique is specified for Collection
Template param is a pointer now, typedefs added for ease.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/collection.h | 9 |
1 files changed, 6 insertions, 3 deletions
diff --git a/lib/collection.h b/lib/collection.h index 6802bcb..2deefb9 100644 --- a/lib/collection.h +++ b/lib/collection.h @@ -6,11 +6,11 @@ #include <type_traits> #include <vector> -template<typename Object, bool shared = true> class Collection { +template<typename Ptr> class Collection { public: virtual ~Collection() = default; - using Ptr = std::conditional_t<shared, std::shared_ptr<Object>, std::unique_ptr<Object>>; + using Object = Ptr::element_type; using Objects = std::vector<Ptr>; Objects objects; @@ -19,7 +19,7 @@ public: create(Params &&... params) requires std::is_base_of_v<Object, T> { - if constexpr (shared) { + if constexpr (requires(Ptr ptr) { ptr = std::make_shared<T>(std::forward<Params>(params)...); }) { auto obj = std::make_shared<T>(std::forward<Params>(params)...); objects.emplace_back(obj); return obj; @@ -129,3 +129,6 @@ protected: }); } }; + +template<typename T> using SharedCollection = Collection<std::shared_ptr<T>>; +template<typename T> using UniqueCollection = Collection<std::unique_ptr<T>>; |