Ginkgo  Generated from pipelines/3133041809 branch based on develop. Ginkgo version 2.0.0
A numerical linear algebra library targeting many-core architectures
temporary_conversion.hpp
1 // SPDX-FileCopyrightText: 2017 - 2026 The Ginkgo authors
2 //
3 // SPDX-License-Identifier: BSD-3-Clause
4 
5 #ifndef GKO_PUBLIC_CORE_BASE_TEMPORARY_CONVERSION_HPP_
6 #define GKO_PUBLIC_CORE_BASE_TEMPORARY_CONVERSION_HPP_
7 
8 
9 #include <memory>
10 #include <tuple>
11 #include <type_traits>
12 
13 #include <ginkgo/core/base/polymorphic_object.hpp>
14 #include <ginkgo/core/base/utils.hpp>
15 
16 
17 namespace gko {
18 namespace detail {
19 
20 
39 template <typename CopyType, typename OrigType>
40 class convert_back_deleter {
41  static_assert(std::is_base_of_v<ConvertibleTo<OrigType>, CopyType>,
42  "The CopyType must be convertible to OrigType");
43 
44 public:
45  using pointer = CopyType*;
46  using original_pointer = OrigType*;
47 
54  convert_back_deleter(original_pointer original) : original_{original} {}
55 
61  void operator()(pointer ptr) const
62  {
63  ptr->convert_to(original_);
64  delete ptr;
65  }
66 
67 private:
68  original_pointer original_;
69 };
70 
71 // specialization for constant objects, no need to convert back something that
72 // cannot change
73 template <typename CopyType, typename OrigType>
74 class convert_back_deleter<const CopyType, OrigType> {
75 public:
76  using pointer = const CopyType*;
77  using original_pointer = OrigType*;
78  convert_back_deleter(original_pointer) {}
79 
80  void operator()(pointer ptr) const { delete ptr; }
81 };
82 
83 
96 template <typename TargetType>
97 struct conversion_target_helper {
105  template <typename SourceType>
106  static std::unique_ptr<TargetType> create_empty(const SourceType* source)
107  {
108  return TargetType::create(source->get_executor());
109  }
110 };
111 
112 
113 } // namespace detail
114 
115 
128 template <typename T>
130  // std::function deleter allows to decide the (type of) deleter at
131  // runtime
132  using handle_type = std::unique_ptr<T, std::function<void(T*)>>;
133 
134  template <typename OtherT>
135  friend class temporary_conversion;
136 
137 public:
138  using value_type = T;
139  using pointer = T*;
140 
150  template <typename Other>
152  {
153  using DecayT = std::decay_t<T>;
154  using DecayOther = std::decay_t<Other>;
155  static_assert(std::is_same_v<T, Other> ||
156  std::is_base_of_v<DecayOther, DecayT> ||
157  std::is_base_of_v<DecayT, DecayOther> ||
158  (std::is_base_of_v<ConvertibleTo<DecayT>, DecayOther> &&
159  (std::is_const_v<T> ||
160  std::is_base_of_v<ConvertibleTo<DecayOther>, DecayT>)));
161  }
162 
178  template <typename Other>
179  void static dynamic_assert_compatibility(Other* other)
180  {
181  if (!other) {
182  GKO_NOT_SUPPORTED(other);
183  }
184  if constexpr (std::is_same_v<T, Other>) {
185  return;
186  }
187  if (dynamic_cast<T*>(other)) {
188  return;
189  }
190  using DecayT = std::decay_t<T>;
191  using DecayOther = std::decay_t<Other>;
192  if (!dynamic_cast<const ConvertibleTo<DecayT>*>(other)) {
193  GKO_NOT_SUPPORTED(other);
194  }
195  if constexpr (!std::is_const_v<T> &&
196  !std::is_base_of_v<ConvertibleTo<DecayOther>, DecayT>) {
197  GKO_NOT_SUPPORTED(other);
198  }
199  }
200 
213  template <typename OrigT>
214  static temporary_conversion create(OrigT* orig)
215  {
216  static_assert_compatibility<OrigT>();
218  if constexpr (std::is_same_v<T, OrigT>) {
219  return handle_type{orig, null_deleter<T>{}};
220  }
221  if (auto p = dynamic_cast<T*>(orig)) {
222  return {handle_type{p, null_deleter<T>{}}};
223  }
224  using DecayT = std::decay_t<T>;
225  using DecayOrig = std::decay_t<OrigT>;
226  if constexpr (std::is_const_v<T> ||
227  std::is_base_of_v<ConvertibleTo<DecayOrig>, DecayT>) {
228  auto converted =
229  detail::conversion_target_helper<DecayT>::create_empty(orig);
230  as<ConvertibleTo<DecayT>>(orig)->convert_to(converted);
231  return {handle_type(converted.release(),
232  detail::convert_back_deleter<T, OrigT>{orig})};
233  }
234  GKO_NOT_SUPPORTED(orig); // unreachable
235  }
236 
256  template <typename OrigT>
257  static temporary_conversion create(std::unique_ptr<OrigT> orig)
258  {
259  static_assert_compatibility<OrigT>();
260  dynamic_assert_compatibility(orig.get());
261  std::function<void(OrigT*)> deleter = orig.get_deleter();
262  auto orig_ptr = orig.release();
263  if constexpr (std::is_same_v<T, OrigT>) {
264  return handle_type{orig_ptr, deleter};
265  }
266  if (auto p = dynamic_cast<T*>(orig_ptr)) {
267  return {
268  handle_type{p, [orig_ptr, deleter](T*) { deleter(orig_ptr); }}};
269  }
270  using DecayT = std::decay_t<T>;
271  using DecayOrig = std::decay_t<OrigT>;
272  if constexpr (std::is_const_v<T> ||
273  std::is_base_of_v<ConvertibleTo<DecayOrig>, DecayT>) {
274  auto converted =
275  detail::conversion_target_helper<DecayT>::create_empty(
276  orig_ptr);
277  as<ConvertibleTo<DecayT>>(orig_ptr)->convert_to(converted);
278  return {handle_type(converted.release(),
279  [orig_ptr, orig_deleter = deleter](T* ptr) {
280  auto deleter =
281  detail::convert_back_deleter<T, OrigT>{
282  orig_ptr};
283  deleter(ptr);
284  orig_deleter(orig_ptr);
285  })};
286  }
287  GKO_NOT_SUPPORTED(orig_ptr); // unreachable
288  }
289 
300  template <typename Derived, typename = std::enable_if_t<std::is_base_of_v<
301  std::decay_t<T>, std::decay_t<Derived>>>>
304  {
305  auto handle = std::move(derived).empty_out();
306  return {handle_type{handle.release(),
307  [deleter = handle.get_deleter()](T* ptr) {
308  deleter(dynamic_cast<Derived*>(ptr));
309  }}};
310  }
311 
324  template <typename Base, typename = std::enable_if_t<std::is_base_of_v<
325  std::decay_t<Base>, std::decay_t<T>>>>
328  {
329  if (!dynamic_cast<T*>(base.get())) {
330  GKO_NOT_SUPPORTED(base);
331  }
332  auto handle = std::move(base).empty_out();
333  return {handle_type{dynamic_cast<T*>(handle.release()),
334  [deleter = handle.get_deleter()](T* ptr) {
335  deleter(static_cast<Base*>(ptr));
336  }}};
337  }
338 
344  T* get() const { return handle_.get(); }
345 
351  T* operator->() const { return handle_.get(); }
352 
353 private:
354  temporary_conversion(handle_type handle) : handle_{std::move(handle)} {}
355 
356  handle_type empty_out() && { return std::move(handle_); }
357 
358  handle_type handle_;
359 };
360 
361 
375 template <typename T, typename U>
377 {
378  if (!dynamic_cast<T*>(obj.get())) {
379  GKO_NOT_SUPPORTED(obj.get());
380  }
381  return temporary_conversion<T>::create_from_base(std::move(obj));
382 }
383 
387 template <typename T, typename U>
389 {
390  if (!dynamic_cast<const T*>(obj.get())) {
391  GKO_NOT_SUPPORTED(obj.get());
392  }
394 }
395 
396 
397 namespace detail {
398 
399 
400 // For backwards compatibility
402 
403 
404 } // namespace detail
405 } // namespace gko
406 
407 
408 #endif // GKO_PUBLIC_CORE_BASE_TEMPORARY_CONVERSION_HPP_
gko::temporary_conversion::create
static temporary_conversion create(OrigT *orig)
Create a temporary conversion from a bare pointer.
Definition: temporary_conversion.hpp:214
gko::temporary_conversion::create_from_base
static temporary_conversion create_from_base(temporary_conversion< Base > &&base)
Create a temporary conversion for a derived type T from an object of a base type.
Definition: temporary_conversion.hpp:326
gko::temporary_conversion::static_assert_compatibility
static void static_assert_compatibility()
Static assertion to check if a conversion from Other to T is possible.
Definition: temporary_conversion.hpp:151
gko::temporary_conversion::get
T * get() const
Returns the object held by temporary_conversion.
Definition: temporary_conversion.hpp:344
gko::null_deleter
This is a deleter that does not delete the object.
Definition: utils_helper.hpp:497
gko::temporary_conversion::operator->
T * operator->() const
Calls a method on the underlying object.
Definition: temporary_conversion.hpp:351
gko::as
temporary_conversion< T > as(temporary_conversion< U > &&obj)
Performs polymorphic type conversion of a temporary_conversion.
Definition: temporary_conversion.hpp:376
gko
The Ginkgo namespace.
Definition: abstract_factory.hpp:19
gko::temporary_conversion::dynamic_assert_compatibility
static void dynamic_assert_compatibility(Other *other)
Dynamic assertion to check if a conversion from Other to T is possible.
Definition: temporary_conversion.hpp:179
gko::ConvertibleTo
ConvertibleTo interface is used to mark that the implementer can be converted to the object of Result...
Definition: polymorphic_object.hpp:147
gko::temporary_conversion
A temporary_conversion is a special smart pointer-like object that is designed to hold an object temp...
Definition: temporary_conversion.hpp:129
gko::temporary_conversion::create_from_derived
static temporary_conversion create_from_derived(temporary_conversion< Derived > &&derived)
Create a temporary conversion for a base type T from an object of a derived type.
Definition: temporary_conversion.hpp:302
gko::temporary_conversion::create
static temporary_conversion create(std::unique_ptr< OrigT > orig)
Create a temporary conversion that also owns orig.
Definition: temporary_conversion.hpp:257