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