IDS peak IPL v1.18.0
Loading...
Searching...
No Matches
peak_common_size.hpp
Go to the documentation of this file.
1
5
6#pragma once
7
13
14#include <cassert>
15#include <cstdint>
16#include <sstream>
17
18namespace peak
19{
20namespace common
21{
22
23namespace detail
24{
35template <typename T>
36class SizeT
37{
38
39public:
40 static_assert(std::is_arithmetic<T>::value, "Template argument of Size must be arithmetic.");
41
49 constexpr SizeT() = default;
50
61 template <typename H = T
63 ,
64 typename std::enable_if_t<std::is_integral<H>::value, int> = 0
66 >
67 constexpr SizeT(T width, T height)
68 : m_width{ width }
69 , m_height{ height }
70 {
71 static_assert(std::is_unsigned<T>::value, "Parameter width and/or height has to be greater or equal to zero.");
72 }
73
87 template <typename H = T
89 ,
90 typename std::enable_if_t<std::is_floating_point<H>::value, int> = 0
92 >
93 constexpr SizeT(T width, T height)
94 : m_width{ width }
95 , m_height{ height }
96 {
97 if (width < 0)
98 {
99 throw peak::common::InvalidParameterException("Parameter width has to be greater or equal to zero.");
100 }
101 if (height < 0)
102 {
103 throw peak::common::InvalidParameterException("Parameter height has to be greater or equal to zero.");
104 }
105 }
106
115 {
116 return m_width;
117 }
118
127 {
128 return m_height;
129 }
130
139 {
140 return { m_height, m_width };
141 }
142
153 template <typename H
155 ,
156 typename std::enable_if_t<std::is_integral<H>::value, int> = 0
158 >
159 constexpr bool operator==(const SizeT<H>& rhs) const
160 {
161 return GetHeight() == rhs.GetHeight() && GetWidth() == rhs.GetWidth();
162 }
163
174 template <typename H
176 ,
177 typename std::enable_if_t<std::is_floating_point<H>::value, int> = 0
179 >
180 constexpr bool operator==(const SizeT<H>& rhs) const
181 {
182 // Unit in the last place
183 constexpr uint32_t ulp = 4;
184
185 using common_type = std::common_type_t<H, T>;
186
187 return detail::AreAlmostEqual<common_type>(static_cast<common_type>(GetWidth()), static_cast<common_type>(rhs.GetWidth()), ulp)
188 && detail::AreAlmostEqual<common_type>(static_cast<common_type>(GetHeight()), static_cast<common_type>(rhs.GetHeight()), ulp);
189 }
190
201 template <typename H>
202 constexpr bool operator!=(const SizeT<H>& rhs) const
203 {
204 return !(*this == rhs);
205 }
206
218 friend std::ostream& operator<<(std::ostream& os, SizeT size)
219 {
220 return os << "{ width: " << size.GetWidth() << ", height: " << size.GetHeight() << " }";
221 }
222
233 {
234 std::ostringstream ss;
235 ss << *this;
236 return ss.str();
237 }
238
239private:
240 T m_width{};
241 T m_height{};
242
243 friend detail::BackendAccessor<SizeT<T>>;
244
245 template <typename U = T, typename std::enable_if<has_c_type_of<SizeT<U>>::value, bool>::type = false>
246 constexpr explicit operator detail::c_type_of_t<SizeT<T>>() const // NOLINT
247 {
248 return { this->GetWidth(), this->GetHeight() };
249 }
250};
251
252} // namespace detail
253
269
285
286} // namespace common
287} // namespace peak
Thrown when a function receives an invalid parameter.
Definition peak_common_exceptions.hpp:53
Represents a two-dimensional size with non-negative width and height.
Definition peak_common_size.hpp:37
PEAK_COMMON_NO_DISCARD constexpr T GetWidth() const
Returns the width.
Definition peak_common_size.hpp:114
constexpr SizeT(T width, T height)
Constructs a SizeT object from integral width and height.
Definition peak_common_size.hpp:67
PEAK_COMMON_NO_DISCARD constexpr T GetHeight() const
Returns the height.
Definition peak_common_size.hpp:126
constexpr bool operator!=(const SizeT< H > &rhs) const
Inequality operator.
Definition peak_common_size.hpp:202
constexpr SizeT()=default
Default constructor for SizeT.
PEAK_COMMON_NO_DISCARD std::string ToString() const
Convert the SizeT instance to a string representation.
Definition peak_common_size.hpp:232
constexpr bool operator==(const SizeT< H > &rhs) const
Equality operator for integral types.
Definition peak_common_size.hpp:159
PEAK_COMMON_NO_DISCARD constexpr SizeT< T > Transposed() const
Returns a transposed size with width and height swapped.
Definition peak_common_size.hpp:138
friend std::ostream & operator<<(std::ostream &os, SizeT size)
Stream operator to output SizeT in a readable format.
Definition peak_common_size.hpp:218
constexpr bool AreAlmostEqual(H lhs, H rhs, uint32_t ulp)
Compares two floating-point values for approximate equality using a ULP-based tolerance.
Definition peak_common_math.hpp:45
detail::SizeT< float > SizeF
Size with floating-point dimensions.
Definition peak_common_size.hpp:284
detail::SizeT< uint32_t > Size
Size with unsigned 32-bit integer dimensions.
Definition peak_common_size.hpp:268
Namespace for internal implementation details and low-level types.
Definition peak_common_math.hpp:19
Root namespace for common types and interfaces shared across multiple libraries.
Definition peak_common_math.hpp:17
#define PEAK_COMMON_NO_DISCARD
Definition peak_common_defines.h:89