Pepper_56_C++_interfaces
Pepper_56_C++_interfaces
 All Classes Namespaces Files Functions Typedefs Enumerations Macros Groups
size.h
Go to the documentation of this file.
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef PPAPI_CPP_SIZE_H_
6 #define PPAPI_CPP_SIZE_H_
7 
8 #include "ppapi/c/pp_size.h"
9 #include "ppapi/cpp/logging.h"
10 
14 
15 namespace pp {
16 
18 class Size {
19  public:
20 
22  Size() {
23  size_.width = 0;
24  size_.height = 0;
25  }
26 
32  Size(const PP_Size& s) { // Implicit.
33  // Want the >= 0 checking of the setter.
34  set_width(s.width);
35  set_height(s.height);
36  }
37 
43  Size(int w, int h) {
44  // Want the >= 0 checking of the setter.
45  set_width(w);
46  set_height(h);
47  }
48 
50  ~Size() {
51  }
52 
57  operator PP_Size() {
58  return size_;
59  }
60 
64  const PP_Size& pp_size() const {
65  return size_;
66  }
67 
71  PP_Size& pp_size() {
72  return size_;
73  }
74 
78  int width() const {
79  return size_.width;
80  }
81 
85  void set_width(int w) {
86  if (w < 0) {
87  PP_DCHECK(w >= 0);
88  w = 0;
89  }
90  size_.width = w;
91  }
92 
96  int height() const {
97  return size_.height;
98  }
99 
103  void set_height(int h) {
104  if (h < 0) {
105  PP_DCHECK(h >= 0);
106  h = 0;
107  }
108  size_.height = h;
109  }
110 
114  int GetArea() const {
115  return width() * height();
116  }
117 
122  void SetSize(int w, int h) {
123  set_width(w);
124  set_height(h);
125  }
126 
131  void Enlarge(int w, int h) {
132  set_width(width() + w);
133  set_height(height() + h);
134  }
135 
139  bool IsEmpty() const {
140  // Size doesn't allow negative dimensions, so testing for 0 is enough.
141  return (width() == 0) || (height() == 0);
142  }
143 
144  private:
145  PP_Size size_;
146 };
147 
149 class FloatSize {
150  public:
151 
154  size_.width = 0.0f;
155  size_.height = 0.0f;
156  }
157 
163  FloatSize(const PP_FloatSize& s) { // Implicit.
164  // Want the >= 0 checking of the setter.
165  set_width(s.width);
166  set_height(s.height);
167  }
168 
174  FloatSize(float w, float h) {
175  // Want the >= 0.0f checking of the setter.
176  set_width(w);
177  set_height(h);
178  }
179 
182  }
183 
188  operator PP_FloatSize() {
189  return size_;
190  }
191 
197  const PP_FloatSize& pp_float_size() const {
198  return size_;
199  }
200 
205  PP_FloatSize& pp_float_size() {
206  return size_;
207  }
208 
212  float width() const {
213  return size_.width;
214  }
215 
219  void set_width(float w) {
220  if (w < 0.0f) {
221  PP_DCHECK(w >= 0.0f);
222  w = 0.0f;
223  }
224  size_.width = w;
225  }
226 
230  float height() const {
231  return size_.height;
232  }
233 
237  void set_height(float h) {
238  if (h < 0.0f) {
239  PP_DCHECK(h >= 0.0f);
240  h = 0.0f;
241  }
242  size_.height = h;
243  }
244 
248  float GetArea() const {
249  return width() * height();
250  }
251 
256  void SetSize(float w, float h) {
257  set_width(w);
258  set_height(h);
259  }
260 
265  void Enlarge(float w, float h) {
266  set_width(width() + w);
267  set_height(height() + h);
268  }
269 
273  bool IsEmpty() const {
274  // Size doesn't allow negative dimensions, so testing for 0.0f is enough.
275  return (width() == 0.0f) || (height() == 0.0f);
276  }
277 
278  private:
279  PP_FloatSize size_;
280 };
281 
282 } // namespace pp
283 
292 inline bool operator==(const pp::Size& lhs, const pp::Size& rhs) {
293  return lhs.width() == rhs.width() && lhs.height() == rhs.height();
294 }
295 
303 inline bool operator!=(const pp::Size& lhs, const pp::Size& rhs) {
304  return !(lhs == rhs);
305 }
306 
316 inline bool operator==(const pp::FloatSize& lhs, const pp::FloatSize& rhs) {
317  return lhs.width() == rhs.width() && lhs.height() == rhs.height();
318 }
319 
329 inline bool operator!=(const pp::FloatSize& lhs, const pp::FloatSize& rhs) {
330  return !(lhs == rhs);
331 }
332 
333 #endif // PPAPI_CPP_SIZE_H_
334 
bool IsEmpty() const
Definition: size.h:273
void SetSize(float w, float h)
Definition: size.h:256
FloatSize(float w, float h)
Definition: size.h:174
Size()
The default constructor. Initializes the width and height to 0.
Definition: size.h:22
~Size()
Destructor.
Definition: size.h:50
A size of an object based on width and height.
Definition: size.h:149
#define PP_DCHECK(a)
Definition: logging.h:16
const PP_Size & pp_size() const
Definition: size.h:64
FloatSize(const PP_FloatSize &s)
Definition: size.h:163
PP_FloatSize & pp_float_size()
Definition: size.h:205
PP_Size & pp_size()
Definition: size.h:71
void set_width(float w)
Definition: size.h:219
const PP_FloatSize & pp_float_size() const
Definition: size.h:197
~FloatSize()
Destructor.
Definition: size.h:181
int height() const
Definition: size.h:96
void SetSize(int w, int h)
Definition: size.h:122
Size(const PP_Size &s)
Definition: size.h:32
A size of an object based on width and height.
Definition: size.h:18
int GetArea() const
Definition: size.h:114
float GetArea() const
Definition: size.h:248
int width() const
Definition: size.h:78
void set_width(int w)
Definition: size.h:85
void set_height(float h)
Definition: size.h:237
FloatSize()
The default constructor. Initializes the width and height to 0.0f.
Definition: size.h:153
bool IsEmpty() const
Definition: size.h:139
bool operator!=(const pp::Size &lhs, const pp::Size &rhs)
Definition: size.h:303
bool operator==(const pp::Size &lhs, const pp::Size &rhs)
Definition: size.h:292
float width() const
Definition: size.h:212
float height() const
Definition: size.h:230
void set_height(int h)
Definition: size.h:103
void Enlarge(float w, float h)
Definition: size.h:265
void Enlarge(int w, int h)
Definition: size.h:131
Size(int w, int h)
Definition: size.h:43