MyGUI  3.2.0
MyGUI_TSize.h
Go to the documentation of this file.
1 
6 /*
7  This file is part of MyGUI.
8 
9  MyGUI is free software: you can redistribute it and/or modify
10  it under the terms of the GNU Lesser General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  (at your option) any later version.
13 
14  MyGUI is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public License
20  along with MyGUI. If not, see <http://www.gnu.org/licenses/>.
21 */
22 #ifndef __MYGUI_TSIZE_H__
23 #define __MYGUI_TSIZE_H__
24 
25 #include "MyGUI_Prerequest.h"
26 
27 namespace MyGUI
28 {
29  namespace types
30  {
31 
32  template<typename T>
33  struct TSize
34  {
35  T width;
36  T height;
37 
38  TSize() :
39  width(0),
40  height(0)
41  {
42  }
43 
44  TSize(T const& _width, T const& _height) :
45  width(_width),
46  height(_height)
47  {
48  }
49 
50  TSize(TSize const& _obj) :
51  width(_obj.width),
52  height(_obj.height)
53  {
54  }
55 
56  TSize& operator -= (TSize const& _obj)
57  {
58  width -= _obj.width;
59  height -= _obj.height;
60  return *this;
61  }
62 
63  TSize& operator += (TSize const& _obj)
64  {
65  width += _obj.width;
66  height += _obj.height;
67  return *this;
68  }
69 
70  TSize operator - (TSize const& _obj) const
71  {
72  return TSize(width - _obj.width, height - _obj.height);
73  }
74 
75  TSize operator + (TSize const& _obj) const
76  {
77  return TSize(width + _obj.width, height + _obj.height);
78  }
79 
80  TSize& operator = (TSize const& _obj)
81  {
82  width = _obj.width;
83  height = _obj.height;
84  return *this;
85  }
86 
87  template<typename U>
88  TSize& operator = (TSize<U> const& _obj)
89  {
90  width = _obj.width;
91  height = _obj.height;
92  return *this;
93  }
94 
95  bool operator == (TSize const& _obj) const
96  {
97  return ((width == _obj.width) && (height == _obj.height));
98  }
99 
100  bool operator != (TSize const& _obj) const
101  {
102  return !((width == _obj.width) && (height == _obj.height));
103  }
104 
105  void clear()
106  {
107  width = height = 0;
108  }
109 
110  void set(T const& _width, T const& _height)
111  {
112  width = _width;
113  height = _height;
114  }
115 
116  void swap(TSize& _value)
117  {
118  TSize tmp = _value;
119  _value = *this;
120  *this = tmp;
121  }
122 
123  bool empty() const
124  {
125  return ((width == 0) && (height == 0));
126  }
127 
128  std::string print() const
129  {
130  std::ostringstream stream;
131  stream << *this;
132  return stream.str();
133  }
134 
135  static TSize<T> parse(const std::string& _value)
136  {
137  TSize<T> result;
138  std::istringstream stream(_value);
139  stream >> result.width >> result.height;
140  if (stream.fail())
141  {
142  return TSize<T>();
143  }
144  else
145  {
146  int item = stream.get();
147  while (item != -1)
148  {
149  if (item != ' ' && item != '\t')
150  return TSize<T>();
151  item = stream.get();
152  }
153  }
154  return result;
155  }
156 
157  friend std::ostream& operator << (std::ostream& _stream, const TSize<T>& _value)
158  {
159  _stream << _value.width << " " << _value.height;
160  return _stream;
161  }
162 
163  friend std::istream& operator >> (std::istream& _stream, TSize<T>& _value)
164  {
165  _stream >> _value.width >> _value.height;
166  if (_stream.fail())
167  _value.clear();
168  return _stream;
169  }
170  };
171 
172  } // namespace types
173 
174 } // namespace MyGUI
175 
176 #endif // __MYGUI_TSIZE_H__
bool empty() const
Definition: MyGUI_TSize.h:123
bool operator==(TSize const &_obj) const
Definition: MyGUI_TSize.h:95
TSize & operator-=(TSize const &_obj)
Definition: MyGUI_TSize.h:56
void set(T const &_width, T const &_height)
Definition: MyGUI_TSize.h:110
TSize(TSize const &_obj)
Definition: MyGUI_TSize.h:50
static TSize< T > parse(const std::string &_value)
Definition: MyGUI_TSize.h:135
bool operator!=(TSize const &_obj) const
Definition: MyGUI_TSize.h:100
TSize & operator+=(TSize const &_obj)
Definition: MyGUI_TSize.h:63
TSize operator-(TSize const &_obj) const
Definition: MyGUI_TSize.h:70
std::string print() const
Definition: MyGUI_TSize.h:128
TSize(T const &_width, T const &_height)
Definition: MyGUI_TSize.h:44
TSize & operator=(TSize const &_obj)
Definition: MyGUI_TSize.h:80
TSize operator+(TSize const &_obj) const
Definition: MyGUI_TSize.h:75
void swap(TSize &_value)
Definition: MyGUI_TSize.h:116
friend std::istream & operator>>(std::istream &_stream, TSize< T > &_value)
Definition: MyGUI_TSize.h:163