Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ppapi / cpp / size.h
blobe96b598b3f829d9c4a2ca8d800add59eb6b63536
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"
11 /// @file
12 /// This file defines the API to create a size based on width
13 /// and height.
15 namespace pp {
17 /// A size of an object based on width and height.
18 class Size {
19 public:
21 /// The default constructor. Initializes the width and height to 0.
22 Size() {
23 size_.width = 0;
24 size_.height = 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.
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);
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);
49 /// Destructor.
50 ~Size() {
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_;
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_;
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_;
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;
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;
90 size_.width = w;
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;
100 /// Setter function for setting the value of height.
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;
108 size_.height = 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) {
123 set_width(w);
124 set_height(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);
144 private:
145 PP_Size size_;
148 /// A size of an object based on width and height.
149 class FloatSize {
150 public:
152 /// The default constructor. Initializes the width and height to 0.0f.
153 FloatSize() {
154 size_.width = 0.0f;
155 size_.height = 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.
165 set_width(s.width);
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.
176 set_width(w);
177 set_height(h);
180 /// Destructor.
181 ~FloatSize() {
184 /// PP_FloatSize() allows implicit conversion of a <code>FloatSize</code> to a
185 /// <code>PP_FloatSize</code>.
187 /// @return A Size.
188 operator PP_FloatSize() {
189 return size_;
192 /// Getter function for returning the internal <code>PP_FloatSize</code>
193 /// struct.
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_;
201 /// Getter function for returning the internal <code>PP_FloatSize</code>
202 /// struct.
204 /// @return A mutable reference to the <code>PP_FloatSize</code> struct.
205 PP_FloatSize& pp_float_size() {
206 return 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 {
213 return size_.width;
216 /// Setter function for setting the value of width.
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;
224 size_.width = w;
227 /// Getter function for returning the value of height.
229 /// @return The value of height for this <code>FloatSize</code>.
230 float height() const {
231 return size_.height;
234 /// Setter function for setting the value of height.
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;
242 size_.height = h;
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) {
257 set_width(w);
258 set_height(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);
278 private:
279 PP_FloatSize size_;
282 } // namespace pp
284 /// This function determines whether the width and height values of two sizes
285 /// are equal.
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.
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
308 /// are equal.
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.
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
323 /// equation.
324 /// @param[in] rhs The <code>FloatSize</code> on the right-hand side of the
325 /// equation.
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_