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.
5 #ifndef PPAPI_CPP_SIZE_H_
6 #define PPAPI_CPP_SIZE_H_
8 #include "ppapi/c/pp_size.h"
9 #include "ppapi/cpp/logging.h"
12 /// This file defines the API to create a size based on width
17 /// A size of an object based on width and height.
21 /// The default constructor. Initializes the width and height to 0.
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.
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.
38 /// A constructor accepting two int values for width and height and
39 /// converting them to a <code>Size</code>.
41 /// @param[in] w An int value representing a width.
42 /// @param[in] h An int value representing a height.
44 // Want the >= 0 checking of the setter.
53 /// PP_Size() allows implicit conversion of a <code>Size</code> to a
54 /// <code>PP_Size</code>.
61 /// Getter function for returning the internal <code>PP_Size</code> struct.
63 /// @return A const reference to the internal <code>PP_Size</code> struct.
64 const PP_Size
& pp_size() const {
68 /// Getter function for returning the internal <code>PP_Size</code> struct.
70 /// @return A mutable reference to the <code>PP_Size</code> struct.
75 /// Getter function for returning the value of width.
77 /// @return The value of width for this <code>Size</code>.
82 /// Setter function for setting the value of width.
84 /// @param[in] w A new width value.
85 void set_width(int w
) {
93 /// Getter function for returning the value of height.
95 /// @return The value of height for this <code>Size</code>.
100 /// Setter function for setting the value of height.
102 /// @param[in] h A new height value.
103 void set_height(int h
) {
111 /// GetArea() determines the area (width * height).
113 /// @return The area.
114 int GetArea() const {
115 return width() * height();
118 /// SetSize() sets the value of width and height.
120 /// @param[in] w A new width value.
121 /// @param[in] h A new height value.
122 void SetSize(int w
, int h
) {
127 /// Enlarge() enlarges the size of an object.
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
);
136 /// IsEmpty() determines if the size is zero.
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);
148 /// A size of an object based on width and height.
152 /// The default constructor. Initializes the width and height to 0.0f.
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.
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.
166 set_height(s
.height
);
169 /// A constructor accepting two float values for width and height and
170 /// converting them to a <code>FloatSize</code>.
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.
184 /// PP_FloatSize() allows implicit conversion of a <code>FloatSize</code> to a
185 /// <code>PP_FloatSize</code>.
188 operator PP_FloatSize() {
192 /// Getter function for returning the internal <code>PP_FloatSize</code>
195 /// @return A const reference to the internal <code>PP_FloatSize</code>
197 const PP_FloatSize
& pp_float_size() const {
201 /// Getter function for returning the internal <code>PP_FloatSize</code>
204 /// @return A mutable reference to the <code>PP_FloatSize</code> struct.
205 PP_FloatSize
& pp_float_size() {
209 /// Getter function for returning the value of width.
211 /// @return The value of width for this <code>FloatSize</code>.
212 float width() const {
216 /// Setter function for setting the value of width.
218 /// @param[in] w A new width value.
219 void set_width(float w
) {
221 PP_DCHECK(w
>= 0.0f
);
227 /// Getter function for returning the value of height.
229 /// @return The value of height for this <code>FloatSize</code>.
230 float height() const {
234 /// Setter function for setting the value of height.
236 /// @param[in] h A new height value.
237 void set_height(float h
) {
239 PP_DCHECK(h
>= 0.0f
);
245 /// GetArea() determines the area (width * height).
247 /// @return The area.
248 float GetArea() const {
249 return width() * height();
252 /// SetSize() sets the value of width and height.
254 /// @param[in] w A new width value.
255 /// @param[in] h A new height value.
256 void SetSize(float w
, float h
) {
261 /// Enlarge() enlarges the size of an object.
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
);
270 /// IsEmpty() determines if the size is zero.
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
);
284 /// This function determines whether the width and height values of two sizes
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
291 /// @return true if they are equal, false if unequal.
292 inline bool operator==(const pp::Size
& lhs
, const pp::Size
& rhs
) {
293 return lhs
.width() == rhs
.width() && lhs
.height() == rhs
.height();
296 /// This function determines whether two <code>Sizes</code> are not equal.
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.
301 /// @return true if the <code>Size</code> of lhs are equal to the
302 /// <code>Size</code> of rhs, otherwise false.
303 inline bool operator!=(const pp::Size
& lhs
, const pp::Size
& rhs
) {
304 return !(lhs
== rhs
);
307 /// This function determines whether the width and height values of two sizes
310 /// @param[in] lhs The <code>FloatSize</code> on the left-hand side of the
312 /// @param[in] rhs The <code>FloatSize</code> on the right-hand side of the
315 /// @return true if they are equal, false if unequal.
316 inline bool operator==(const pp::FloatSize
& lhs
, const pp::FloatSize
& rhs
) {
317 return lhs
.width() == rhs
.width() && lhs
.height() == rhs
.height();
320 /// This function determines whether two <code>FloatSizes</code> are not equal.
322 /// @param[in] lhs The <code>FloatSize</code> on the left-hand side of the
324 /// @param[in] rhs The <code>FloatSize</code> on the right-hand side of the
327 /// @return true if the <code>FloatSize</code> of lhs are equal to the
328 /// <code>FloatSize</code> of rhs, otherwise false.
329 inline bool operator!=(const pp::FloatSize
& lhs
, const pp::FloatSize
& rhs
) {
330 return !(lhs
== rhs
);
333 #endif // PPAPI_CPP_SIZE_H_