MyGUI 3.4.1
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
12namespace MyGUI
13{
14 namespace types
15 {
16
17 template<typename T>
18 struct TSize
19 {
22
24 width(0),
25 height(0)
26 {
27 }
28
29 TSize(T const& _width, T const& _height) :
30 width(_width),
31 height(_height)
32 {
33 }
34
35 TSize(TSize const& _obj) :
36 width(_obj.width),
37 height(_obj.height)
38 {
39 }
40
41 TSize& operator -= (TSize const& _obj)
42 {
43 width -= _obj.width;
44 height -= _obj.height;
45 return *this;
46 }
47
48 TSize& operator += (TSize const& _obj)
49 {
50 width += _obj.width;
51 height += _obj.height;
52 return *this;
53 }
54
55 TSize operator - (TSize const& _obj) const
56 {
57 return TSize(width - _obj.width, height - _obj.height);
58 }
59
60 TSize operator + (TSize const& _obj) const
61 {
62 return TSize(width + _obj.width, height + _obj.height);
63 }
64
65 TSize& operator = (TSize const& _obj)
66 {
67 width = _obj.width;
68 height = _obj.height;
69 return *this;
70 }
71
72 template<typename U>
74 {
75 width = _obj.width;
76 height = _obj.height;
77 return *this;
78 }
79
80 bool operator == (TSize const& _obj) const
81 {
82 return ((width == _obj.width) && (height == _obj.height));
83 }
84
85 bool operator != (TSize const& _obj) const
86 {
87 return !((width == _obj.width) && (height == _obj.height));
88 }
89
90 void clear()
91 {
92 width = height = 0;
93 }
94
95 void set(T const& _width, T const& _height)
96 {
97 width = _width;
98 height = _height;
99 }
100
101 void swap(TSize& _value)
102 {
103 TSize tmp = _value;
104 _value = *this;
105 *this = tmp;
106 }
107
108 bool empty() const
109 {
110 return ((width == 0) && (height == 0));
111 }
112
113 std::string print() const
114 {
115 std::ostringstream stream;
116 stream << *this;
117 return stream.str();
118 }
119
120 static TSize<T> parse(const std::string& _value)
121 {
122 TSize<T> result;
123 std::istringstream stream(_value);
124 stream >> result.width >> result.height;
125 if (stream.fail())
126 {
127 return TSize<T>();
128 }
129 else
130 {
131 int item = stream.get();
132 while (item != -1)
133 {
134 if (item != ' ' && item != '\t')
135 return TSize<T>();
136 item = stream.get();
137 }
138 }
139 return result;
140 }
141
142 friend std::ostream& operator << (std::ostream& _stream, const TSize<T>& _value)
143 {
144 _stream << _value.width << " " << _value.height;
145 return _stream;
146 }
147
148 friend std::istream& operator >> (std::istream& _stream, TSize<T>& _value)
149 {
150 _stream >> _value.width >> _value.height;
151 if (_stream.fail())
152 _value.clear();
153 return _stream;
154 }
155 };
156
157 } // namespace types
158
159} // namespace MyGUI
160
161#endif // MYGUI_TSIZE_H_
TSize(TSize const &_obj)
Definition: MyGUI_TSize.h:35
friend std::istream & operator>>(std::istream &_stream, TSize< T > &_value)
Definition: MyGUI_TSize.h:148
friend std::ostream & operator<<(std::ostream &_stream, const TSize< T > &_value)
Definition: MyGUI_TSize.h:142
static TSize< T > parse(const std::string &_value)
Definition: MyGUI_TSize.h:120
TSize operator+(TSize const &_obj) const
Definition: MyGUI_TSize.h:60
TSize & operator=(TSize const &_obj)
Definition: MyGUI_TSize.h:65
bool empty() const
Definition: MyGUI_TSize.h:108
TSize operator-(TSize const &_obj) const
Definition: MyGUI_TSize.h:55
TSize & operator+=(TSize const &_obj)
Definition: MyGUI_TSize.h:48
TSize(T const &_width, T const &_height)
Definition: MyGUI_TSize.h:29
std::string print() const
Definition: MyGUI_TSize.h:113
bool operator!=(TSize const &_obj) const
Definition: MyGUI_TSize.h:85
TSize & operator-=(TSize const &_obj)
Definition: MyGUI_TSize.h:41
void set(T const &_width, T const &_height)
Definition: MyGUI_TSize.h:95
bool operator==(TSize const &_obj) const
Definition: MyGUI_TSize.h:80
void swap(TSize &_value)
Definition: MyGUI_TSize.h:101