@auto_js/utility 0.0.7 → 0.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/functional/functional.cc +15 -0
- package/package.json +1 -1
- package/utility/covariant_value.cc +5 -2
- package/utility/utility.cc +33 -0
package/functional/functional.cc
CHANGED
|
@@ -133,4 +133,19 @@ class reference_wrapper {
|
|
|
133
133
|
Type* value_;
|
|
134
134
|
};
|
|
135
135
|
|
|
136
|
+
// Wrapper allowing the return of temporaries from `operator->()`
|
|
137
|
+
export template <class Type>
|
|
138
|
+
class pointer_delegate {
|
|
139
|
+
public:
|
|
140
|
+
constexpr explicit pointer_delegate(const Type& value) : value{value} {}
|
|
141
|
+
constexpr explicit pointer_delegate(Type&& value) : value{std::move(value)} {}
|
|
142
|
+
constexpr auto operator*() -> Type& { return value; }
|
|
143
|
+
constexpr auto operator*() const -> const Type& { return value; }
|
|
144
|
+
constexpr auto operator->() -> Type* { return &value; }
|
|
145
|
+
constexpr auto operator->() const -> const Type* { return &value; }
|
|
146
|
+
|
|
147
|
+
private:
|
|
148
|
+
Type value;
|
|
149
|
+
};
|
|
150
|
+
|
|
136
151
|
} // namespace util
|
package/package.json
CHANGED
|
@@ -10,10 +10,13 @@ export template <class Type, class... Types>
|
|
|
10
10
|
// NOLINTNEXTLINE(cppcoreguidelines-special-member-functions)
|
|
11
11
|
class covariant_value : public pointer_facade {
|
|
12
12
|
public:
|
|
13
|
+
constexpr covariant_value() : covariant_value{std::type_identity<Types...[ 0 ]>{}} {}
|
|
14
|
+
|
|
13
15
|
template <class Derived>
|
|
14
16
|
requires(... || (type<Derived> == type<Types>))
|
|
15
|
-
|
|
16
|
-
|
|
17
|
+
// NOLINTNEXTLINE(google-explicit-constructor)
|
|
18
|
+
constexpr covariant_value(std::type_identity<Derived>, auto&&... args) :
|
|
19
|
+
value_{std::in_place_type<Derived>, std::forward<decltype(args)>(args)...} {}
|
|
17
20
|
|
|
18
21
|
constexpr auto operator*(this auto& self) noexcept -> auto& {
|
|
19
22
|
using reference_type = util::apply_cvref_t<decltype(self), Type>;
|
package/utility/utility.cc
CHANGED
|
@@ -157,4 +157,37 @@ auto make_inplace_container(auto&&... args) -> Type {
|
|
|
157
157
|
return inplace_container_constructor<Type>{}(std::forward<decltype(args)>(args)...);
|
|
158
158
|
}
|
|
159
159
|
|
|
160
|
+
// `std::bit_cast` into a type that is larger than needed. Useful for packing arbitrary small
|
|
161
|
+
// trivials into C-style function callbacks which accept a `void*`
|
|
162
|
+
export template <class Type>
|
|
163
|
+
constexpr auto bit_padding_cast(const auto& value) -> Type {
|
|
164
|
+
using value_type = std::remove_cvref_t<decltype(value)>;
|
|
165
|
+
static_assert(sizeof(value_type) <= sizeof(Type), "'Type' must be larger than 'value'");
|
|
166
|
+
if constexpr (sizeof(value_type) == sizeof(Type)) {
|
|
167
|
+
return std::bit_cast<Type>(value);
|
|
168
|
+
} else {
|
|
169
|
+
struct [[gnu::packed]] holder {
|
|
170
|
+
value_type value;
|
|
171
|
+
std::array<std::byte, sizeof(Type) - sizeof(value_type)> padding;
|
|
172
|
+
};
|
|
173
|
+
return std::bit_cast<Type>(holder{.value = value, .padding = {}});
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// `std::bit_cast` from the result of `util::bit_padding_cast`
|
|
178
|
+
export template <class Type>
|
|
179
|
+
constexpr auto bit_truncation_cast(const auto& value) -> Type {
|
|
180
|
+
using value_type = std::remove_cvref_t<decltype(value)>;
|
|
181
|
+
static_assert(sizeof(value_type) >= sizeof(Type), "'Type' must be smaller than 'value'");
|
|
182
|
+
if constexpr (sizeof(value_type) == sizeof(Type)) {
|
|
183
|
+
return std::bit_cast<Type>(value);
|
|
184
|
+
} else {
|
|
185
|
+
struct [[gnu::packed]] holder {
|
|
186
|
+
Type value;
|
|
187
|
+
std::array<std::byte, sizeof(value_type) - sizeof(Type)> padding;
|
|
188
|
+
};
|
|
189
|
+
return std::bit_cast<holder>(value).value;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
160
193
|
} // namespace util
|