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
11/// @file
12/// This file defines the API to create a size based on width
13/// and height.
14
15namespace pp {
16
17/// A size of an object based on width and height.
18class Size {
19 public:
20
21  /// The default constructor. Initializes the width and height to 0.
22  Size() {
23    size_.width = 0;
24    size_.height = 0;
25  }
26
27  /// A constructor accepting a pointer to a <code>PP_Size</code> and
28  /// converting the <code>PP_Size</code> to a <code>Size</code>. This is an
29  /// implicit conversion constructor.
30  ///
31  /// @param[in] s A pointer to a <code>PP_Size</code>.
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
38  /// A constructor accepting two int values for width and height and
39  /// converting them to a <code>Size</code>.
40  ///
41  /// @param[in] w An int value representing a width.
42  /// @param[in] h An int value representing a height.
43  Size(int w, int h) {
44    // Want the >= 0 checking of the setter.
45    set_width(w);
46    set_height(h);
47  }
48
49  /// Destructor.
50  ~Size() {
51  }
52
53  /// PP_Size() allows implicit conversion of a <code>Size</code> to a
54  /// <code>PP_Size</code>.
55  ///
56  /// @return A Size.
57  operator PP_Size() {
58    return size_;
59  }
60
61  /// Getter function for returning the internal <code>PP_Size</code> struct.
62  ///
63  /// @return A const reference to the internal <code>PP_Size</code> struct.
64  const PP_Size& pp_size() const {
65    return size_;
66  }
67
68  /// Getter function for returning the internal <code>PP_Size</code> struct.
69  ///
70  /// @return A mutable reference to the <code>PP_Size</code> struct.
71  PP_Size& pp_size() {
72    return size_;
73  }
74
75  /// Getter function for returning the value of width.
76  ///
77  /// @return The value of width for this <code>Size</code>.
78  int width() const {
79    return size_.width;
80  }
81
82  /// Setter function for setting the value of width.
83  ///
84  /// @param[in] w A new width value.
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
93  /// Getter function for returning the value of height.
94  ///
95  /// @return The value of height for this <code>Size</code>.
96  int height() const {
97    return size_.height;
98  }
99
100  /// Setter function for setting the value of height.
101  ///
102  /// @param[in] h A new height value.
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
111  /// GetArea() determines the area (width * height).
112  ///
113  /// @return The area.
114  int GetArea() const {
115    return width() * height();
116  }
117
118  /// SetSize() sets the value of width and height.
119  ///
120  /// @param[in] w A new width value.
121  /// @param[in] h A new height value.
122  void SetSize(int w, int h) {
123    set_width(w);
124    set_height(h);
125  }
126
127  /// Enlarge() enlarges the size of an object.
128  ///
129  /// @param[in] w A width to add the current width.
130  /// @param[in] h A height to add to the current height.
131  void Enlarge(int w, int h) {
132    set_width(width() + w);
133    set_height(height() + h);
134  }
135
136  /// IsEmpty() determines if the size is zero.
137  ///
138  /// @return true if the size is zero.
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
148/// A size of an object based on width and height.
149class FloatSize {
150 public:
151
152  /// The default constructor. Initializes the width and height to 0.0f.
153  FloatSize() {
154    size_.width = 0.0f;
155    size_.height = 0.0f;
156  }
157
158  /// A constructor accepting a pointer to a <code>PP_FloatSize</code> and
159  /// converting the <code>PP_FloatSize</code> to a <code>FloatSize</code>.
160  /// This is an implicit conversion constructor.
161  ///
162  /// @param[in] s A pointer to a <code>PP_FloatSize</code>.
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
169  /// A constructor accepting two float values for width and height and
170  /// converting them to a <code>FloatSize</code>.
171  ///
172  /// @param[in] w An float value representing a width.
173  /// @param[in] h An float value representing a height.
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
180  /// Destructor.
181  ~FloatSize() {
182  }
183
184  /// PP_FloatSize() allows implicit conversion of a <code>FloatSize</code> to a
185  /// <code>PP_FloatSize</code>.
186  ///
187  /// @return A Size.
188  operator PP_FloatSize() {
189    return size_;
190  }
191
192  /// Getter function for returning the internal <code>PP_FloatSize</code>
193  /// struct.
194  ///
195  /// @return A const reference to the internal <code>PP_FloatSize</code>
196  /// struct.
197  const PP_FloatSize& pp_float_size() const {
198    return size_;
199  }
200
201  /// Getter function for returning the internal <code>PP_FloatSize</code>
202  /// struct.
203  ///
204  /// @return A mutable reference to the <code>PP_FloatSize</code> struct.
205  PP_FloatSize& pp_float_size() {
206    return size_;
207  }
208
209  /// Getter function for returning the value of width.
210  ///
211  /// @return The value of width for this <code>FloatSize</code>.
212  float width() const {
213    return size_.width;
214  }
215
216  /// Setter function for setting the value of width.
217  ///
218  /// @param[in] w A new width value.
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
227  /// Getter function for returning the value of height.
228  ///
229  /// @return The value of height for this <code>FloatSize</code>.
230  float height() const {
231    return size_.height;
232  }
233
234  /// Setter function for setting the value of height.
235  ///
236  /// @param[in] h A new height value.
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
245  /// GetArea() determines the area (width * height).
246  ///
247  /// @return The area.
248  float GetArea() const {
249    return width() * height();
250  }
251
252  /// SetSize() sets the value of width and height.
253  ///
254  /// @param[in] w A new width value.
255  /// @param[in] h A new height value.
256  void SetSize(float w, float h) {
257    set_width(w);
258    set_height(h);
259  }
260
261  /// Enlarge() enlarges the size of an object.
262  ///
263  /// @param[in] w A width to add the current width.
264  /// @param[in] h A height to add to the current height.
265  void Enlarge(float w, float h) {
266    set_width(width() + w);
267    set_height(height() + h);
268  }
269
270  /// IsEmpty() determines if the size is zero.
271  ///
272  /// @return true if the size is zero.
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
284/// This function determines whether the width and height values of two sizes
285/// are equal.
286///
287/// @param[in] lhs The <code>Size</code> on the left-hand side of the equation.
288/// @param[in] rhs The <code>Size</code> on the right-hand side of the
289/// equation.
290///
291/// @return true if they are equal, false if unequal.
292inline bool operator==(const pp::Size& lhs, const pp::Size& rhs) {
293  return lhs.width() == rhs.width() && lhs.height() == rhs.height();
294}
295
296/// This function determines whether two <code>Sizes</code> are not equal.
297///
298/// @param[in] lhs The <code>Size</code> on the left-hand side of the equation.
299/// @param[in] rhs The <code>Size</code> on the right-hand side of the equation.
300///
301/// @return true if the <code>Size</code> of lhs are equal to the
302/// <code>Size</code> of rhs, otherwise false.
303inline bool operator!=(const pp::Size& lhs, const pp::Size& rhs) {
304  return !(lhs == rhs);
305}
306
307/// This function determines whether the width and height values of two sizes
308/// are equal.
309///
310/// @param[in] lhs The <code>FloatSize</code> on the left-hand side of the
311/// equation.
312/// @param[in] rhs The <code>FloatSize</code> on the right-hand side of the
313/// equation.
314///
315/// @return true if they are equal, false if unequal.
316inline bool operator==(const pp::FloatSize& lhs, const pp::FloatSize& rhs) {
317  return lhs.width() == rhs.width() && lhs.height() == rhs.height();
318}
319
320/// This function determines whether two <code>FloatSizes</code> are not equal.
321///
322/// @param[in] lhs The <code>FloatSize</code> on the left-hand side of the
323/// equation.
324/// @param[in] rhs The <code>FloatSize</code> on the right-hand side of the
325/// equation.
326///
327/// @return true if the <code>FloatSize</code> of lhs are equal to the
328/// <code>FloatSize</code> of rhs, otherwise false.
329inline bool operator!=(const pp::FloatSize& lhs, const pp::FloatSize& rhs) {
330  return !(lhs == rhs);
331}
332
333#endif  // PPAPI_CPP_SIZE_H_
334
335