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