MyGUI  3.2.0
MyGUI_TPoint.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_TPONT_H__
00023 #define __MYGUI_TPONT_H__
00024 
00025 #include "MyGUI_Prerequest.h"
00026 
00027 namespace MyGUI
00028 {
00029     namespace types
00030     {
00031 
00032         template<typename T>
00033         struct TPoint
00034         {
00035             T left;
00036             T top;
00037 
00038             TPoint() :
00039                 left(0),
00040                 top(0)
00041             {
00042             }
00043 
00044             TPoint(T const& _left, T const& _top) :
00045                 left(_left),
00046                 top(_top)
00047             {
00048             }
00049 
00050             TPoint(TPoint const& _obj) :
00051                 left(_obj.left),
00052                 top(_obj.top)
00053             {
00054             }
00055 
00056             TPoint& operator -= (TPoint const& _obj)
00057             {
00058                 left -= _obj.left;
00059                 top -= _obj.top;
00060                 return *this;
00061             }
00062 
00063             TPoint& operator += (TPoint const& _obj)
00064             {
00065                 left += _obj.left;
00066                 top += _obj.top;
00067                 return *this;
00068             }
00069 
00070             TPoint operator - (TPoint const& _obj) const
00071             {
00072                 return TPoint(left - _obj.left, top - _obj.top);
00073             }
00074 
00075             TPoint operator + (TPoint const& _obj) const
00076             {
00077                 return TPoint(left + _obj.left, top + _obj.top);
00078             }
00079 
00080             TPoint& operator = (TPoint const& _obj)
00081             {
00082                 left = _obj.left;
00083                 top = _obj.top;
00084                 return *this;
00085             }
00086 
00087             template<typename U>
00088             TPoint& operator = (TPoint<U> const& _obj)
00089             {
00090                 left = _obj.left;
00091                 top = _obj.top;
00092                 return *this;
00093             }
00094 
00095             bool operator == (TPoint const& _obj) const
00096             {
00097                 return ((left == _obj.left) && (top == _obj.top));
00098             }
00099 
00100             bool operator != (TPoint const& _obj) const
00101             {
00102                 return ! ((left == _obj.left) && (top == _obj.top));
00103             }
00104 
00105             void clear()
00106             {
00107                 left = top = 0;
00108             }
00109 
00110             void set(T const& _left, T const& _top)
00111             {
00112                 left = _left;
00113                 top = _top;
00114             }
00115 
00116             void swap(TPoint& _value)
00117             {
00118                 TPoint tmp = _value;
00119                 _value = *this;
00120                 *this = tmp;
00121             }
00122 
00123             bool empty() const
00124             {
00125                 return ((left == 0) && (top == 0));
00126             }
00127 
00128             std::string print() const
00129             {
00130                 std::ostringstream stream;
00131                 stream << *this;
00132                 return stream.str();
00133             }
00134 
00135             static TPoint<T> parse(const std::string& _value)
00136             {
00137                 TPoint<T> result;
00138                 std::istringstream stream(_value);
00139                 stream >> result.left >> result.top;
00140                 if (stream.fail())
00141                 {
00142                     return TPoint<T>();
00143                 }
00144                 else
00145                 {
00146                     int item = stream.get();
00147                     while (item != -1)
00148                     {
00149                         if (item != ' ' && item != '\t')
00150                             return TPoint<T>();
00151                         item = stream.get();
00152                     }
00153                 }
00154                 return result;
00155             }
00156 
00157             friend std::ostream& operator << (std::ostream& _stream, const TPoint<T>&  _value)
00158             {
00159                 _stream << _value.left << " " << _value.top;
00160                 return _stream;
00161             }
00162 
00163             friend std::istream& operator >> (std::istream& _stream, TPoint<T>&  _value)
00164             {
00165                 _stream >> _value.left >> _value.top;
00166                 if (_stream.fail())
00167                     _value.clear();
00168                 return _stream;
00169             }
00170         };
00171 
00172     } // namespace types
00173 
00174 } // namespace MyGUI
00175 
00176 #endif // __MYGUI_TPONT_H__