Size.h
1 /* Copyright (C) 2006-2015 Laurentiu-Gheorghe Crisan
2  Copyright (C) 2006-2015 Marc Boris Duerner
3  Copyright (C) 2010 Aloysius Indrayanto
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  As a special exception, you may use this file as part of a free
11  software library without restriction. Specifically, if other files
12  instantiate templates or use macros or inline functions from this
13  file, or you compile this file and link it with other files to
14  produce an executable, this file does not by itself cause the
15  resulting executable to be covered by the GNU General Public
16  License. This exception does not however invalidate any other
17  reasons why the executable file might be covered by the GNU Library
18  General Public License.
19 
20  This library is distributed in the hope that it will be useful,
21  but WITHOUT ANY WARRANTY; without even the implied warranty of
22  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23  Lesser General Public License for more details.
24 
25  You should have received a copy of the GNU Lesser General Public
26  License along with this library; if not, write to the Free Software
27  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
28  02110-1301 USA
29 */
30 
31 #ifndef PT_GFX_SIZE_H
32 #define PT_GFX_SIZE_H
33 
34 #include <Pt/Gfx/Api.h>
35 #include <Pt/Types.h>
36 #include <Pt/Math.h>
37 
38 namespace Pt {
39 
40 namespace Gfx {
41 
44 template<typename T>
45 class BasicSize {
46  public:
49  BasicSize(T w = 0, T h = 0)
50  : _w(w), _h(h)
51  {}
52 
53  void set(T w, T h)
54  {
55  _w = w;
56  _h = h;
57  }
58 
59  bool isNull() const
60  {
61  return _w == 0 || _h == 0;
62  }
63 
65  inline T width() const
66  { return _w; }
67 
69  inline T height() const
70  { return _h; }
71 
73  inline void setWidth(T w)
74  { _w = w; }
75 
77  inline void setHeight(T h)
78  { _h = h; }
79 
81  inline void setWidthHeight(T w, T h)
82  {
83  _w = w;
84  _h = h;
85  }
86 
88  const BasicSize& addWidth(T w)
89  {
90  _w += w;
91  return *this;
92  }
93 
95  const BasicSize& subWidth(T w)
96  {
97  _w -= w;
98  return *this;
99  }
100 
102  const BasicSize& addHeight(T h)
103  {
104  _h += h;
105  return *this;
106  }
107 
109  const BasicSize& subHeight(T h)
110  {
111  _h -= h;
112  return *this;
113  }
114 
115  void add(const BasicSize& s)
116  {
117  _w += s._w;
118  _h += s._h;
119  }
120 
121  BasicSize& operator=(const BasicSize& other)
122  {
123  _w = other._w; _h = other._h;
124  return *this;
125  }
126 
127  BasicSize& operator+=(const BasicSize& s)
128  {
129  _w += s._w;
130  _h += s._h;
131  return *this;
132  }
133 
134  bool operator==(const BasicSize& other) const
135  { return (_w == other._w && _h == other._h); }
136 
137  bool operator!=(const BasicSize& other) const
138  { return (_w != other._w || _h != other._h); }
139 
140  bool operator>(const BasicSize& other) const
141  {
142  if ( _w < other._w || _h < other._h)
143  return false;
144 
145  return ( (*this) != other );
146  }
147 
148  bool operator<(const BasicSize& other) const
149  {
150  if ( _w > other._w || _h > other._h )
151  return false;
152 
153  return ( other != (*this) );
154  }
155 
156  protected:
157  T _w;
158  T _h;
159 };
160 
161 typedef BasicSize<Pt::ssize_t> Size;
162 typedef BasicSize<double> SizeF;
163 
164 inline Size round(const SizeF& r)
165 {
166  return Size( lround(r.width()),
167  lround(r.height()) );
168 }
169 
170 } //namespace
171 
172 } //namespace
173 
174 #endif
T width() const
Returns the width.
Definition: Size.h:65
void setHeight(T h)
Sets the height.
Definition: Size.h:77
BasicSize(T w=0, T h=0)
Construct with width and height.
Definition: Size.h:49
const BasicSize & subWidth(T w)
Decrement the width of the BasicSize by the given value.
Definition: Size.h:95
Size with width and height.
Definition: Size.h:45
void setWidthHeight(T w, T h)
Sets the widht and height.
Definition: Size.h:81
Pt::int32_t lround(float x)
Rounds to nearest integer value.
Definition: Math.h:289
T height() const
Returns the height.
Definition: Size.h:69
void setWidth(T w)
Sets the width.
Definition: Size.h:73
const BasicSize & addWidth(T w)
Increment the width of the BasicSize by the given value.
Definition: Size.h:88
const BasicSize & addHeight(T h)
Increment the height of the BasicSize by the given value.
Definition: Size.h:102
const BasicSize & subHeight(T h)
Decrement the height of the BasicSize by the given value.
Definition: Size.h:109