33#ifndef GKO_PUBLIC_CORE_BASE_POLYMORPHIC_OBJECT_HPP_
34#define GKO_PUBLIC_CORE_BASE_POLYMORPHIC_OBJECT_HPP_
41#include <ginkgo/core/base/executor.hpp>
42#include <ginkgo/core/base/utils.hpp>
43#include <ginkgo/core/log/logger.hpp>
94 std::shared_ptr<const Executor> exec)
const
98 auto created = this->create_default_impl(std::move(exec));
100 exec_.get(),
this,
created.get());
127 std::unique_ptr<PolymorphicObject>
clone(
128 std::shared_ptr<const Executor> exec)
const
143 std::unique_ptr<PolymorphicObject>
clone()
const
145 return this->
clone(exec_);
162 exec_.get(),
other,
this);
165 exec_.get(),
other,
this);
184 template <
typename Derived,
typename Deleter>
186 "This function will be removed in a future release, the replacement "
187 "will copy instead of move. If a move is intended, use move_from "
194 exec_.get(),
other.get(),
this);
195 auto copied = this->copy_from_impl(std::move(
other));
197 exec_.get(),
other.get(),
this);
208 template <
typename Derived,
typename Deleter>
210 std::is_base_of<PolymorphicObject, std::decay_t<Derived>>::value,
221 const std::shared_ptr<const PolymorphicObject>&
other)
240 exec_.get(),
other.get(),
this);
241 auto moved = this->move_from_impl(
other.get());
243 exec_.get(),
other.get(),
this);
279 : exec_{std::
move(exec)}
283 explicit PolymorphicObject(
const PolymorphicObject&
other)
296 virtual std::unique_ptr<PolymorphicObject> create_default_impl(
297 std::shared_ptr<const Executor> exec)
const = 0;
307 virtual PolymorphicObject* copy_from_impl(
308 const PolymorphicObject*
other) = 0;
318 virtual PolymorphicObject* copy_from_impl(
319 std::unique_ptr<PolymorphicObject>
other) = 0;
329 virtual PolymorphicObject* move_from_impl(PolymorphicObject*
other) = 0;
339 virtual PolymorphicObject* move_from_impl(
340 std::unique_ptr<PolymorphicObject>
other) = 0;
348 virtual PolymorphicObject* clear_impl() = 0;
351 std::shared_ptr<const Executor> exec_;
373template <
typename AbstractObject,
typename PolymorphicBase = PolymorphicObject>
376 using PolymorphicBase::PolymorphicBase;
378 std::unique_ptr<AbstractObject> create_default(
379 std::shared_ptr<const Executor> exec)
const
381 return std::unique_ptr<AbstractObject>{
static_cast<AbstractObject*
>(
382 this->PolymorphicBase::create_default(std::move(exec)).release())};
385 std::unique_ptr<AbstractObject> create_default()
const
387 return std::unique_ptr<AbstractObject>{
static_cast<AbstractObject*
>(
388 this->PolymorphicBase::create_default().release())};
391 std::unique_ptr<AbstractObject>
clone(
392 std::shared_ptr<const Executor> exec)
const
394 return std::unique_ptr<AbstractObject>{
static_cast<AbstractObject*
>(
395 this->PolymorphicBase::clone(std::move(exec)).release())};
398 std::unique_ptr<AbstractObject>
clone()
const
400 return std::unique_ptr<AbstractObject>{
static_cast<AbstractObject*
>(
401 this->PolymorphicBase::clone().release())};
407 this->PolymorphicBase::copy_from(
other));
410 template <
typename Derived>
412 "This function will be removed in a future release, the replacement "
413 "will copy instead of move. If a move in intended, use move_to "
416 std::is_base_of<PolymorphicObject, std::decay_t<Derived>>::value,
420 this->PolymorphicBase::copy_from(std::move(
other)));
423 template <
typename Derived>
425 std::is_base_of<PolymorphicObject, std::decay_t<Derived>>::value,
427 copy_from(
const std::unique_ptr<Derived>&
other)
429 return copy_from(
other.get());
433 const std::shared_ptr<const PolymorphicObject>&
other)
435 return copy_from(
other.get());
441 this->PolymorphicBase::move_from(
other.get()));
446 return static_cast<AbstractObject*
>(this->PolymorphicBase::clear());
459#define GKO_ENABLE_SELF(_type) \
460 _type* self() noexcept { return static_cast<_type*>(this); } \
462 const _type* self() const noexcept \
464 return static_cast<const _type*>(this); \
498template <
typename ResultType>
540template <
typename R,
typename T>
541std::unique_ptr<
R, std::function<
void(
R*)>> copy_and_convert_to_impl(
542 std::shared_ptr<const Executor> exec,
T* obj)
545 if (
obj_as_r !=
nullptr && obj->get_executor() == exec) {
549 auto copy = R::create(exec);
551 return {copy.release(), std::default_delete<R>{}};
556template <
typename R,
typename T>
557std::shared_ptr<R> copy_and_convert_to_impl(
558 std::shared_ptr<const Executor> exec, std::shared_ptr<T> obj)
560 auto obj_as_r = std::dynamic_pointer_cast<R>(obj);
561 if (
obj_as_r !=
nullptr && obj->get_executor() == exec) {
564 auto copy = R::create(exec);
566 return {std::move(copy)};
590template <
typename R,
typename T>
592 std::shared_ptr<const Executor> exec,
T* obj)
594 return detail::copy_and_convert_to_impl<R>(std::move(exec), obj);
604template <
typename R,
typename T>
606 std::shared_ptr<const Executor> exec,
const T* obj)
608 return detail::copy_and_convert_to_impl<const R>(std::move(exec), obj);
629template <
typename R,
typename T>
631 std::shared_ptr<T> obj)
633 return detail::copy_and_convert_to_impl<R>(std::move(exec), obj);
644template <
typename R,
typename T>
646 std::shared_ptr<const Executor> exec, std::shared_ptr<const T> obj)
648 return detail::copy_and_convert_to_impl<const R>(std::move(exec), obj);
689template <
typename ConcreteObject,
typename PolymorphicBase = PolymorphicObject>
694 ConcreteObject, PolymorphicBase>::EnableAbstractPolymorphicObject;
696 std::unique_ptr<PolymorphicObject> create_default_impl(
697 std::shared_ptr<const Executor> exec)
const override
709 std::unique_ptr<PolymorphicObject>
other)
override
722 std::unique_ptr<PolymorphicObject>
other)
override
751template <
typename ConcreteType,
typename ResultType = ConcreteType>
775template <
typename ConcreteType>
778 template <
typename...
Args>
779 static std::unique_ptr<ConcreteType> create(
Args&&...
args)
781 return std::unique_ptr<ConcreteType>(
ConvertibleTo interface is used to mark that the implementer can be converted to the object of Result...
Definition polymorphic_object.hpp:499
virtual void convert_to(result_type *result) const =0
Converts the implementer to an object of type result_type.
virtual void move_to(result_type *result)=0
Converts the implementer to an object of type result_type by moving data from this object.
This mixin inherits from (a subclass of) PolymorphicObject and provides a base implementation of a ne...
Definition polymorphic_object.hpp:374
This mixin implements a static create() method on ConcreteType that dynamically allocates the memory,...
Definition polymorphic_object.hpp:776
This mixin is used to enable a default PolymorphicObject::copy_from() implementation for objects that...
Definition polymorphic_object.hpp:752
void move_to(result_type *result) override
Converts the implementer to an object of type result_type by moving data from this object.
Definition polymorphic_object.hpp:760
void convert_to(result_type *result) const override
Converts the implementer to an object of type result_type.
Definition polymorphic_object.hpp:758
This mixin inherits from (a subclass of) PolymorphicObject and provides a base implementation of a ne...
Definition polymorphic_object.hpp:691
A PolymorphicObject is the abstract base for all "heavy" objects in Ginkgo that behave polymorphicall...
Definition polymorphic_object.hpp:72
PolymorphicObject * copy_from(const PolymorphicObject *other)
Copies another object into this object.
Definition polymorphic_object.hpp:159
PolymorphicObject * copy_from(const std::shared_ptr< const PolymorphicObject > &other)
Copies another object into this object.
Definition polymorphic_object.hpp:220
std::enable_if_t< std::is_base_of< PolymorphicObject, std::decay_t< Derived > >::value, PolymorphicObject > * copy_from(const std::unique_ptr< Derived, Deleter > &other)
Copies another object into this object.
Definition polymorphic_object.hpp:212
PolymorphicObject * move_from(ptr_param< PolymorphicObject > other)
Moves another object into this object.
Definition polymorphic_object.hpp:237
std::unique_ptr< PolymorphicObject > create_default() const
Creates a new "default" object of the same dynamic type as this object.
Definition polymorphic_object.hpp:112
std::unique_ptr< PolymorphicObject > clone() const
Creates a clone of the object.
Definition polymorphic_object.hpp:143
std::shared_ptr< const Executor > get_executor() const noexcept
Returns the Executor of the object.
Definition polymorphic_object.hpp:263
std::unique_ptr< PolymorphicObject > create_default(std::shared_ptr< const Executor > exec) const
Creates a new "default" object of the same dynamic type as this object.
Definition polymorphic_object.hpp:93
std::unique_ptr< PolymorphicObject > clone(std::shared_ptr< const Executor > exec) const
Creates a clone of the object.
Definition polymorphic_object.hpp:127
PolymorphicObject * clear()
Transforms the object into its default state.
Definition polymorphic_object.hpp:256
EnableLogging is a mixin which should be inherited by any class which wants to enable logging.
Definition logger.hpp:777
This class is used for function parameters in the place of raw pointers.
Definition utils_helper.hpp:71
The Ginkgo namespace.
Definition abstract_factory.hpp:48
constexpr T one()
Returns the multiplicative identity for T.
Definition math.hpp:803
std::unique_ptr< R, std::function< void(R *)> copy_and_convert_to)(std::shared_ptr< const Executor > exec, T *obj)
Converts the object to R and places it on Executor exec.
Definition polymorphic_object.hpp:591
detail::cloned_type< Pointer > clone(const Pointer &p)
Creates a unique clone of the object pointed to by p.
Definition utils_helper.hpp:203