12#include "utils/traits.h"
13#include "utils/variant_helpers.h"
40 { init_from (obj, other, clone_type) } -> std::same_as<void>;
44std::unique_ptr<Derived>
50 std::unique_ptr<Derived> cloned;
51 cloned = std::make_unique<Derived> (std::forward<Args> (args)...);
52 init_from (*cloned, obj, clone_type);
56template <CloneableObject Derived,
typename... Args>
57std::shared_ptr<Derived>
63 std::shared_ptr<Derived> cloned;
64 cloned = std::make_shared<Derived> (std::forward<Args> (args)...);
65 init_from (*cloned, obj, clone_type);
69template <CloneableObject Derived,
typename... Args>
76 auto unique_ptr = clone_unique (obj, clone_type, std::forward<Args> (args)...);
77 return unique_ptr.release ();
87 requires utils::QObjectDerived<Derived>
89 auto * cloned = clone_raw_ptr (obj, clone_type, std::forward<Args> (args)...);
92 cloned->setParent (parent);
98utils::QObjectUniquePtr<Derived>
104 requires utils::QObjectDerived<Derived>
106 return utils::QObjectUniquePtr<Derived> (
107 clone_qobject (obj, parent, clone_type, std::forward<Args> (args)...));
119template <
typename T,
typename Base>
123template <
typename Variant,
typename Base>
125 []<
typename... Ts> (std::variant<Ts...> *) {
128 "All types in Variant must inherit from Base");
129 }(
static_cast<Variant *
> (
nullptr));
140template <
typename T, std::
size_t N>
143 std::array<std::unique_ptr<T>, N> &dest,
144 const std::array<std::unique_ptr<T>, N> &src,
147 for (
size_t i = 0; i < N; ++i)
158 dest[i] = std::make_unique<T> (*src[i]);
176template <
typename T,
template <
typename...>
class Ptr>
179 std::vector<Ptr<T>> &dest,
180 const std::vector<Ptr<T>> &src,
184 dest.reserve (src.size ());
186 for (
const auto &ptr : src)
192 if constexpr (std::is_same_v<Ptr<T>, std::unique_ptr<T>>)
194 dest.push_back (clone_unique (
197 else if constexpr (std::is_same_v<Ptr<T>, std::shared_ptr<T>>)
199 dest.push_back (clone_shared (
205 if constexpr (std::is_same_v<Ptr<T>, std::unique_ptr<T>>)
207 dest.push_back (std::make_unique<T> (*ptr));
209 else if constexpr (std::is_same_v<Ptr<T>, std::shared_ptr<T>>)
211 dest.push_back (std::make_shared<T> (*ptr));
217 dest.push_back (
nullptr);
229template <
typename Container>
233 const Container &src,
246template <
typename Container,
typename Variant,
typename Base>
247 requires AllInheritFromBase<Variant, Base>
249clone_variant_container (
251 const Container &src,
256 for (
size_t i = 0; i < src.size (); ++i)
260 dest[i] = clone_unique_with_variant<Variant, Base> (
272 dest.reserve (src.size ());
274 for (
const auto &ptr : src)
279 clone_unique_with_variant<Variant, Base> (
284 dest.push_back (
nullptr);
295template <
typename Variant,
typename Container>
298clone_variant_container (
300 const Container &src,
303 using Base =
typename Container::value_type::element_type;
308 for (
size_t i = 0; i < src.size (); ++i)
312 dest[i] = clone_unique_with_variant<Variant, Base> (
325 dest.reserve (src.size ());
327 for (
const auto &ptr : src)
332 clone_unique_with_variant<Variant, Base> (
337 dest.push_back (
nullptr);
Concept to ensure all types in a variant inherit from a base class.
Concept that checks if a type is cloneable.
Concept that checks if a type is cloneable.
Concept to check if a type inherits from a base class.
void clone_unique_ptr_container(Container &dest, const Container &src, ObjectCloneType clone_type=ObjectCloneType::Snapshot)
Clones the elements of a container of std::unique_ptr into the destination container.
void clone_unique_ptr_array(std::array< std::unique_ptr< T >, N > &dest, const std::array< std::unique_ptr< T >, N > &src, ObjectCloneType clone_type=ObjectCloneType::Snapshot)
Clones the elements of a std::array of std::unique_ptr into the destination array.
@ NewIdentity
Creates a separately identified object.
@ Snapshot
Creates a snapshot of the object with the same identity.
void clone_ptr_vector(std::vector< Ptr< T > > &dest, const std::vector< Ptr< T > > &src, ObjectCloneType clone_type=ObjectCloneType::Snapshot)
Clones the elements of a std::vector of std::unique_ptr or std::shared_ptr into the destination vecto...