Ginkgo Generated from branch based on master. Ginkgo version 1.7.0
A numerical linear algebra library targeting many-core architectures
Loading...
Searching...
No Matches
temporary_conversion.hpp
1/*******************************<GINKGO LICENSE>******************************
2Copyright (c) 2017-2023, the Ginkgo authors
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions
7are met:
8
91. Redistributions of source code must retain the above copyright
10notice, this list of conditions and the following disclaimer.
11
122. Redistributions in binary form must reproduce the above copyright
13notice, this list of conditions and the following disclaimer in the
14documentation and/or other materials provided with the distribution.
15
163. Neither the name of the copyright holder nor the names of its
17contributors may be used to endorse or promote products derived from
18this software without specific prior written permission.
19
20THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31******************************<GINKGO LICENSE>*******************************/
32
33#ifndef GKO_PUBLIC_CORE_BASE_TEMPORARY_CONVERSION_HPP_
34#define GKO_PUBLIC_CORE_BASE_TEMPORARY_CONVERSION_HPP_
35
36
37#include <memory>
38#include <tuple>
39#include <type_traits>
40
41
42#include <ginkgo/core/base/lin_op.hpp>
43#include <ginkgo/core/base/utils.hpp>
44
45
46namespace gko {
47namespace detail {
48
49
68template <typename CopyType, typename OrigType>
69class convert_back_deleter {
70public:
71 using pointer = CopyType*;
72 using original_pointer = OrigType*;
73
80 convert_back_deleter(original_pointer original) : original_{original} {}
81
87 void operator()(pointer ptr) const
88 {
89 ptr->convert_to(original_);
90 delete ptr;
91 }
92
93private:
94 original_pointer original_;
95};
96
97// specialization for constant objects, no need to convert back something that
98// cannot change
99template <typename CopyType, typename OrigType>
100class convert_back_deleter<const CopyType, const OrigType> {
101public:
102 using pointer = const CopyType*;
103 using original_pointer = const OrigType*;
104 convert_back_deleter(original_pointer) {}
105
106 void operator()(pointer ptr) const { delete ptr; }
107};
108
109
119template <typename TargetType>
120struct conversion_target_helper {
128 template <typename SourceType,
129 typename = std::enable_if_t<std::is_base_of<
131 static std::unique_ptr<TargetType> create_empty(const SourceType* source)
132 {
133 return TargetType::create(source->get_executor());
134 }
135};
136
137
148template <typename... ConversionCandidates>
149struct conversion_helper {
151 template <typename TargetType, typename MaybeConstLinOp>
152 static std::unique_ptr<TargetType, std::function<void(TargetType*)>>
153 convert(MaybeConstLinOp* obj)
154 {
155 return convert_impl<TargetType, MaybeConstLinOp,
156 ConversionCandidates...>(obj);
157 }
158
164 template <typename TargetType, typename MaybeConstLinOp,
165 typename FirstCandidate, typename... TrailingCandidates>
166 static std::unique_ptr<TargetType, std::function<void(TargetType*)>>
167 convert_impl(MaybeConstLinOp* obj)
168 {
169 // make candidate_type conditionally const based on whether obj is const
170 using candidate_type =
171 std::conditional_t<std::is_const<MaybeConstLinOp>::value,
174 if ((cast_obj = dynamic_cast<candidate_type*>(obj))) {
175 // if the cast is successful, obj is of dynamic type candidate_type
176 // so we can convert from this type to TargetType
177 auto converted = conversion_target_helper<
178 std::remove_cv_t<TargetType>>::create_empty(cast_obj);
179 cast_obj->convert_to(converted);
180 // Make sure ConvertibleTo<TargetType> is available and symmetric
181 static_assert(
182 std::is_base_of<ConvertibleTo<std::remove_cv_t<TargetType>>,
183 FirstCandidate>::value,
184 "ConvertibleTo not implemented");
185 static_assert(std::is_base_of<ConvertibleTo<FirstCandidate>,
186 TargetType>::value,
187 "ConvertibleTo not symmetric");
188 return {converted.release(),
190 } else {
191 // else try the remaining candidates
192 return conversion_helper<TrailingCandidates...>::template convert<
193 TargetType>(obj);
194 }
195 }
196};
197
198template <>
199struct conversion_helper<> {
200 template <typename T, typename MaybeConstLinOp>
201 static std::unique_ptr<T, std::function<void(T*)>> convert(
202 MaybeConstLinOp* obj)
203 {
204 // return nullptr if no previous candidates matched
205 return {nullptr, null_deleter<T>{}};
206 }
207};
208
209
222template <typename T>
223class temporary_conversion {
224public:
225 using value_type = T;
226 using pointer = T*;
227 using lin_op_type =
228 std::conditional_t<std::is_const<T>::value, const LinOp, LinOp>;
229
236 template <typename... ConversionCandidates>
237 static temporary_conversion create(ptr_param<lin_op_type> ptr)
238 {
239 T* cast_ptr{};
240 if ((cast_ptr = dynamic_cast<T*>(ptr.get()))) {
241 return handle_type{cast_ptr, null_deleter<T>{}};
242 } else {
243 return conversion_helper<ConversionCandidates...>::template convert<
244 T>(ptr.get());
245 }
246 }
247
253 T* get() const { return handle_.get(); }
254
260 T* operator->() const { return handle_.get(); }
261
265 explicit operator bool() { return static_cast<bool>(handle_); }
266
267private:
268 // std::function deleter allows to decide the (type of) deleter at
269 // runtime
270 using handle_type = std::unique_ptr<T, std::function<void(T*)>>;
271
272 temporary_conversion(handle_type handle) : handle_{std::move(handle)} {}
273
274 handle_type handle_;
275};
276
277
278} // namespace detail
279} // namespace gko
280
281
282#endif // GKO_PUBLIC_CORE_BASE_TEMPORARY_CONVERSION_HPP_
The Ginkgo namespace.
Definition abstract_factory.hpp:48
constexpr T one()
Returns the multiplicative identity for T.
Definition math.hpp:803