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