summaryrefslogtreecommitdiff
path: root/asl/utility.hpp
blob: 7740eff37a210afcd72b7178719aa28ef750e250 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#pragma once

#include "asl/meta.hpp"
#include "asl/layout.hpp"

#define ASL_MOVE(expr_) (static_cast<::asl::un_ref_t<decltype(expr_)>&&>(expr_))

#define ASL_FWD(expr_) (static_cast<decltype(expr_)&&>(expr_))

namespace asl
{

template<typename T, typename U>
T exchange(T& obj, U&& new_value)
{
    T old_value = ASL_MOVE(obj);
    obj = ASL_FWD(new_value);
    return old_value;
}

template<trivially_copyable U, trivially_copyable T>
constexpr U bit_cast(T value) requires (size_of<T> == size_of<U>)
{
    return __builtin_bit_cast(U, value); 
}

template<typename T>
T min(T a, T b)
{
    return (a <= b) ? a : b;
}

#define ASL_DELETE_COPY(T)                         \
    T(const T&) = delete;                          \
    T& operator=(const T&) = delete;

#define ASL_DELETE_MOVE(T)                         \
    T(T&&) = delete;                               \
    T& operator=(T&&) = delete;

#define ASL_DELETE_COPY_MOVE(T)                    \
    ASL_DELETE_COPY(T)                             \
    ASL_DELETE_MOVE(T)

} // namespace asl