IDS peak ICV v1.2.0
Loading...
Searching...
No Matches
peak_common_size.hpp
Go to the documentation of this file.
1
11#pragma once
12
17#include <peak_common_c/detail/peak_common_defines.h>
18
19#include <cassert>
20#include <cstdint>
21#include <sstream>
22
23namespace peak
24{
25namespace common
26{
27
28namespace detail
29{
40template <typename T>
41class SizeT
42{
43
44public:
45 static_assert(std::is_arithmetic<T>::value, "Template argument of Size must be arithmetic.");
46
54 constexpr SizeT() = default;
55
66 template <typename H = T
68 ,
69 typename std::enable_if_t<std::is_integral<H>::value, int> = 0
71 >
72 constexpr SizeT(T width, T height)
73 : m_width{ width }
74 , m_height{ height }
75 {
76 static_assert(std::is_unsigned<T>::value, "Parameter width and/or height has to be greater or equal to zero.");
77 }
78
92 template <typename H = T
94 ,
95 typename std::enable_if_t<std::is_floating_point<H>::value, int> = 0
97 >
98 constexpr SizeT(T width, T height)
99 : m_width{ width }
100 , m_height{ height }
101 {
102 if (width < 0)
103 {
104 throw peak::common::InvalidParameterException("Parameter width has to be greater or equal to zero.");
105 }
106 if (height < 0)
107 {
108 throw peak::common::InvalidParameterException("Parameter height has to be greater or equal to zero.");
109 }
110 }
111
119 PEAK_COMMON_NO_DISCARD constexpr T GetWidth() const
120 {
121 return m_width;
122 }
123
131 PEAK_COMMON_NO_DISCARD constexpr T GetHeight() const
132 {
133 return m_height;
134 }
135
143 PEAK_COMMON_NO_DISCARD constexpr SizeT<T> Transposed() const
144 {
145 return { m_height, m_width };
146 }
147
158 template <typename H
160 ,
161 typename std::enable_if_t<std::is_integral<H>::value, int> = 0
163 >
164 constexpr bool operator==(const SizeT<H>& rhs) const
165 {
166 return GetHeight() == rhs.GetHeight() && GetWidth() == rhs.GetWidth();
167 }
168
179 template <typename H
181 ,
182 typename std::enable_if_t<std::is_floating_point<H>::value, int> = 0
184 >
185 constexpr bool operator==(const SizeT<H>& rhs) const
186 {
187 // Unit in the last place
188 constexpr uint32_t ulp = 4;
189
190 using common_type = std::common_type_t<H, T>;
191
192 return detail::AreAlmostEqual<common_type>(static_cast<common_type>(GetWidth()), static_cast<common_type>(rhs.GetWidth()), ulp)
193 && detail::AreAlmostEqual<common_type>(static_cast<common_type>(GetHeight()), static_cast<common_type>(rhs.GetHeight()), ulp);
194 }
195
206 template <typename H>
207 constexpr bool operator!=(const SizeT<H>& rhs) const
208 {
209 return !(*this == rhs);
210 }
211
223 friend std::ostream& operator<<(std::ostream& os, SizeT size)
224 {
225 return os << "{ width: " << size.GetWidth() << ", height: " << size.GetHeight() << " }";
226 }
227
237 PEAK_COMMON_NO_DISCARD std::string ToString() const
238 {
239 std::ostringstream ss;
240 ss << *this;
241 return ss.str();
242 }
243
244private:
245 T m_width{};
246 T m_height{};
247
248 friend detail::BackendAccessor<SizeT<T>>;
249
250 template <typename U = T, typename std::enable_if<has_c_type_of<SizeT<U>>::value, bool>::type = false>
251 constexpr explicit operator detail::c_type_of_t<SizeT<T>>() const // NOLINT
252 {
253 return { this->GetWidth(), this->GetHeight() };
254 }
255};
256
257} // namespace detail
258
274
290
291} // namespace common
292} // namespace peak
Thrown when a function receives an invalid parameter.
Represents a two-dimensional size with non-negative width and height.
std::string ToString() const
Convert the SizeT instance to a string representation.
constexpr T GetWidth() const
Returns the width.
constexpr T GetHeight() const
Returns the height.
constexpr bool operator!=(const SizeT< H > &rhs) const
Inequality operator.
friend std::ostream & operator<<(std::ostream &os, SizeT size)
Stream operator to output SizeT in a readable format.
constexpr SizeT< T > Transposed() const
Returns a transposed size with width and height swapped.
constexpr SizeT()=default
Default constructor for SizeT.
constexpr bool operator==(const SizeT< H > &rhs) const
Equality operator for integral types.
constexpr SizeT(T width, T height)
Constructs a SizeT object from integral width and height.
constexpr bool AreAlmostEqual(H lhs, H rhs, uint32_t ulp)
Compares two floating-point values for approximate equality using a ULP-based tolerance.
typename c_type_of_< Type >::type c_type_of_t