OGLplus  (0.59.0) a C++ wrapper for rendering APIs

compare.hpp
Go to the documentation of this file.
1 #ifndef EAGINE_COMPARE_HPP
9 #define EAGINE_COMPARE_HPP
10 
11 #include "type_identity.hpp"
12 #include <limits>
13 #include <type_traits>
14 #include <utility>
15 
16 namespace eagine {
17 //------------------------------------------------------------------------------
18 template <typename L, typename R>
19 struct equal_cmp_any {
20  static constexpr auto check(const L& l, const R& r) noexcept {
21  return l == r;
22  }
23 };
24 //------------------------------------------------------------------------------
25 template <typename L, typename R, bool LSign, bool RSign>
26 struct equal_cmp_int;
27 //------------------------------------------------------------------------------
28 template <typename L, typename R, bool Sign>
29 struct equal_cmp_int<L, R, Sign, Sign> {
30  static constexpr auto check(L l, R r) noexcept {
31  return l == r;
32  }
33 };
34 //------------------------------------------------------------------------------
35 template <typename L, typename R>
36 struct equal_cmp_int<L, R, true, false> {
37  static constexpr auto check(L l, R r) noexcept {
38  using Tmp = std::make_unsigned_t<L>;
39  return (l < 0) ? false : Tmp(l) == r;
40  }
41 };
42 //------------------------------------------------------------------------------
43 template <typename L, typename R>
44 struct equal_cmp_int<L, R, false, true> : equal_cmp_int<R, L, true, false> {};
45 //------------------------------------------------------------------------------
46 template <typename L, typename R>
47 using equal_cmp_pick = std::conditional_t<
48  std::is_integral_v<L> && std::is_integral_v<R>,
49  equal_cmp_int<L, R, std::is_signed_v<L>, std::is_signed_v<R>>,
50  equal_cmp_any<L, R>>;
51 //------------------------------------------------------------------------------
52 template <typename L, typename R>
53 struct equal_cmp : equal_cmp_pick<L, R> {};
54 //------------------------------------------------------------------------------
55 template <>
56 struct equal_cmp<float, float> {
57  static constexpr auto check(float l, float r) noexcept {
58  return !((l - r) < 0.F || (l - r) > 0.F);
59  }
60 };
61 //------------------------------------------------------------------------------
62 template <>
63 struct equal_cmp<double, double> {
64  static constexpr auto check(double l, double r) noexcept {
65  return !((l - r) < 0.0 || (l - r) > 0.0);
66  }
67 };
68 //------------------------------------------------------------------------------
69 template <typename L, typename R>
70 struct equal_cmp_obj : equal_cmp<L, R> {
71  constexpr equal_cmp_obj(L l, R r) noexcept
72  : _l{std::move(l)}
73  , _r{std::move(r)} {}
74 
75  constexpr explicit inline operator bool() const noexcept {
76  return equal_cmp<L, R>::check(_l, _r);
77  }
78 
79  L _l{};
80  R _r{};
81 };
82 //------------------------------------------------------------------------------
83 template <typename T>
84 struct cmp_decay_to : type_identity<T> {};
85 template <typename T>
86 using cmp_decay_to_t = typename cmp_decay_to<T>::type;
87 //------------------------------------------------------------------------------
88 template <typename L, typename R>
89 static constexpr auto are_equal(const L& l, const R& r) noexcept {
90  return equal_cmp<cmp_decay_to_t<L>, cmp_decay_to_t<R>>::check(l, r);
91 }
92 //------------------------------------------------------------------------------
93 } // namespace eagine
94 
95 #endif // EAGINE_COMPARE_HPP
Common code is placed in this namespace.
Definition: eagine.hpp:21

Copyright © 2015-2021 Matúš Chochlík.
<chochlik -at -gmail.com>
Documentation generated on Tue Apr 13 2021 by Doxygen (version 1.8.17).