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

sign.hpp
Go to the documentation of this file.
1 #ifndef EAGINE_MATH_SIGN_HPP
9 #define EAGINE_MATH_SIGN_HPP
10 
11 #include <type_traits>
12 
13 namespace eagine::math {
14 //------------------------------------------------------------------------------
19 template <typename T>
20 class sign {
21 public:
23  using type = sign;
24 
26  using value_type = T;
27 
29  constexpr sign() noexcept = default;
30 
32  constexpr explicit sign(bool pos) noexcept
33  : _positive{pos} {}
34 
36  template <
37  typename X,
38  typename = std::enable_if_t<!std::is_same_v<X, sign<T>>>>
39  constexpr explicit sign(const X& value) noexcept
40  : _positive{value >= X(0)} {}
41 
43  template <
44  typename X,
45  typename = std::enable_if_t<!std::is_same_v<X, sign<T>>>>
46  auto operator=(const X& value) noexcept -> auto& {
47  _positive = (value >= X(0));
48  return *this;
49  }
50 
53  static constexpr auto plus() noexcept -> sign {
54  return {true};
55  }
56 
59  static constexpr auto minus() noexcept -> sign {
60  return {false};
61  }
62 
64  operator T() const noexcept {
65  return _positive ? T{1} : T{-1};
66  }
67 
70  auto flip() noexcept -> auto& {
71  _positive = !_positive;
72  return *this;
73  }
74 
77  auto flipped() const noexcept -> sign {
78  return {!_positive};
79  }
80 
84  auto operator-() const noexcept -> sign {
85  return flipped();
86  }
87 
91  auto operator!() const noexcept -> sign {
92  return flipped();
93  }
94 
95 private:
96  bool _positive{true};
97 };
98 //------------------------------------------------------------------------------
99 } // namespace eagine::math
100 
101 #endif // EAGINE_MATH_SIGN_HPP
auto operator-() const noexcept -> sign
Returns a new sign opposite to this sign.
Definition: sign.hpp:84
float value_type
Alias for the value type T.
Definition: sign.hpp:26
constexpr sign(const X &value) noexcept
Construction taking the sign from the specified numeric value.
Definition: sign.hpp:39
auto operator!() const noexcept -> sign
Returns a new sign opposite to this sign.
Definition: sign.hpp:91
Class representing a positive or negative numeric sign.
Definition: sign.hpp:20
Math-related code is placed in this namespace.
Definition: eagine.hpp:48
auto flipped() const noexcept -> sign
Returns a new sign opposite to this sign.
Definition: sign.hpp:77
constexpr sign() noexcept=default
Default constructor (constructs a positive sign).
static constexpr auto plus() noexcept -> sign
Constructs a positive sign (plus).
Definition: sign.hpp:53
auto flip() noexcept -> auto &
Flip this sign from positive to negative or vice-versa.
Definition: sign.hpp:70
auto operator=(const X &value) noexcept -> auto &
Assignment taking the sign from the specified numeric value.
Definition: sign.hpp:46
static constexpr auto minus() noexcept -> sign
Constructs a negative sign (minus).
Definition: sign.hpp:59

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