11#include "utils/initializable_object.h"
13#include "utils/traits.h"
14#include "utils/variant_helpers.h"
41 { init_from (obj, other, clone_type) } -> std::same_as<void>;
45std::unique_ptr<Derived>
51 std::unique_ptr<Derived> cloned;
54 auto result = Derived::create_unique (std::forward<Args> (args)...);
57 cloned = std::move (result);
61 cloned = std::make_unique<Derived> (std::forward<Args> (args)...);
63 init_from (*cloned, obj, clone_type);
68std::shared_ptr<Derived>
74 std::shared_ptr<Derived> cloned;
75 if constexpr (utils::Initializable<Derived>)
78 Derived::template create_shared<Derived> (std::forward<Args> (args)...);
81 cloned = std::move (result);
85 cloned = std::make_shared<Derived> (std::forward<Args> (args)...);
87 init_from (*cloned, obj, clone_type);
98 auto unique_ptr = clone_unique (obj, clone_type, std::forward<Args> (args)...);
99 return unique_ptr.release ();
109 requires utils::QObjectDerived<Derived>
111 auto * cloned = clone_raw_ptr (obj, clone_type, std::forward<Args> (args)...);
114 cloned->setParent (parent);
120utils::QObjectUniquePtr<Derived>
121clone_unique_qobject (
126 requires utils::QObjectDerived<Derived>
128 return utils::QObjectUniquePtr<Derived> (
129 clone_qobject (obj, parent, clone_type, std::forward<Args> (args)...));
141template <
typename T,
typename Base>
145template <
typename Variant,
typename Base>
147 []<
typename... Ts> (std::variant<Ts...> *) {
150 "All types in Variant must inherit from Base");
151 }(
static_cast<Variant *
> (
nullptr));
162template <
typename T, std::
size_t N>
165 std::array<std::unique_ptr<T>, N> &dest,
166 const std::array<std::unique_ptr<T>, N> &src,
169 for (
size_t i = 0; i < N; ++i)
180 dest[i] = std::make_unique<T> (*src[i]);
198template <
typename T,
template <
typename...>
class Ptr>
201 std::vector<Ptr<T>> &dest,
202 const std::vector<Ptr<T>> &src,
206 dest.reserve (src.size ());
208 for (
const auto &ptr : src)
214 if constexpr (std::is_same_v<Ptr<T>, std::unique_ptr<T>>)
216 dest.push_back (clone_unique (
219 else if constexpr (std::is_same_v<Ptr<T>, std::shared_ptr<T>>)
221 dest.push_back (clone_shared (
227 if constexpr (std::is_same_v<Ptr<T>, std::unique_ptr<T>>)
229 dest.push_back (std::make_unique<T> (*ptr));
231 else if constexpr (std::is_same_v<Ptr<T>, std::shared_ptr<T>>)
233 dest.push_back (std::make_shared<T> (*ptr));
239 dest.push_back (
nullptr);
251template <
typename Container>
255 const Container &src,
268template <
typename Container,
typename Variant,
typename Base>
269 requires AllInheritFromBase<Variant, Base>
271clone_variant_container (
273 const Container &src,
278 for (
size_t i = 0; i < src.size (); ++i)
282 dest[i] = clone_unique_with_variant<Variant, Base> (
294 dest.reserve (src.size ());
296 for (
const auto &ptr : src)
301 clone_unique_with_variant<Variant, Base> (
306 dest.push_back (
nullptr);
317template <
typename Variant,
typename Container>
320clone_variant_container (
322 const Container &src,
325 using Base =
typename Container::value_type::element_type;
330 for (
size_t i = 0; i < src.size (); ++i)
334 dest[i] = clone_unique_with_variant<Variant, Base> (
347 dest.reserve (src.size ());
349 for (
const auto &ptr : src)
354 clone_unique_with_variant<Variant, Base> (
359 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...