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 15 namespace pp { 16 17 /// A size of an object based on width and height. 18 class 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 } // namespace pp 149 150 /// This function determines whether the width and height values of two sizes 151 /// are equal. 152 /// 153 /// @param[in] lhs The <code>Size</code> on the left-hand side of the equation. 154 /// @param[in] rhs The <code>Size</code> on the right-hand side of the 155 /// equation. 156 /// 157 /// @return true if they are equal, false if unequal. 158 inline bool operator==(const pp::Size& lhs, const pp::Size& rhs) { 159 return lhs.width() == rhs.width() && lhs.height() == rhs.height(); 160 } 161 162 /// This function determines whether two <code>Sizes</code> are not equal. 163 /// 164 /// @param[in] lhs The <code>Size</code> on the left-hand side of the equation. 165 /// @param[in] rhs The <code>Size</code> on the right-hand side of the equation. 166 /// 167 /// @return true if the <code>Size</code> of lhs are equal to the 168 /// <code>Size</code> of rhs, otherwise false. 169 inline bool operator!=(const pp::Size& lhs, const pp::Size& rhs) { 170 return !(lhs == rhs); 171 } 172 173 #endif // PPAPI_CPP_SIZE_H_ 174 175