MU Library
access.hpp
Go to the documentation of this file.
1 // SPDX-License-Identifier: Apache-2.0
2 // Copyright 2026 XCENA Inc.
3 
4 #pragma once
5 
6 #include <cstddef>
7 #include <type_traits>
8 
9 namespace mu
10 {
11 
12 namespace detail
13 {
14 
16 {
17  static constexpr bool reads_existing_data = true;
18  static constexpr bool writes_result = false;
19 };
20 
22 {
23  static constexpr bool reads_existing_data = false;
24  static constexpr bool writes_result = true;
25 };
26 
27 } // namespace detail
28 
41 template <typename T>
42 class input_ptr
43 {
44  static_assert(!std::is_void_v<T>, "input_ptr<void> is not supported");
45  static_assert(!std::is_const_v<T>, "input_ptr adds const access itself");
46  static_assert(!std::is_function_v<T>, "input_ptr cannot point to a function");
47 
48 public:
49  using element_type = T;
51  using pointer = const T*;
52 
53  constexpr input_ptr() noexcept = default;
54  constexpr input_ptr(const T* ptr) noexcept
55  : ptr_(ptr)
56  {
57  }
58 
59  constexpr operator pointer() const noexcept
60  {
61  return ptr_;
62  }
63 
64  [[nodiscard]] constexpr const T& operator*() const noexcept
65  {
66  return *ptr_;
67  }
68 
69  [[nodiscard]] constexpr pointer operator->() const noexcept
70  {
71  return ptr_;
72  }
73 
74  [[nodiscard]] constexpr const T& operator[](std::ptrdiff_t index) const noexcept
75  {
76  return ptr_[index];
77  }
78 
79 private:
80  const T* ptr_ = nullptr;
81 };
82 
96 template <typename T>
98 {
99  static_assert(!std::is_void_v<T>, "output_ptr<void> is not supported");
100  static_assert(!std::is_const_v<T>, "output_ptr requires mutable elements");
101  static_assert(!std::is_function_v<T>, "output_ptr cannot point to a function");
102 
103 public:
104  using element_type = T;
106  using pointer = T*;
107 
108  constexpr output_ptr() noexcept = default;
109  constexpr output_ptr(T* ptr) noexcept
110  : ptr_(ptr)
111  {
112  }
113 
114  constexpr operator pointer() const noexcept
115  {
116  return ptr_;
117  }
118 
119  [[nodiscard]] constexpr T& operator*() const noexcept
120  {
121  return *ptr_;
122  }
123 
124  [[nodiscard]] constexpr pointer operator->() const noexcept
125  {
126  return ptr_;
127  }
128 
129  [[nodiscard]] constexpr T& operator[](std::ptrdiff_t index) const noexcept
130  {
131  return ptr_[index];
132  }
133 
134 private:
135  T* ptr_ = nullptr;
136 };
137 
138 static_assert(std::is_trivially_copyable_v<input_ptr<int>>,
139  "input_ptr must remain trivially copyable");
140 static_assert(std::is_standard_layout_v<input_ptr<int>>,
141  "input_ptr must remain standard-layout");
142 static_assert(sizeof(input_ptr<int>) == sizeof(const int*),
143  "input_ptr must remain pointer-sized");
146 static_assert(std::is_trivially_copyable_v<output_ptr<int>>,
147  "output_ptr must remain trivially copyable");
148 static_assert(std::is_standard_layout_v<output_ptr<int>>,
149  "output_ptr must remain standard-layout");
150 static_assert(sizeof(output_ptr<int>) == sizeof(int*),
151  "output_ptr must remain pointer-sized");
154 
155 } // namespace mu
Pointer to a kernel input buffer.
Definition: access.hpp:43
constexpr pointer operator->() const noexcept
Definition: access.hpp:69
constexpr const T & operator[](std::ptrdiff_t index) const noexcept
Definition: access.hpp:74
const T * pointer
Definition: access.hpp:51
constexpr input_ptr() noexcept=default
constexpr const T & operator*() const noexcept
Definition: access.hpp:64
Pointer to a kernel output buffer.
Definition: access.hpp:98
constexpr T & operator[](std::ptrdiff_t index) const noexcept
Definition: access.hpp:129
constexpr output_ptr() noexcept=default
constexpr pointer operator->() const noexcept
Definition: access.hpp:124
constexpr T & operator*() const noexcept
Definition: access.hpp:119
Definition: access.hpp:10
static constexpr bool writes_result
Definition: access.hpp:18
static constexpr bool reads_existing_data
Definition: access.hpp:17
static constexpr bool reads_existing_data
Definition: access.hpp:23
static constexpr bool writes_result
Definition: access.hpp:24