DPC++ Runtime
Runtime libraries for oneAPI DPC++
impl_utils.hpp
Go to the documentation of this file.
1 //===- impl_utils.hpp -----------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #pragma once
10 
11 #include <cassert> // for assert
12 #include <type_traits> // for add_pointer_t
13 
14 namespace sycl {
15 inline namespace _V1 {
16 namespace detail {
17 
18 // Helper function for extracting implementation from SYCL's interface objects.
19 // Note! This function relies on the fact that all SYCL interface classes
20 // contain "impl" field that points to implementation object. "impl" field
21 // should be accessible from this function.
22 //
23 // Note that due to a bug in MSVC compilers (including MSVC2019 v19.20), it
24 // may not recognize the usage of this function in friend member declarations
25 // if the template parameter name there is not equal to the name used here,
26 // i.e. 'Obj'. For example, using 'Obj' here and 'T' in such declaration
27 // would trigger that error in MSVC:
28 // template <class T>
29 // friend decltype(T::impl) detail::getSyclObjImpl(const T &SyclObject);
30 template <class Obj> decltype(Obj::impl) getSyclObjImpl(const Obj &SyclObject) {
31  assert(SyclObject.impl && "every constructor should create an impl");
32  return SyclObject.impl;
33 }
34 
35 // Returns the raw pointer to the impl object of given face object. The caller
36 // must make sure the returned pointer is not captured in a field or otherwise
37 // stored - i.e. must live only as on-stack value.
38 template <class T>
39 typename std::add_pointer_t<typename decltype(T::impl)::element_type>
40 getRawSyclObjImpl(const T &SyclObject) {
41  return SyclObject.impl.get();
42 }
43 
44 // Helper function for creation SYCL interface objects from implementations.
45 // Note! This function relies on the fact that all SYCL interface classes
46 // contain "impl" field that points to implementation object. "impl" field
47 // should be accessible from this function.
48 template <class T> T createSyclObjFromImpl(decltype(T::impl) ImplObj) {
49  return T(ImplObj);
50 }
51 
52 } // namespace detail
53 } // namespace _V1
54 } // namespace sycl
std::add_pointer_t< typename decltype(T::impl)::element_type > getRawSyclObjImpl(const T &SyclObject)
Definition: impl_utils.hpp:40
decltype(Obj::impl) getSyclObjImpl(const Obj &SyclObject)
Definition: impl_utils.hpp:30
T createSyclObjFromImpl(decltype(T::impl) ImplObj)
Definition: impl_utils.hpp:48
Definition: access.hpp:18