Elffuss BraidHub

kikocis/kleidiai public

main · 4.2 KiBRawPermalink
1//
2// SPDX-FileCopyrightText: Copyright 2025-2026 Arm Limited and/or its affiliates <open-source-office@arm.com>
3//
4// SPDX-License-Identifier: Apache-2.0
5//
6
7#pragma once
8
9#include <algorithm>
10#include <cstddef>
11#include <memory>
12#include <type_traits>
13#include <utility>
14
15namespace kai::test {
16
17namespace internal {
18
19class PolyControl {
20public:
21 virtual ~PolyControl() = default;
22
23 template <typename Derived>
24 explicit PolyControl(std::in_place_type_t<Derived> type);
25
26 template <typename Base>
27 [[nodiscard]] const Base& data() const;
28
29 template <typename Base>
30 [[nodiscard]] Base& data();
31
32 [[nodiscard]] std::unique_ptr<PolyControl> clone() const;
33
34private:
35 std::unique_ptr<PolyControl> (*m_clone_fn)(const PolyControl& inner);
36 const void* (*m_get_constant_fn)(const PolyControl& inner) = nullptr;
37 void* (*m_get_mutable_fn)(PolyControl& inner) = nullptr;
38};
39
40template <typename Derived>
41struct PolyInner : public PolyControl {
42 template <typename... Args>
43 explicit PolyInner(Args&&... args);
44
45 Derived data;
46};
47
48template <typename Derived>
49inline PolyControl::PolyControl([[maybe_unused]] std::in_place_type_t<Derived> type) :
50 m_clone_fn([](const PolyControl& inner) -> std::unique_ptr<PolyControl> {
51 auto& self = static_cast<const PolyInner<Derived>&>(inner);
52 return std::make_unique<PolyInner<Derived>>(self.data);
53 }),
54 m_get_constant_fn(+[](const PolyControl& inner) -> const void* {
55 auto& self = static_cast<const PolyInner<Derived>&>(inner);
56 return &self.data;
57 }),
58 m_get_mutable_fn(+[](PolyControl& inner) -> void* {
59 auto& self = static_cast<PolyInner<Derived>&>(inner);
60 return &self.data;
61 }) {
62}
63
64template <typename Base>
65inline const Base& PolyControl::data() const {
66 return *static_cast<const Base*>(m_get_constant_fn(*this));
67}
68
69template <typename Base>
70[[nodiscard]] inline Base& PolyControl::data() {
71 return *static_cast<Base*>(m_get_mutable_fn(*this));
72}
73
74[[nodiscard]] inline std::unique_ptr<PolyControl> PolyControl::clone() const {
75 return m_clone_fn(*this);
76}
77
78template <typename Derived>
79template <typename... Args>
80inline PolyInner<Derived>::PolyInner(Args&&... args) :
81 PolyControl(std::in_place_type<Derived>), data(std::forward<Args>(args)...) {
82}
83
84} // namespace internal
85
86/// Copyable unique pointer.
87///
88/// This class is similar to @ref std::polymorphic from C++23.
89template <typename Base>
90class Poly {
91 template <typename Derived>
92 friend class Poly;
93
94public:
95 Poly() = default;
96
97 template <typename Derived, typename... Args>
98 explicit Poly([[maybe_unused]] std::in_place_type_t<Derived> type, Args&&... args) :
99 m_wrapper(std::make_unique<internal::PolyInner<Derived>>(std::forward<Args>(args)...)) {
100 }
101
102 Poly(const Poly& other) : m_wrapper(other.m_wrapper->clone()) {
103 }
104
105 Poly& operator=(const Poly& other) {
106 if (this != &other) {
107 m_wrapper = other.m_wrapper->clone();
108 }
109
110 return *this;
111 }
112
113 template <
114 typename Derived,
115 std::enable_if_t<std::is_base_of_v<Base, Derived> && std::is_convertible_v<const Derived*, const Base*>, bool> =
116 true>
117 Poly(Poly<Derived>&& other) : m_wrapper(std::move(other.m_wrapper)) {
118 }
119
120 template <
121 typename Derived,
122 std::enable_if_t<std::is_base_of_v<Base, Derived> && std::is_convertible_v<const Derived*, const Base*>, bool> =
123 true>
124 Poly& operator=(Poly<Derived>&& other) {
125 m_wrapper = std::move(other.m_wrapper);
126 return *this;
127 }
128
129 [[nodiscard]] const Base* operator->() const {
130 return &m_wrapper->template data<Base>();
131 }
132
133 [[nodiscard]] Base* operator->() {
134 return &m_wrapper->template data<Base>();
135 }
136
137 [[nodiscard]] const Base& operator*() const {
138 return m_wrapper->template data<Base>();
139 }
140
141 [[nodiscard]] Base& operator*() {
142 return m_wrapper->template data<Base>();
143 }
144
145private:
146 std::unique_ptr<internal::PolyControl> m_wrapper;
147};
148
149template <typename T, typename... Args>
150[[nodiscard]] Poly<T> make_poly(Args&&... args) {
151 return Poly<T>(std::in_place_type<T>, std::forward<Args>(args)...);
152}
153
154} // namespace kai::test