|
| 1 | +//==-------------- test_proxy.hpp - DPC++ Explicit SIMD API ----------------==// |
| 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 | +// Test proxy to differentiate move and copy constructor calls |
| 9 | +//===----------------------------------------------------------------------===// |
| 10 | + |
| 11 | +#pragma once |
| 12 | + |
| 13 | +#ifndef __ESIMD_ENABLE_TEST_PROXY |
| 14 | + |
| 15 | +// No ABI-breaking changes by default |
| 16 | +#define __ESIMD_DECLARE_TEST_PROXY |
| 17 | +#define __ESIMD_DECLARE_TEST_PROXY_ACCESS |
| 18 | +#define __esimd_move_test_proxy(other) |
| 19 | + |
| 20 | +#else |
| 21 | + |
| 22 | +// Declare the class attribute |
| 23 | +// |
| 24 | +// We are using non static data member initialization approach to force |
| 25 | +// the value required. Initialization will take place even if no |
| 26 | +// default/copy/move constructor of the test_proxy class was explcitly |
| 27 | +// called by any of the user-defined constructors of the proxy target |
| 28 | +#define __ESIMD_DECLARE_TEST_PROXY \ |
| 29 | + esimd::detail::test_proxy M_testProxy = esimd::detail::test_proxy(); |
| 30 | + |
| 31 | +// Declare the getter to access the proxy from the tests |
| 32 | +#define __ESIMD_DECLARE_TEST_PROXY_ACCESS \ |
| 33 | + const auto &get_test_proxy() const { return M_testProxy; } |
| 34 | + |
| 35 | +// Test proxy will be handled in a proper way by default/implicit move |
| 36 | +// constructors and move operators. |
| 37 | +// Still the user-defined constructors or move operators should explicitly state |
| 38 | +// what to do with each of class atributes, so a proper wrapper required |
| 39 | +#define __esimd_move_test_proxy(other) \ |
| 40 | + do { \ |
| 41 | + M_testProxy = std::move(other.M_testProxy); \ |
| 42 | + } while (false); |
| 43 | + |
| 44 | +__SYCL_INLINE_NAMESPACE(cl) { |
| 45 | +namespace sycl { |
| 46 | +namespace ext { |
| 47 | +namespace intel { |
| 48 | +namespace experimental { |
| 49 | +namespace esimd { |
| 50 | +namespace detail { |
| 51 | + |
| 52 | +/// The test_proxy class. |
| 53 | +/// |
| 54 | +/// This is a helper class for tests to differentiate between the copy |
| 55 | +/// constructor/assignment and the move constructor/assignment calls, |
| 56 | +/// as the copy constructor works as the default fallback for every case with |
| 57 | +/// move constructor disabled or not provided |
| 58 | +/// |
| 59 | +/// The test proxy is enabled only if the __ESIMD_ENABLE_TEST_PROXY macro is |
| 60 | +/// defined. |
| 61 | +/// It is expected for the class with the test proxy (the class under test) to: |
| 62 | +/// - provide the get_test_proxy() method |
| 63 | +/// - properly handle moving the test_proxy member in user-defined move |
| 64 | +/// constructors and user-defined assignment operators |
| 65 | +/// |
| 66 | +/// Therefore the following expression is expected to return `true` only if the |
| 67 | +/// move constructor or move operator was called for the instance of the class |
| 68 | +/// under test: |
| 69 | +/// instance.get_test_proxy().was_moved() |
| 70 | +/// |
| 71 | +/// \ingroup sycl_esimd |
| 72 | +class test_proxy { |
| 73 | + // Define the default value to use for every constructor |
| 74 | + bool m_moved = false; |
| 75 | + |
| 76 | +public: |
| 77 | + test_proxy() { __esimd_dbg_print(test_proxy()); } |
| 78 | + |
| 79 | + test_proxy(const test_proxy &) { |
| 80 | + __esimd_dbg_print(test_proxy(const test_proxy &other)); |
| 81 | + } |
| 82 | + test_proxy(test_proxy &&) { |
| 83 | + __esimd_dbg_print(test_proxy(test_proxy && other)); |
| 84 | + m_moved = true; |
| 85 | + } |
| 86 | + test_proxy &operator=(const test_proxy &) { |
| 87 | + __esimd_dbg_print(test_proxy::operator=(const test_proxy &other)); |
| 88 | + return *this; |
| 89 | + } |
| 90 | + test_proxy &operator=(test_proxy &&) { |
| 91 | + __esimd_dbg_print(test_proxy::operator=(test_proxy &&other)); |
| 92 | + m_moved = true; |
| 93 | + return *this; |
| 94 | + } |
| 95 | + bool was_moved() const { return m_moved; } |
| 96 | +}; |
| 97 | + |
| 98 | +} // namespace detail |
| 99 | +} // namespace esimd |
| 100 | +} // namespace experimental |
| 101 | +} // namespace intel |
| 102 | +} // namespace ext |
| 103 | +} // namespace sycl |
| 104 | +} // __SYCL_INLINE_NAMESPACE(cl) |
| 105 | + |
| 106 | +#endif // __ESIMD_ENABLE_TEST_PROXY |
0 commit comments