diff options
Diffstat (limited to 'compat')
| -rw-r--r-- | compat/make-unique.hpp | 9 | ||||
| -rw-r--r-- | compat/ndebug-guard.hpp | 3 | ||||
| -rw-r--r-- | compat/run-in-thread.hpp | 22 | ||||
| -rw-r--r-- | compat/util.hpp | 17 |
4 files changed, 38 insertions, 13 deletions
diff --git a/compat/make-unique.hpp b/compat/make-unique.hpp index bb5315c5..64c647b1 100644 --- a/compat/make-unique.hpp +++ b/compat/make-unique.hpp @@ -8,7 +8,7 @@ #include <utility> #include <cstddef> -namespace detail { +namespace raii_detail { template<class T> struct Unique_if { typedef std::unique_ptr<T> Single_object; @@ -26,18 +26,19 @@ template<class T, size_t N> struct Unique_if<T[N]> } template<class T, class... Args> - typename detail::Unique_if<T>::Single_object + typename ::raii_detail::Unique_if<T>::Single_object make_unique(Args&&... args) { return std::unique_ptr<T>(new T(std::forward<Args>(args)...)); } template<class T> - typename detail::Unique_if<T>::Unknown_bound + typename ::raii_detail::Unique_if<T>::Unknown_bound make_unique(std::size_t n) { typedef typename std::remove_extent<T>::type U; return std::unique_ptr<T>(new U[n]()); } template<class T, class... Args> - typename detail::Unique_if<T>::Known_bound + typename ::raii_detail::Unique_if<T>::Known_bound make_unique(Args&&...) = delete; + diff --git a/compat/ndebug-guard.hpp b/compat/ndebug-guard.hpp new file mode 100644 index 00000000..209177b3 --- /dev/null +++ b/compat/ndebug-guard.hpp @@ -0,0 +1,3 @@ +#ifdef NDEBUG +# error "NDEBUG defined, don't define it" +#endif diff --git a/compat/run-in-thread.hpp b/compat/run-in-thread.hpp index d377f625..90aa143b 100644 --- a/compat/run-in-thread.hpp +++ b/compat/run-in-thread.hpp @@ -1,12 +1,16 @@ #pragma once -#include <QObject> +#include "ndebug-guard.hpp" +#include <cassert> #include <thread> #include <condition_variable> #include <utility> -namespace detail { +#include <QObject> +#include <QThread> + +namespace qt_impl_detail { template<typename t> struct run_in_thread_traits @@ -25,14 +29,14 @@ struct run_in_thread_traits<void> 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> static type call(F& fun) { fun(); return 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 + -> typename qt_impl_detail::run_in_thread_traits<decltype(std::forward<F>(fun)())>::ret_type { using lock_guard = std::unique_lock<std::mutex>; @@ -42,7 +46,7 @@ auto run_in_thread_sync(QObject* obj, F&& fun) std::thread::id waiting_thread = std::this_thread::get_id(); - using traits = detail::run_in_thread_traits<decltype(std::forward<F>(fun)())>; + using traits = qt_impl_detail::run_in_thread_traits<decltype(std::forward<F>(fun)())>; typename traits::type ret; @@ -50,7 +54,9 @@ auto run_in_thread_sync(QObject* obj, F&& fun) { QObject src; - src.moveToThread(obj->thread()); + QThread* t(obj->thread()); + assert(t); + src.moveToThread(t); QObject::connect(&src, &QObject::destroyed, obj, @@ -80,6 +86,8 @@ template<typename F> void run_in_thread_async(QObject* obj, F&& fun) { QObject src; - src.moveToThread(obj->thread()); + QThread* t(obj->thread()); + assert(t); + src.moveToThread(t); QObject::connect(&src, &QObject::destroyed, obj, std::move(fun), Qt::AutoConnection); } diff --git a/compat/util.hpp b/compat/util.hpp index 7a6858a3..5b9e2c69 100644 --- a/compat/util.hpp +++ b/compat/util.hpp @@ -1,10 +1,15 @@ #pragma once +#include "ndebug-guard.hpp" + #include "make-unique.hpp" #include "run-in-thread.hpp" #include <memory> #include <cmath> +#include <utility> + +#include <QSharedPointer> #define progn(...) ([&]() { __VA_ARGS__ }()) template<typename t> using mem = std::shared_ptr<t>; @@ -22,7 +27,7 @@ int iround(const t& val) return int(std::round(val)); } -namespace detail { +namespace util_detail { template<typename n> inline auto clamp_(n val, n min, n max) -> n @@ -39,5 +44,13 @@ inline auto clamp_(n val, n min, n max) -> n 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); + return ::util_detail::clamp_<decltype(val * min * max)>(val, min, max); +} + +template<typename t, typename... xs> +auto qptr(xs... args) +{ + return QSharedPointer<t>(new t(std::forward<xs>(args)...)); } + +template<typename t> using qshared = QSharedPointer<t>; |
