summaryrefslogtreecommitdiffhomepage
path: root/compat/util.hpp
blob: 528ae1a8fd51bfa7281ce1504e8887cd239d1cac (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#pragma once

#include "make-unique.hpp"

#include <memory>
#include <cmath>
#include <utility>
#include <type_traits>
#include <thread>
#include <condition_variable>

#include <QObject>
#include <QDebug>

#define progn(...) ([&]() { __VA_ARGS__ }())
template<typename t> using mem = std::shared_ptr<t>;
template<typename t> using ptr = std::unique_ptr<t>;

#if defined(_MSC_VER) && !defined(Q_CREATOR_RUN)
#   define DEFUN_WARN_UNUSED _Check_return_
#else
#   define DEFUN_WARN_UNUSED __attribute__((warn_unused_result))
#endif

template<typename t>
int iround(const t& val)
{
    return int(std::round(val));
}

namespace detail {

template<typename n>
inline auto clamp_(n val, n min, n max) -> n
{
    if (val > max)
        return max;
    if (val < min)
        return min;
    return val;
}

}

template<typename t, typename u, typename w>
inline auto clamp(const t& val, const u& min, const w& max) -> decltype(val * min * max)
{
    return ::detail::clamp_<decltype(val * min * max)>(val, min, max);
}

namespace detail {

template<typename t>
struct run_in_thread_traits
{
    using type = t;
    using ret_type = t&&;
    static inline void assign(t& lvalue, t&& rvalue) { lvalue = rvalue; }
    static inline ret_type&& pass(ret_type&& val) { return std::move(val); }
    template<typename F> static ret_type call(F& fun) { return std::move(fun()); }
};

template<>
struct run_in_thread_traits<void>
{
    using type = unsigned char;
    using ret_type = void;
    static inline void assign(unsigned char&, unsigned char&&) {}
    static inline void pass(type&&) {}
    template<typename F> static type&& call(F& fun) { fun(); return std::move(type(0)); }
};

}

template<typename F>
auto run_in_thread_sync(QObject* obj, F&& fun)
    -> typename detail::run_in_thread_traits<decltype(std::forward<F>(fun)())>::ret_type
{
    using lock_guard = std::unique_lock<std::mutex>;

    std::mutex mtx;
    lock_guard guard(mtx);
    std::condition_variable cvar;

    std::thread::id waiting_thread = std::this_thread::get_id();

    using traits = detail::run_in_thread_traits<decltype(std::forward<F>(fun)())>;

    typename traits::type ret;

    bool skip_wait = false;

    {
        QObject src;
        src.moveToThread(obj->thread());
        QObject::connect(&src,
                         &QObject::destroyed,
                         obj,
                         [&]() {
            std::thread::id calling_thread = std::this_thread::get_id();
            if (waiting_thread == calling_thread)
            {
                skip_wait = true;
                traits::assign(ret, traits::call(fun));
            }
            else
            {
                lock_guard guard(mtx);
                traits::assign(ret, traits::call(fun));
                cvar.notify_one();
            }
        },
        Qt::AutoConnection);
    }

    if (!skip_wait)
        cvar.wait(guard);
    return traits::pass(std::move(ret));
}

template<typename F>
void run_in_thread_async(QObject* obj, F&& fun)
{
    QObject src;
    src.moveToThread(obj->thread());
    QObject::connect(&src, &QObject::destroyed, obj, std::move(fun), Qt::AutoConnection);
}