utility 2026.1.9
A comprehensive C++ utilities library tailored for the development of modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
size.hpp
1/*
2 Copyright (c) 2026 ETIB Corporation
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in
6 the Software without restriction, including without limitation the rights to
7 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8 of the Software, and to permit persons to whom the Software is furnished to do
9 so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
21*/
22
23#pragma once
24
25#include <concepts>
26#include <ostream>
27
28namespace utility::graphic
29{
30
35 template<typename Type>
37 std::is_floating_point_v<Type> || std::is_integral_v<Type>;
38
48 template<CanBeSizeComponent SizeComponentType> class Size
49 {
50 private:
51 SizeComponentType _width;
52 SizeComponentType _height;
53 SizeComponentType _depth;
54
55 public:
60 : _width(SizeComponentType { 0 })
61 , _height(SizeComponentType { 0 })
62 , _depth(SizeComponentType { 0 })
63 {
64 }
71 Size(SizeComponentType width, SizeComponentType height,
72 SizeComponentType depth = SizeComponentType { 0 })
73 : _width(width)
74 , _height(height)
75 , _depth(depth)
76 {
77 }
78
83 Size(const Size &other) = default;
84
89 Size(Size &&other) noexcept = default;
90
96 Size &operator=(const Size &other) = default;
97
103 Size &operator=(Size &&other) noexcept = default;
104
108 ~Size(void) = default;
109
115 Size &setWidth(SizeComponentType width) noexcept
116 {
117 _width = width;
118 return *this;
119 }
120
125 const SizeComponentType &getWidth(void) const noexcept
126 {
127 return _width;
128 }
129
135 Size &setHeight(SizeComponentType height) noexcept
136 {
137 _height = height;
138 return *this;
139 }
140
145 const SizeComponentType &getHeight(void) const noexcept
146 {
147 return _height;
148 }
149
155 Size &setDepth(SizeComponentType depth) noexcept
156 {
157 _depth = depth;
158 return *this;
159 }
160
165 const SizeComponentType &getDepth(void) const noexcept
166 {
167 return _depth;
168 }
169
175 bool operator==(const Size &other) const noexcept
176 {
177 return _width == other._width && _height == other._height
178 && _depth == other._depth;
179 }
180
186 bool operator!=(const Size &other) const noexcept
187 {
188 return !(*this == other);
189 }
190
196 bool operator<(const Size &other) const noexcept
197 {
198 if (_width != other._width) {
199 return _width < other._width;
200 }
201 if (_height != other._height) {
202 return _height < other._height;
203 }
204 return false;
205 }
206 };
207
211 using SizeF = Size<float>;
212
216 using SizeD = Size<double>;
217
221 using SizeI = Size<int>;
222
226 using SizeUI = Size<unsigned int>;
227
234 std::ostream &operator<<(std::ostream &stream, const SizeF &size);
235
242 std::ostream &operator<<(std::ostream &stream, const SizeD &size);
243
250 std::ostream &operator<<(std::ostream &stream, const SizeI &size);
251
258 std::ostream &operator<<(std::ostream &stream, const SizeUI &size);
259
260} // namespace utility::graphic
Template class representing a size (width, height).
Definition size.hpp:49
Size & setWidth(SizeComponentType width) noexcept
Set the width component.
Definition size.hpp:115
Size & setHeight(SizeComponentType height) noexcept
Set the height component.
Definition size.hpp:135
Size & operator=(const Size &other)=default
Copy assignment operator.
Size()
Default constructor (zero position, identity orientation).
Definition size.hpp:59
Size(const Size &other)=default
Copy constructor.
bool operator<(const Size &other) const noexcept
Less-than comparison for ordering.
Definition size.hpp:196
bool operator!=(const Size &other) const noexcept
Inequality comparison.
Definition size.hpp:186
Size(Size &&other) noexcept=default
Move constructor.
const SizeComponentType & getWidth(void) const noexcept
Get the position component.
Definition size.hpp:125
bool operator==(const Size &other) const noexcept
Equality comparison.
Definition size.hpp:175
const SizeComponentType & getDepth(void) const noexcept
Get the depth component.
Definition size.hpp:165
Size & operator=(Size &&other) noexcept=default
Move assignment operator.
Size & setDepth(SizeComponentType depth) noexcept
Set the depth component.
Definition size.hpp:155
const SizeComponentType & getHeight(void) const noexcept
Get the height component.
Definition size.hpp:145
Size(SizeComponentType width, SizeComponentType height, SizeComponentType depth=SizeComponentType { 0 })
Construct a size from width and height.
Definition size.hpp:71
~Size(void)=default
Default destructor for Size.
Concept to constrain size component type.
Definition size.hpp:36