QmlMaterial 0.1.0
Loading...
Searching...
No Matches
core.hpp
1#pragma once
2
3#include <cstdint>
4#include <cstddef>
5#include <cstring>
6#include <memory>
7#include <span>
8#include <utility>
9#include <cmath>
10
11using i8 = std::int8_t;
12using i16 = std::int16_t;
13using i32 = std::int32_t;
14using i64 = std::int64_t;
15
16using u8 = std::uint8_t;
17using u16 = std::uint16_t;
18using u32 = std::uint32_t;
19using u64 = std::uint64_t;
20
21using idx = std::ptrdiff_t;
22using usize = std::size_t;
23using isize = std::ptrdiff_t;
24using byte = std::byte;
25using voidp = void*;
26using const_voidp = const void*;
27
28template<typename T, typename D = std::default_delete<T>>
29using up = std::unique_ptr<T, D>;
30
31namespace ycore
32{
33
34template<typename T>
35[[nodiscard]] constexpr bool fuzzy_equal(T a, T b) {
36 const T scale = std::same_as<T, double> ? T(1000000000000.) : T(100000.f);
37 return std::fabs(a - b) * scale <= std::min(std::fabs(a), std::fabs(b));
38}
39
40template<typename T>
41using param_t = std::conditional_t<std::is_trivially_copyable_v<T> && sizeof(T) <= 32, T, const T&>;
42
43template<typename T>
44auto cmp_set(T& lhs, param_t<T> rhs) noexcept -> bool {
45 if constexpr (std::is_floating_point_v<T>) {
46 if (! fuzzy_equal(lhs, rhs)) {
47 lhs = rhs;
48 return true;
49 }
50 } else {
51 if (lhs != rhs) {
52 lhs = rhs;
53 return true;
54 }
55 }
56 return false;
57}
58
59template<class F>
60struct y_combinator {
61 F f; // the lambda will be stored here
62
63 // a forwarding operator():
64 template<class... Args>
65 decltype(auto) operator()(Args&&... args) const {
66 // we pass ourselves to f, then the arguments.
67 return f(*this, std::forward<Args>(args)...);
68 }
69};
70
71template<class F>
72y_combinator(F) -> y_combinator<F>;
73
74} // namespace ycore