33 #ifndef GKO_CORE_BASE_UTILS_HPP_ 34 #define GKO_CORE_BASE_UTILS_HPP_ 37 #include <ginkgo/core/base/exception.hpp> 38 #include <ginkgo/core/base/std_extensions.hpp> 39 #include <ginkgo/core/base/types.hpp> 44 #include <type_traits> 62 struct pointee_impl {};
65 struct pointee_impl<T *> {
69 template <
typename T,
typename Deleter>
70 struct pointee_impl<
std::unique_ptr<T, Deleter>> {
75 struct pointee_impl<
std::shared_ptr<T>> {
80 using pointee =
typename pointee_impl<typename std::decay<T>::type>::type;
83 template <
typename T,
typename =
void>
84 struct is_clonable_impl : std::false_type {};
87 struct is_clonable_impl<T, xstd::void_t<decltype(std::declval<T>().clone())>>
91 constexpr
bool is_clonable()
93 return is_clonable_impl<typename std::decay<T>::type>::value;
97 template <
typename T,
typename =
void>
98 struct is_clonable_to_impl : std::false_type {};
100 template <
typename T>
101 struct is_clonable_to_impl<
102 T, xstd::void_t<decltype(std::declval<T>().clone(
103 std::declval<std::shared_ptr<const Executor>>()))>>
106 template <
typename T>
107 constexpr
bool is_clonable_to()
109 return is_clonable_to_impl<typename std::decay<T>::type>::value;
113 template <
typename T>
114 struct have_ownership_impl : std::false_type {};
116 template <
typename T,
typename Deleter>
117 struct have_ownership_impl<
std::unique_ptr<T, Deleter>> : std::true_type {};
119 template <
typename T>
120 struct have_ownership_impl<std::shared_ptr<T>> : std::true_type {};
122 template <
typename T>
123 constexpr
bool have_ownership()
125 return have_ownership_impl<typename std::decay<T>::type>::value;
129 template <
typename Po
inter>
131 std::unique_ptr<typename std::remove_cv<pointee<Pointer>>::type>;
134 template <
typename Po
inter>
135 using shared_type = std::shared_ptr<pointee<Pointer>>;
155 template <
typename Po
inter>
156 inline detail::cloned_type<Pointer>
clone(
const Pointer &p)
158 static_assert(detail::is_clonable<detail::pointee<Pointer>>(),
159 "Object is not clonable");
160 return detail::cloned_type<Pointer>(
161 static_cast<typename std::remove_cv<detail::pointee<Pointer>
>::type *>(
162 p->clone().release()));
181 template <
typename Po
inter>
182 inline detail::cloned_type<Pointer>
clone(std::shared_ptr<const Executor> exec,
185 static_assert(detail::is_clonable_to<detail::pointee<Pointer>>(),
186 "Object is not clonable");
187 return detail::cloned_type<Pointer>(
188 static_cast<typename std::remove_cv<detail::pointee<Pointer>
>::type *>(
189 p->clone(std::move(exec)).release()));
205 template <
typename OwningPo
inter>
206 inline detail::shared_type<OwningPointer>
share(OwningPointer &&p)
208 static_assert(detail::have_ownership<OwningPointer>(),
209 "OwningPointer does not have ownership of the object");
210 return detail::shared_type<OwningPointer>(std::move(p));
226 template <
typename OwningPo
inter>
227 inline typename std::remove_reference<OwningPointer>::type &&
give(
230 static_assert(detail::have_ownership<OwningPointer>(),
231 "OwningPointer does not have ownership of the object");
246 template <
typename Po
inter>
247 inline typename std::enable_if<detail::have_ownership<Pointer>(),
248 detail::pointee<Pointer> *>::type
264 template <
typename Po
inter>
265 inline typename std::enable_if<!detail::have_ownership<Pointer>(),
266 detail::pointee<Pointer> *>::type
284 template <
typename T,
typename U>
285 inline typename std::decay<T>::type *
as(U *obj)
287 if (
auto p =
dynamic_cast<typename std::decay<T>::type *
>(obj)) {
290 throw NotSupported(__FILE__, __LINE__, __func__,
typeid(obj).name());
307 template <
typename T,
typename U>
308 inline const typename std::decay<T>::type *
as(
const U *obj)
310 if (
auto p =
dynamic_cast<const typename std::decay<T>::type *
>(obj)) {
313 throw NotSupported(__FILE__, __LINE__, __func__,
typeid(obj).name());
324 template <
typename T>
338 template <
typename T>
343 void operator()(pointer)
const noexcept {}
361 template <
typename T>
381 original_->copy_from(ptr);
392 template <
typename T>
395 using pointer =
const T *;
398 void operator()(pointer ptr)
const {
delete ptr; }
416 template <
typename T>
419 using value_type = T;
430 if (ptr->get_executor() == exec) {
432 handle_ = handle_type(ptr, [](pointer) {});
436 handle_ = handle_type(
gko::clone(std::move(exec), ptr).release(),
446 T *
get()
const {
return handle_.get(); }
457 using handle_type = std::unique_ptr<T, std::function<void(T *)>>;
473 template <
typename T>
484 #endif // GKO_CORE_BASE_UTILS_HPP_ detail::cloned_type< Pointer > clone(const Pointer &p)
Creates a unique clone of the object pointed to by p.
Definition: utils.hpp:156
temporary_clone(std::shared_ptr< const Executor > exec, pointer ptr)
Creates a temporary_clone.
Definition: utils.hpp:428
void operator()(pointer ptr) const
Deletes the object.
Definition: utils.hpp:379
A copy_back_deleter is a type of deleter that copies the data to an internally referenced object befo...
Definition: utils.hpp:362
The Ginkgo namespace.
Definition: abstract_factory.hpp:45
T * operator->() const
Calls a method on the underlying object.
Definition: utils.hpp:453
detail::shared_type< OwningPointer > share(OwningPointer &&p)
Marks the object pointed to by p as shared.
Definition: utils.hpp:206
This is a deleter that does not delete the object.
Definition: utils.hpp:325
std::remove_reference< OwningPointer >::type && give(OwningPointer &&p)
Marks that the object pointed to by p can be given to the callee.
Definition: utils.hpp:227
std::enable_if< detail::have_ownership< Pointer >), detail::pointee< Pointer > * >::type lend(const Pointer &p)
Returns a non-owning (plain) pointer to the object pointed to by p.
Definition: utils.hpp:249
void operator()(pointer) const noexcept
Deletes the object.
Definition: utils.hpp:334
std::decay< T >::type * as(U *obj)
Performs polymorphic type conversion.
Definition: utils.hpp:285
copy_back_deleter(pointer original)
Creates a new deleter object.
Definition: utils.hpp:372
A temporary_clone is a special smart pointer-like object that is designed to hold an object temporari...
Definition: utils.hpp:417
NotSupported is thrown in case it is not possible to perform the requested operation on the given obj...
Definition: exception.hpp:153
temporary_clone< T > make_temporary_clone(std::shared_ptr< const Executor > exec, T *ptr)
Creates a temporary_clone.
Definition: utils.hpp:474