MyGUI  3.2.0
MyGUI_TSize.h
Go to the documentation of this file.
00001 
00006 /*
00007     This file is part of MyGUI.
00008 
00009     MyGUI is free software: you can redistribute it and/or modify
00010     it under the terms of the GNU Lesser General Public License as published by
00011     the Free Software Foundation, either version 3 of the License, or
00012     (at your option) any later version.
00013 
00014     MyGUI is distributed in the hope that it will be useful,
00015     but WITHOUT ANY WARRANTY; without even the implied warranty of
00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017     GNU Lesser General Public License for more details.
00018 
00019     You should have received a copy of the GNU Lesser General Public License
00020     along with MyGUI.  If not, see <http://www.gnu.org/licenses/>.
00021 */
00022 #ifndef __MYGUI_TSIZE_H__
00023 #define __MYGUI_TSIZE_H__
00024 
00025 #include "MyGUI_Prerequest.h"
00026 
00027 namespace MyGUI
00028 {
00029     namespace types
00030     {
00031 
00032         template<typename T>
00033         struct TSize
00034         {
00035             T width;
00036             T height;
00037 
00038             TSize() :
00039                 width(0),
00040                 height(0)
00041             {
00042             }
00043 
00044             TSize(T const& _width, T const& _height) :
00045                 width(_width),
00046                 height(_height)
00047             {
00048             }
00049 
00050             TSize(TSize const& _obj) :
00051                 width(_obj.width),
00052                 height(_obj.height)
00053             {
00054             }
00055 
00056             TSize& operator -= (TSize const& _obj)
00057             {
00058                 width -= _obj.width;
00059                 height -= _obj.height;
00060                 return *this;
00061             }
00062 
00063             TSize& operator += (TSize const& _obj)
00064             {
00065                 width += _obj.width;
00066                 height += _obj.height;
00067                 return *this;
00068             }
00069 
00070             TSize operator - (TSize const& _obj) const
00071             {
00072                 return TSize(width - _obj.width, height - _obj.height);
00073             }
00074 
00075             TSize operator + (TSize const& _obj) const
00076             {
00077                 return TSize(width + _obj.width, height + _obj.height);
00078             }
00079 
00080             TSize& operator = (TSize const& _obj)
00081             {
00082                 width = _obj.width;
00083                 height = _obj.height;
00084                 return *this;
00085             }
00086 
00087             template<typename U>
00088             TSize& operator = (TSize<U> const& _obj)
00089             {
00090                 width = _obj.width;
00091                 height = _obj.height;
00092                 return *this;
00093             }
00094 
00095             bool operator == (TSize const& _obj) const
00096             {
00097                 return ((width == _obj.width) && (height == _obj.height));
00098             }
00099 
00100             bool operator != (TSize const& _obj) const
00101             {
00102                 return !((width == _obj.width) && (height == _obj.height));
00103             }
00104 
00105             void clear()
00106             {
00107                 width = height = 0;
00108             }
00109 
00110             void set(T const& _width, T const& _height)
00111             {
00112                 width = _width;
00113                 height = _height;
00114             }
00115 
00116             void swap(TSize& _value)
00117             {
00118                 TSize tmp = _value;
00119                 _value = *this;
00120                 *this = tmp;
00121             }
00122 
00123             bool empty() const
00124             {
00125                 return ((width == 0) && (height == 0));
00126             }
00127 
00128             std::string print() const
00129             {
00130                 std::ostringstream stream;
00131                 stream << *this;
00132                 return stream.str();
00133             }
00134 
00135             static TSize<T> parse(const std::string& _value)
00136             {
00137                 TSize<T> result;
00138                 std::istringstream stream(_value);
00139                 stream >> result.width >> result.height;
00140                 if (stream.fail())
00141                 {
00142                     return TSize<T>();
00143                 }
00144                 else
00145                 {
00146                     int item = stream.get();
00147                     while (item != -1)
00148                     {
00149                         if (item != ' ' && item != '\t')
00150                             return TSize<T>();
00151                         item = stream.get();
00152                     }
00153                 }
00154                 return result;
00155             }
00156 
00157             friend std::ostream& operator << (std::ostream& _stream, const TSize<T>&  _value)
00158             {
00159                 _stream << _value.width << " " << _value.height;
00160                 return _stream;
00161             }
00162 
00163             friend std::istream& operator >> (std::istream& _stream, TSize<T>&  _value)
00164             {
00165                 _stream >> _value.width >> _value.height;
00166                 if (_stream.fail())
00167                     _value.clear();
00168                 return _stream;
00169             }
00170         };
00171 
00172     } // namespace types
00173 
00174 } // namespace MyGUI
00175 
00176 #endif // __MYGUI_TSIZE_H__