MyGUI
3.2.1
|
00001 /* 00002 * This source file is part of MyGUI. For the latest info, see http://mygui.info/ 00003 * Distributed under the MIT License 00004 * (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT) 00005 */ 00006 00007 #ifndef __MYGUI_TSIZE_H__ 00008 #define __MYGUI_TSIZE_H__ 00009 00010 #include "MyGUI_Prerequest.h" 00011 00012 namespace MyGUI 00013 { 00014 namespace types 00015 { 00016 00017 template<typename T> 00018 struct TSize 00019 { 00020 T width; 00021 T height; 00022 00023 TSize() : 00024 width(0), 00025 height(0) 00026 { 00027 } 00028 00029 TSize(T const& _width, T const& _height) : 00030 width(_width), 00031 height(_height) 00032 { 00033 } 00034 00035 TSize(TSize const& _obj) : 00036 width(_obj.width), 00037 height(_obj.height) 00038 { 00039 } 00040 00041 TSize& operator -= (TSize const& _obj) 00042 { 00043 width -= _obj.width; 00044 height -= _obj.height; 00045 return *this; 00046 } 00047 00048 TSize& operator += (TSize const& _obj) 00049 { 00050 width += _obj.width; 00051 height += _obj.height; 00052 return *this; 00053 } 00054 00055 TSize operator - (TSize const& _obj) const 00056 { 00057 return TSize(width - _obj.width, height - _obj.height); 00058 } 00059 00060 TSize operator + (TSize const& _obj) const 00061 { 00062 return TSize(width + _obj.width, height + _obj.height); 00063 } 00064 00065 TSize& operator = (TSize const& _obj) 00066 { 00067 width = _obj.width; 00068 height = _obj.height; 00069 return *this; 00070 } 00071 00072 template<typename U> 00073 TSize& operator = (TSize<U> const& _obj) 00074 { 00075 width = _obj.width; 00076 height = _obj.height; 00077 return *this; 00078 } 00079 00080 bool operator == (TSize const& _obj) const 00081 { 00082 return ((width == _obj.width) && (height == _obj.height)); 00083 } 00084 00085 bool operator != (TSize const& _obj) const 00086 { 00087 return !((width == _obj.width) && (height == _obj.height)); 00088 } 00089 00090 void clear() 00091 { 00092 width = height = 0; 00093 } 00094 00095 void set(T const& _width, T const& _height) 00096 { 00097 width = _width; 00098 height = _height; 00099 } 00100 00101 void swap(TSize& _value) 00102 { 00103 TSize tmp = _value; 00104 _value = *this; 00105 *this = tmp; 00106 } 00107 00108 bool empty() const 00109 { 00110 return ((width == 0) && (height == 0)); 00111 } 00112 00113 std::string print() const 00114 { 00115 std::ostringstream stream; 00116 stream << *this; 00117 return stream.str(); 00118 } 00119 00120 static TSize<T> parse(const std::string& _value) 00121 { 00122 TSize<T> result; 00123 std::istringstream stream(_value); 00124 stream >> result.width >> result.height; 00125 if (stream.fail()) 00126 { 00127 return TSize<T>(); 00128 } 00129 else 00130 { 00131 int item = stream.get(); 00132 while (item != -1) 00133 { 00134 if (item != ' ' && item != '\t') 00135 return TSize<T>(); 00136 item = stream.get(); 00137 } 00138 } 00139 return result; 00140 } 00141 00142 friend std::ostream& operator << (std::ostream& _stream, const TSize<T>& _value) 00143 { 00144 _stream << _value.width << " " << _value.height; 00145 return _stream; 00146 } 00147 00148 friend std::istream& operator >> (std::istream& _stream, TSize<T>& _value) 00149 { 00150 _stream >> _value.width >> _value.height; 00151 if (_stream.fail()) 00152 _value.clear(); 00153 return _stream; 00154 } 00155 }; 00156 00157 } // namespace types 00158 00159 } // namespace MyGUI 00160 00161 #endif // __MYGUI_TSIZE_H__