From 364c1ecd8703dadaaa9c7479335e469b153a1be0 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Thu, 18 Jan 2018 12:28:25 +0100 Subject: compat: split/cleanup util.hpp Some of the headers are clearly useless. Remove them. Move what's inside util.hpp into separate headers. Adjust usages. Will remove util.hpp. --- compat/clamp.hpp | 54 +++++++++++ compat/functional.hpp | 87 ------------------ compat/macros.hpp | 5 ++ compat/meta.hpp | 8 ++ compat/ndebug-guard.hpp | 5 -- compat/round.hpp | 15 ++++ compat/run-in-thread.hpp | 5 +- compat/time.hpp | 2 - compat/timer.cpp | 1 - compat/util.hpp | 82 +---------------- compat/value-templates.hpp | 192 ---------------------------------------- options/options.hpp | 2 - options/value-traits.hpp | 3 +- proto-ft/ftnoir_protocol_ft.cpp | 3 +- proto-libevdev/CMakeLists.txt | 1 - tracker-fusion/fusion.cpp | 1 - tracker-pt/camera.h | 1 - 17 files changed, 90 insertions(+), 377 deletions(-) create mode 100644 compat/clamp.hpp delete mode 100644 compat/functional.hpp delete mode 100644 compat/ndebug-guard.hpp create mode 100644 compat/round.hpp delete mode 100644 compat/value-templates.hpp diff --git a/compat/clamp.hpp b/compat/clamp.hpp new file mode 100644 index 00000000..8141e25f --- /dev/null +++ b/compat/clamp.hpp @@ -0,0 +1,54 @@ +#pragma once + +#include +#include + +#include "macros.hpp" + +namespace util_detail { + +template +inline auto clamp_float(n val, n min_, n max_) +{ + return std::fmin(std::fmax(val, min_), max_); +} + +template +struct clamp final +{ + static inline auto clamp_(const n& val, const n& min_, const n& max_) + { + if (unlikely(val > max_)) + return max_; + if (unlikely(val < min_)) + return min_; + return val; + } +}; + +template +struct clamp +{ + static inline auto clamp_(float val, float min_, float max_) + { + return clamp_float(val, min_, max_); + } +}; + +template +struct clamp +{ + static inline auto clamp_(double val, double min_, double max_) + { + return clamp_float(val, min_, max_); + } +}; + +} // ns util_detail + +template +inline auto clamp(const t& val, const u& min, const w& max) +{ + using tp = decltype(val + min + max); + return ::util_detail::clamp, tp>::clamp_(val, min, max); +} diff --git a/compat/functional.hpp b/compat/functional.hpp deleted file mode 100644 index dcba0538..00000000 --- a/compat/functional.hpp +++ /dev/null @@ -1,87 +0,0 @@ -#pragma once - -#include "value-templates.hpp" - -#include -#include -#include -#include - -namespace functools -{ - -template -struct reserver_ -{ - static inline void maybe_reserve_space(seq_&, unsigned) - { - //qDebug() << "nada"; - } -}; - -template -struct reserver_().reserve(0u), (void)0)> -{ - static inline void maybe_reserve_space(seq_& seq, unsigned sz) - { - seq.reserve(sz); - } -}; - -template -inline void maybe_reserve_space(seq_& seq, unsigned sz) -{ - reserver_::maybe_reserve_space(seq, sz); -} - -} // ns - -template -struct constant final -{ - using type = t; - constexpr type operator()() const noexcept - { - return value_; - } - static constexpr type value = value_; - - constant() = delete; -}; - -template -auto map(F&& fun, const seq_& seq) -{ - using value_type = std::decay_t::value_type>; - using ret_type = std::decay_t()))>; - - std::vector ret; - auto it = std::back_inserter(ret); - - for (const auto& elt : seq) - it = fun(elt); - - return ret; -} - -template -auto remove_if_not(F&& fun, const seq_& seq) -{ - using namespace functools; - - using seq_type = std::decay_t; - using value_type = std::decay_t::value_type>; - using fun_ret_type = decltype(fun(std::declval())); - static_assert(is_convertible_v, "must return bool"); - - seq_type ret; - maybe_reserve_space(ret, seq.size()); - - std::back_insert_iterator it = std::back_inserter(ret); - - for (const value_type& elt : seq) - if (fun(elt)) - it = elt; - - return ret; -} diff --git a/compat/macros.hpp b/compat/macros.hpp index e3b5976a..d51bace7 100644 --- a/compat/macros.hpp +++ b/compat/macros.hpp @@ -70,3 +70,8 @@ # define likely(x) (x) # define unlikely(x) (x) #endif + +#define progn(...) (([&]() { __VA_ARGS__ })()) +#define prog1(x, ...) (([&]() { auto _ret1324 = (x); do { __VA_ARGS__; } while (0); return _ret1324; })()) + +#define once_only(...) do { static bool once__ = false; if (!once__) { once__ = true; __VA_ARGS__; } } while(false) diff --git a/compat/meta.hpp b/compat/meta.hpp index e4c49ef6..ce81b3d0 100644 --- a/compat/meta.hpp +++ b/compat/meta.hpp @@ -8,6 +8,12 @@ */ #include + +template +using cv_qualified = std::conditional_t>, std::decay_t, const t&>; + +#if 0 + #include #include @@ -76,3 +82,5 @@ template using index_sequence = typename detail::index_sequence_::type; } // ns meta + +#endif diff --git a/compat/ndebug-guard.hpp b/compat/ndebug-guard.hpp deleted file mode 100644 index e38fd0fe..00000000 --- a/compat/ndebug-guard.hpp +++ /dev/null @@ -1,5 +0,0 @@ -#ifdef NDEBUG -# error "NDEBUG defined, don't define it" -#endif - -#include diff --git a/compat/round.hpp b/compat/round.hpp new file mode 100644 index 00000000..90a0ccb3 --- /dev/null +++ b/compat/round.hpp @@ -0,0 +1,15 @@ +#pragma once + +#include + +template +inline int iround(const t& val) +{ + return int(std::round(val)); +} + +template +inline unsigned uround(const t& val) +{ + return std::round(std::fmax(t(0), val)); +} diff --git a/compat/run-in-thread.hpp b/compat/run-in-thread.hpp index 67f4b1f5..c2243fc3 100644 --- a/compat/run-in-thread.hpp +++ b/compat/run-in-thread.hpp @@ -7,7 +7,7 @@ * copyright notice and this permission notice appear in all copies. */ -#include "ndebug-guard.hpp" +#include "macros.hpp" #include #include @@ -53,7 +53,8 @@ struct run_in_thread_traits } template -auto run_in_thread_sync(QObject* obj, F&& fun) +auto never_inline +run_in_thread_sync(QObject* obj, F&& fun) -> typename qt_impl_detail::run_in_thread_traits(fun)())>::ret_type { using lock_guard = std::unique_lock; diff --git a/compat/time.hpp b/compat/time.hpp index e65e4855..d34e4e8c 100644 --- a/compat/time.hpp +++ b/compat/time.hpp @@ -1,7 +1,5 @@ #pragma once -#include "compat/functional.hpp" - #include namespace time_units { diff --git a/compat/timer.cpp b/compat/timer.cpp index ff08c3d5..a3d91af4 100644 --- a/compat/timer.cpp +++ b/compat/timer.cpp @@ -6,7 +6,6 @@ * notice appear in all copies. */ -#include "ndebug-guard.hpp" #include #include "timer.hpp" diff --git a/compat/util.hpp b/compat/util.hpp index a1976e35..dda947e5 100644 --- a/compat/util.hpp +++ b/compat/util.hpp @@ -1,88 +1,10 @@ #pragma once #include "opentrack-library-path.h" -#include "ndebug-guard.hpp" #include "run-in-thread.hpp" #include "meta.hpp" -#include "functional.hpp" #include "macros.hpp" -#include "value-templates.hpp" - -#include -#include -#include -#include - -#include +#include "round.hpp" +#include "clamp.hpp" #include - -#define progn(...) (([&]() { __VA_ARGS__ })()) -#define prog1(x, ...) (([&]() { auto _ret1324 = (x); do { __VA_ARGS__; } while (0); return _ret1324; })()) - -#define once_only(...) do { static bool once = false; if (!once) { once = true; __VA_ARGS__; } } while(false) - -template -inline int iround(const t& val) -{ - return int(std::round(val)); -} - -template -inline unsigned uround(const t& val) -{ - return std::round(std::fmax(t(0), val)); -} - -namespace util_detail { - -template -inline auto clamp_float(n val, n min_, n max_) -{ - return std::fmin(std::fmax(val, min_), max_); -} - -template -struct clamp final -{ - static inline auto clamp_(const n& val, const n& min_, const n& max_) - { - if (unlikely(val > max_)) - return max_; - if (unlikely(val < min_)) - return min_; - return val; - } -}; - -template -struct clamp -{ - static inline auto clamp_(float val, float min_, float max_) - { - return clamp_float(val, min_, max_); - } -}; - -template -struct clamp -{ - static inline auto clamp_(double val, double min_, double max_) - { - return clamp_float(val, min_, max_); - } -}; - -} // ns util_detail - -template -inline auto clamp(const t& val, const u& min, const w& max) -{ - using tp = decltype(val + min + max); - return ::util_detail::clamp, tp>::clamp_(val, min, max); -} - -template -using cv_qualified = std::conditional_t>, std::decay_t, const t&>; - - diff --git a/compat/value-templates.hpp b/compat/value-templates.hpp deleted file mode 100644 index 0764b1ab..00000000 --- a/compat/value-templates.hpp +++ /dev/null @@ -1,192 +0,0 @@ -#pragma once - -#include -#include -#include -#include -#include -#include - -#if __cplusplus < 201703L - -template constexpr bool is_void_v = std::is_void::value; -template constexpr bool is_null_pointer_v = std::is_null_pointer::value; -template constexpr bool is_integral_v = std::is_integral::value; -template constexpr bool is_floating_point_v = std::is_floating_point::value; -template constexpr bool is_array_v = std::is_array::value; -template constexpr bool is_pointer_v = std::is_pointer::value; -template constexpr bool is_lvalue_reference_v = std::is_lvalue_reference::value; -template constexpr bool is_rvalue_reference_v = std::is_rvalue_reference::value; -template constexpr bool is_member_object_pointer_v = std::is_member_object_pointer::value; -template constexpr bool is_member_function_pointer_v = std::is_member_function_pointer::value; -template constexpr bool is_enum_v = std::is_enum::value; -template constexpr bool is_union_v = std::is_union::value; -template constexpr bool is_class_v = std::is_class::value; -template constexpr bool is_function_v = std::is_function::value; - -template constexpr bool is_reference_v = std::is_reference::value; -template constexpr bool is_arithmetic_v = std::is_arithmetic::value; -template constexpr bool is_fundamental_v = std::is_fundamental::value; -template constexpr bool is_object_v = std::is_object::value; -template constexpr bool is_scalar_v = std::is_scalar::value; -template constexpr bool is_compound_v = std::is_compound::value; -template constexpr bool is_member_pointer_v = std::is_member_pointer::value; - -template constexpr bool is_const_v = std::is_const::value; -template constexpr bool is_volatile_v = std::is_volatile::value; -template constexpr bool is_trivial_v = std::is_trivial::value; -template constexpr bool is_trivially_copyable_v = std::is_trivially_copyable::value; -template constexpr bool is_standard_layout_v = std::is_standard_layout::value; -template constexpr bool is_pod_v = std::is_pod::value; -template constexpr bool is_literal_type_v = std::is_literal_type::value; -template constexpr bool is_empty_v = std::is_empty::value; -template constexpr bool is_polymorphic_v = std::is_polymorphic::value; -template constexpr bool is_abstract_v = std::is_abstract::value; -template constexpr bool is_final_v = std::is_final::value; -template constexpr bool is_signed_v = std::is_signed::value; -template constexpr bool is_unsigned_v = std::is_unsigned::value; - -template constexpr bool is_constructible_v = std::is_constructible::value; -template constexpr bool is_trivially_constructible_v = std::is_trivially_constructible::value; -template constexpr bool is_nothrow_constructible_v = std::is_nothrow_constructible::value; -template constexpr bool is_default_constructible_v = std::is_default_constructible::value; -template constexpr bool is_trivially_default_constructible_v = std::is_trivially_default_constructible::value; -template constexpr bool is_nothrow_default_constructible_v = std::is_nothrow_default_constructible::value; -template constexpr bool is_copy_constructible_v = std::is_copy_constructible::value; -template constexpr bool is_trivially_copy_constructible_v = std::is_trivially_copy_constructible::value; -template constexpr bool is_nothrow_copy_constructible_v = std::is_nothrow_copy_constructible::value; -template constexpr bool is_move_constructible_v = std::is_move_constructible::value; -template constexpr bool is_trivially_move_constructible_v = std::is_trivially_move_constructible::value; -template constexpr bool is_nothrow_move_constructible_v = std::is_nothrow_move_constructible::value; -template constexpr bool is_assignable_v = std::is_assignable::value; -template constexpr bool is_trivially_assignable_v = std::is_trivially_assignable::value; -template constexpr bool is_nothrow_assignable_v = std::is_nothrow_assignable::value; -template constexpr bool is_copy_assignable_v = std::is_copy_assignable::value; -template constexpr bool is_trivially_copy_assignable_v = std::is_trivially_copy_assignable::value; -template constexpr bool is_nothrow_copy_assignable_v = std::is_nothrow_copy_assignable::value; -template constexpr bool is_move_assignable_v = std::is_move_assignable::value; -template constexpr bool is_trivially_move_assignable_v = std::is_trivially_move_assignable::value; -template constexpr bool is_nothrow_move_assignable_v = std::is_nothrow_move_assignable::value; -template constexpr bool is_destructible_v = std::is_destructible::value; -template constexpr bool is_trivially_destructible_v = std::is_trivially_destructible::value; -template constexpr bool is_nothrow_destructible_v = std::is_nothrow_destructible::value; -template constexpr bool has_virtual_destructor_v = std::has_virtual_destructor::value; - -template constexpr std::size_t alignment_of_v = std::alignment_of::value; -template constexpr std::size_t rank_v = std::rank::value; -template constexpr std::size_t extent_v = std::extent::value; - -template constexpr bool is_same_v = std::is_same::value; -template constexpr bool is_base_of_v = std::is_base_of::value; -template constexpr bool is_convertible_v = std::is_convertible::value; - -template constexpr bool ratio_equal_v = std::ratio_equal::value; -template constexpr bool ratio_not_equal_v = std::ratio_not_equal::value; -template constexpr bool ratio_less_v = std::ratio_less::value; -template constexpr bool ratio_less_equal_v = std::ratio_less_equal::value; -template constexpr bool ratio_greater_v = std::ratio_greater::value; -template constexpr bool ratio_greater_equal_v = std::ratio_greater_equal::value; - -template constexpr std::size_t tuple_size_v = std::tuple_size::value; -template constexpr bool treat_as_floating_point_v = std::chrono::treat_as_floating_point::value; - -template constexpr bool is_error_code_enum_v = std::is_error_code_enum::value; -template constexpr bool is_error_condition_enum_v = std::is_error_condition_enum::value; - -template constexpr bool is_bind_expression_v = std::is_bind_expression::value; -template constexpr bool is_placeholder_v = std::is_placeholder::value; - -template constexpr bool uses_allocator_v = std::uses_allocator::value; - -#else - -template constexpr bool is_void_v = std::is_void_v; -template constexpr bool is_null_pointer_v = std::is_null_pointer_v; -template constexpr bool is_integral_v = std::is_integral_v; -template constexpr bool is_floating_point_v = std::is_floating_point_v; -template constexpr bool is_array_v = std::is_array_v; -template constexpr bool is_pointer_v = std::is_pointer_v; -template constexpr bool is_lvalue_reference_v = std::is_lvalue_reference_v; -template constexpr bool is_rvalue_reference_v = std::is_rvalue_reference_v; -template constexpr bool is_member_object_pointer_v = std::is_member_object_pointer_v; -template constexpr bool is_member_function_pointer_v = std::is_member_function_pointer_v; -template constexpr bool is_enum_v = std::is_enum_v; -template constexpr bool is_union_v = std::is_union_v; -template constexpr bool is_class_v = std::is_class_v; -template constexpr bool is_function_v = std::is_function_v; - -template constexpr bool is_reference_v = std::is_reference_v; -template constexpr bool is_arithmetic_v = std::is_arithmetic_v; -template constexpr bool is_fundamental_v = std::is_fundamental_v; -template constexpr bool is_object_v = std::is_object_v; -template constexpr bool is_scalar_v = std::is_scalar_v; -template constexpr bool is_compound_v = std::is_compound_v; -template constexpr bool is_member_pointer_v = std::is_member_pointer_v; - -template constexpr bool is_const_v = std::is_const_v; -template constexpr bool is_volatile_v = std::is_volatile_v; -template constexpr bool is_trivial_v = std::is_trivial_v; -template constexpr bool is_trivially_copyable_v = std::is_trivially_copyable_v; -template constexpr bool is_standard_layout_v = std::is_standard_layout_v; -template constexpr bool is_pod_v = std::is_pod_v; -template constexpr bool is_literal_type_v = std::is_literal_type_v; -template constexpr bool is_empty_v = std::is_empty_v; -template constexpr bool is_polymorphic_v = std::is_polymorphic_v; -template constexpr bool is_abstract_v = std::is_abstract_v; -template constexpr bool is_final_v = std::is_final_v; -template constexpr bool is_signed_v = std::is_signed_v; -template constexpr bool is_unsigned_v = std::is_unsigned_v; - -template constexpr bool is_constructible_v = std::is_constructible_v; -template constexpr bool is_trivially_constructible_v = std::is_trivially_constructible_v; -template constexpr bool is_nothrow_constructible_v = std::is_nothrow_constructible_v; -template constexpr bool is_default_constructible_v = std::is_default_constructible_v; -template constexpr bool is_trivially_default_constructible_v = std::is_trivially_default_constructible_v; -template constexpr bool is_nothrow_default_constructible_v = std::is_nothrow_default_constructible_v; -template constexpr bool is_copy_constructible_v = std::is_copy_constructible_v; -template constexpr bool is_trivially_copy_constructible_v = std::is_trivially_copy_constructible_v; -template constexpr bool is_nothrow_copy_constructible_v = std::is_nothrow_copy_constructible_v; -template constexpr bool is_move_constructible_v = std::is_move_constructible_v; -template constexpr bool is_trivially_move_constructible_v = std::is_trivially_move_constructible_v; -template constexpr bool is_nothrow_move_constructible_v = std::is_nothrow_move_constructible_v; -template constexpr bool is_assignable_v = std::is_assignable_v; -template constexpr bool is_trivially_assignable_v = std::is_trivially_assignable_v; -template constexpr bool is_nothrow_assignable_v = std::is_nothrow_assignable_v; -template constexpr bool is_copy_assignable_v = std::is_copy_assignable_v; -template constexpr bool is_trivially_copy_assignable_v = std::is_trivially_copy_assignable_v; -template constexpr bool is_nothrow_copy_assignable_v = std::is_nothrow_copy_assignable_v; -template constexpr bool is_move_assignable_v = std::is_move_assignable_v; -template constexpr bool is_trivially_move_assignable_v = std::is_trivially_move_assignable_v; -template constexpr bool is_nothrow_move_assignable_v = std::is_nothrow_move_assignable_v; -template constexpr bool is_destructible_v = std::is_destructible_v; -template constexpr bool is_trivially_destructible_v = std::is_trivially_destructible_v; -template constexpr bool is_nothrow_destructible_v = std::is_nothrow_destructible_v; -template constexpr bool has_virtual_destructor_v = std::has_virtual_destructor_v; - -template constexpr std::size_t alignment_of_v = std::alignment_of_v; -template constexpr std::size_t rank_v = std::rank_v; -template constexpr std::size_t extent_v = std::extent_v; - -template constexpr bool is_same_v = std::is_same_v; -template constexpr bool is_base_of_v = std::is_base_of_v; -template constexpr bool is_convertible_v = std::is_convertible_v; - -template constexpr bool ratio_equal_v = std::ratio_equal_v; -template constexpr bool ratio_not_equal_v = std::ratio_not_equal_v; -template constexpr bool ratio_less_v = std::ratio_less_v; -template constexpr bool ratio_less_equal_v = std::ratio_less_equal_v; -template constexpr bool ratio_greater_v = std::ratio_greater_v; -template constexpr bool ratio_greater_equal_v = std::ratio_greater_equal_v; - -template constexpr std::size_t tuple_size_v = std::tuple_size_v; -template constexpr bool treat_as_floating_point_v = std::chrono::treat_as_floating_point_v; - -template constexpr bool is_error_code_enum_v = std::is_error_code_enum_v; -template constexpr bool is_error_condition_enum_v = std::is_error_condition_enum_v; - -template constexpr bool is_bind_expression_v = std::is_bind_expression_v; -template constexpr bool is_placeholder_v = std::is_placeholder_v; - -template constexpr bool uses_allocator_v = std::uses_allocator_v; - -#endif diff --git a/options/options.hpp b/options/options.hpp index 0528b0d9..45697597 100644 --- a/options/options.hpp +++ b/options/options.hpp @@ -6,8 +6,6 @@ */ #pragma once -#include "compat/ndebug-guard.hpp" - #include "compat/util.hpp" #include "defs.hpp" diff --git a/options/value-traits.hpp b/options/value-traits.hpp index 3548c77a..ac5f6e5c 100644 --- a/options/value-traits.hpp +++ b/options/value-traits.hpp @@ -1,6 +1,5 @@ #include "export.hpp" -#include "compat/functional.hpp" #include "slider.hpp" #include @@ -37,7 +36,7 @@ struct value_traits : default_value_traits // Qt uses int a lot in slots so use it for all enums template -struct value_traits>> : public default_value_traits +struct value_traits>> : public default_value_traits { }; diff --git a/proto-ft/ftnoir_protocol_ft.cpp b/proto-ft/ftnoir_protocol_ft.cpp index 7fa34a84..04a5a4cf 100644 --- a/proto-ft/ftnoir_protocol_ft.cpp +++ b/proto-ft/ftnoir_protocol_ft.cpp @@ -6,17 +6,18 @@ * copyright notice and this permission notice appear in all copies. */ -#include "compat/ndebug-guard.hpp" #include "compat/util.hpp" #include "ftnoir_protocol_ft.h" #include "csv/csv.h" #include "opentrack-library-path.h" +#include #include #include #include #include + #include #include diff --git a/proto-libevdev/CMakeLists.txt b/proto-libevdev/CMakeLists.txt index d6cd4560..412cca43 100644 --- a/proto-libevdev/CMakeLists.txt +++ b/proto-libevdev/CMakeLists.txt @@ -1,5 +1,4 @@ if(LINUX) - include(FindPkgConfig) pkg_check_modules(libevdev QUIET libevdev) if(libevdev_FOUND) otr_module(proto-libevdev) diff --git a/tracker-fusion/fusion.cpp b/tracker-fusion/fusion.cpp index 0b81b682..beeb7f53 100644 --- a/tracker-fusion/fusion.cpp +++ b/tracker-fusion/fusion.cpp @@ -6,7 +6,6 @@ * notice appear in all copies. */ -#include "compat/ndebug-guard.hpp" #include "fusion.h" #include "opentrack-library-path.h" diff --git a/tracker-pt/camera.h b/tracker-pt/camera.h index 030861c7..42eda9f5 100644 --- a/tracker-pt/camera.h +++ b/tracker-pt/camera.h @@ -7,7 +7,6 @@ #pragma once -#include "compat/ndebug-guard.hpp" #include "pt-api.hpp" #include "compat/util.hpp" -- cgit v1.2.3