MyGUI 3.4.3
MyGUI_TSize.h
Go to the documentation of this file.
1/*
2 * This source file is part of MyGUI. For the latest info, see http://mygui.info/
3 * Distributed under the MIT License
4 * (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT)
5 */
6
7#ifndef MYGUI_TSIZE_H_
8#define MYGUI_TSIZE_H_
9
10#include "MyGUI_Prerequest.h"
11#include "MyGUI_StringUtility.h"
12
13namespace MyGUI::types
14{
15
16 template<typename T>
17 struct TSize
18 {
19 T width{};
20 T height{};
21
22 TSize() = default;
23
24 TSize(T const& _width, T const& _height) :
25 width(_width),
26 height(_height)
27 {
28 }
29
30 TSize& operator-=(TSize const& _obj)
31 {
32 width -= _obj.width;
33 height -= _obj.height;
34 return *this;
35 }
36
37 TSize& operator+=(TSize const& _obj)
38 {
39 width += _obj.width;
40 height += _obj.height;
41 return *this;
42 }
43
44 TSize operator-(TSize const& _obj) const
45 {
46 return TSize(width - _obj.width, height - _obj.height);
47 }
48
49 TSize operator+(TSize const& _obj) const
50 {
51 return TSize(width + _obj.width, height + _obj.height);
52 }
53
54 template<typename U>
55 TSize& operator=(TSize<U> const& _obj)
56 {
57 width = _obj.width;
58 height = _obj.height;
59 return *this;
60 }
61
62 bool operator==(TSize const& _obj) const
63 {
64 return ((width == _obj.width) && (height == _obj.height));
65 }
66
67 bool operator!=(TSize const& _obj) const
68 {
69 return !((width == _obj.width) && (height == _obj.height));
70 }
71
72 void clear()
73 {
74 width = height = 0;
75 }
76
77 void set(T const& _width, T const& _height)
78 {
79 width = _width;
80 height = _height;
81 }
82
83 void swap(TSize& _value)
84 {
85 TSize tmp = _value;
86 _value = *this;
87 *this = tmp;
88 }
89
90 bool empty() const
91 {
92 return ((width == 0) && (height == 0));
93 }
94
95 std::string print() const
96 {
97 std::ostringstream stream;
98 stream << *this;
99 return stream.str();
100 }
101
102 static TSize<T> parse(std::string_view _value)
103 {
104 return utility::parseValue<TSize<T>>(_value);
105 }
106
107 friend std::ostream& operator<<(std::ostream& _stream, const TSize<T>& _value)
108 {
109 _stream << _value.width << " " << _value.height;
110 return _stream;
111 }
112
113 friend std::istream& operator>>(std::istream& _stream, TSize<T>& _value)
114 {
115 _stream >> _value.width >> _value.height;
116 if (_stream.fail())
117 _value.clear();
118 return _stream;
119 }
120 };
121
122} // namespace MyGUI
123
124#endif // MYGUI_TSIZE_H_
T parseValue(std::string_view _value)
friend std::istream & operator>>(std::istream &_stream, TSize< T > &_value)
friend std::ostream & operator<<(std::ostream &_stream, const TSize< T > &_value)
TSize operator+(TSize const &_obj) const
Definition MyGUI_TSize.h:49
bool empty() const
Definition MyGUI_TSize.h:90
TSize operator-(TSize const &_obj) const
Definition MyGUI_TSize.h:44
static TSize< T > parse(std::string_view _value)
TSize & operator+=(TSize const &_obj)
Definition MyGUI_TSize.h:37
TSize(T const &_width, T const &_height)
Definition MyGUI_TSize.h:24
std::string print() const
Definition MyGUI_TSize.h:95
bool operator!=(TSize const &_obj) const
Definition MyGUI_TSize.h:67
TSize & operator=(TSize< U > const &_obj)
Definition MyGUI_TSize.h:55
TSize & operator-=(TSize const &_obj)
Definition MyGUI_TSize.h:30
void set(T const &_width, T const &_height)
Definition MyGUI_TSize.h:77
bool operator==(TSize const &_obj) const
Definition MyGUI_TSize.h:62
void swap(TSize &_value)
Definition MyGUI_TSize.h:83