summaryrefslogtreecommitdiffstats
path: root/ppapi/cpp/size.h
blob: e96b598b3f829d9c4a2ca8d800add59eb6b63536 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef PPAPI_CPP_SIZE_H_
#define PPAPI_CPP_SIZE_H_

#include "ppapi/c/pp_size.h"
#include "ppapi/cpp/logging.h"

/// @file
/// This file defines the API to create a size based on width
/// and height.

namespace pp {

/// A size of an object based on width and height.
class Size {
 public:

  /// The default constructor. Initializes the width and height to 0.
  Size() {
    size_.width = 0;
    size_.height = 0;
  }

  /// A constructor accepting a pointer to a <code>PP_Size</code> and
  /// converting the <code>PP_Size</code> to a <code>Size</code>. This is an
  /// implicit conversion constructor.
  ///
  /// @param[in] s A pointer to a <code>PP_Size</code>.
  Size(const PP_Size& s) {  // Implicit.
    // Want the >= 0 checking of the setter.
    set_width(s.width);
    set_height(s.height);
  }

  /// A constructor accepting two int values for width and height and
  /// converting them to a <code>Size</code>.
  ///
  /// @param[in] w An int value representing a width.
  /// @param[in] h An int value representing a height.
  Size(int w, int h) {
    // Want the >= 0 checking of the setter.
    set_width(w);
    set_height(h);
  }

  /// Destructor.
  ~Size() {
  }

  /// PP_Size() allows implicit conversion of a <code>Size</code> to a
  /// <code>PP_Size</code>.
  ///
  /// @return A Size.
  operator PP_Size() {
    return size_;
  }

  /// Getter function for returning the internal <code>PP_Size</code> struct.
  ///
  /// @return A const reference to the internal <code>PP_Size</code> struct.
  const PP_Size& pp_size() const {
    return size_;
  }

  /// Getter function for returning the internal <code>PP_Size</code> struct.
  ///
  /// @return A mutable reference to the <code>PP_Size</code> struct.
  PP_Size& pp_size() {
    return size_;
  }

  /// Getter function for returning the value of width.
  ///
  /// @return The value of width for this <code>Size</code>.
  int width() const {
    return size_.width;
  }

  /// Setter function for setting the value of width.
  ///
  /// @param[in] w A new width value.
  void set_width(int w) {
    if (w < 0) {
      PP_DCHECK(w >= 0);
      w = 0;
    }
    size_.width = w;
  }

  /// Getter function for returning the value of height.
  ///
  /// @return The value of height for this <code>Size</code>.
  int height() const {
    return size_.height;
  }

  /// Setter function for setting the value of height.
  ///
  /// @param[in] h A new height value.
  void set_height(int h) {
    if (h < 0) {
      PP_DCHECK(h >= 0);
      h = 0;
    }
    size_.height = h;
  }

  /// GetArea() determines the area (width * height).
  ///
  /// @return The area.
  int GetArea() const {
    return width() * height();
  }

  /// SetSize() sets the value of width and height.
  ///
  /// @param[in] w A new width value.
  /// @param[in] h A new height value.
  void SetSize(int w, int h) {
    set_width(w);
    set_height(h);
  }

  /// Enlarge() enlarges the size of an object.
  ///
  /// @param[in] w A width to add the current width.
  /// @param[in] h A height to add to the current height.
  void Enlarge(int w, int h) {
    set_width(width() + w);
    set_height(height() + h);
  }

  /// IsEmpty() determines if the size is zero.
  ///
  /// @return true if the size is zero.
  bool IsEmpty() const {
    // Size doesn't allow negative dimensions, so testing for 0 is enough.
    return (width() == 0) || (height() == 0);
  }

 private:
  PP_Size size_;
};

/// A size of an object based on width and height.
class FloatSize {
 public:

  /// The default constructor. Initializes the width and height to 0.0f.
  FloatSize() {
    size_.width = 0.0f;
    size_.height = 0.0f;
  }

  /// A constructor accepting a pointer to a <code>PP_FloatSize</code> and
  /// converting the <code>PP_FloatSize</code> to a <code>FloatSize</code>.
  /// This is an implicit conversion constructor.
  ///
  /// @param[in] s A pointer to a <code>PP_FloatSize</code>.
  FloatSize(const PP_FloatSize& s) {  // Implicit.
    // Want the >= 0 checking of the setter.
    set_width(s.width);
    set_height(s.height);
  }

  /// A constructor accepting two float values for width and height and
  /// converting them to a <code>FloatSize</code>.
  ///
  /// @param[in] w An float value representing a width.
  /// @param[in] h An float value representing a height.
  FloatSize(float w, float h) {
    // Want the >= 0.0f checking of the setter.
    set_width(w);
    set_height(h);
  }

  /// Destructor.
  ~FloatSize() {
  }

  /// PP_FloatSize() allows implicit conversion of a <code>FloatSize</code> to a
  /// <code>PP_FloatSize</code>.
  ///
  /// @return A Size.
  operator PP_FloatSize() {
    return size_;
  }

  /// Getter function for returning the internal <code>PP_FloatSize</code>
  /// struct.
  ///
  /// @return A const reference to the internal <code>PP_FloatSize</code>
  /// struct.
  const PP_FloatSize& pp_float_size() const {
    return size_;
  }

  /// Getter function for returning the internal <code>PP_FloatSize</code>
  /// struct.
  ///
  /// @return A mutable reference to the <code>PP_FloatSize</code> struct.
  PP_FloatSize& pp_float_size() {
    return size_;
  }

  /// Getter function for returning the value of width.
  ///
  /// @return The value of width for this <code>FloatSize</code>.
  float width() const {
    return size_.width;
  }

  /// Setter function for setting the value of width.
  ///
  /// @param[in] w A new width value.
  void set_width(float w) {
    if (w < 0.0f) {
      PP_DCHECK(w >= 0.0f);
      w = 0.0f;
    }
    size_.width = w;
  }

  /// Getter function for returning the value of height.
  ///
  /// @return The value of height for this <code>FloatSize</code>.
  float height() const {
    return size_.height;
  }

  /// Setter function for setting the value of height.
  ///
  /// @param[in] h A new height value.
  void set_height(float h) {
    if (h < 0.0f) {
      PP_DCHECK(h >= 0.0f);
      h = 0.0f;
    }
    size_.height = h;
  }

  /// GetArea() determines the area (width * height).
  ///
  /// @return The area.
  float GetArea() const {
    return width() * height();
  }

  /// SetSize() sets the value of width and height.
  ///
  /// @param[in] w A new width value.
  /// @param[in] h A new height value.
  void SetSize(float w, float h) {
    set_width(w);
    set_height(h);
  }

  /// Enlarge() enlarges the size of an object.
  ///
  /// @param[in] w A width to add the current width.
  /// @param[in] h A height to add to the current height.
  void Enlarge(float w, float h) {
    set_width(width() + w);
    set_height(height() + h);
  }

  /// IsEmpty() determines if the size is zero.
  ///
  /// @return true if the size is zero.
  bool IsEmpty() const {
    // Size doesn't allow negative dimensions, so testing for 0.0f is enough.
    return (width() == 0.0f) || (height() == 0.0f);
  }

 private:
  PP_FloatSize size_;
};

}  // namespace pp

/// This function determines whether the width and height values of two sizes
/// are equal.
///
/// @param[in] lhs The <code>Size</code> on the left-hand side of the equation.
/// @param[in] rhs The <code>Size</code> on the right-hand side of the
/// equation.
///
/// @return true if they are equal, false if unequal.
inline bool operator==(const pp::Size& lhs, const pp::Size& rhs) {
  return lhs.width() == rhs.width() && lhs.height() == rhs.height();
}

/// This function determines whether two <code>Sizes</code> are not equal.
///
/// @param[in] lhs The <code>Size</code> on the left-hand side of the equation.
/// @param[in] rhs The <code>Size</code> on the right-hand side of the equation.
///
/// @return true if the <code>Size</code> of lhs are equal to the
/// <code>Size</code> of rhs, otherwise false.
inline bool operator!=(const pp::Size& lhs, const pp::Size& rhs) {
  return !(lhs == rhs);
}

/// This function determines whether the width and height values of two sizes
/// are equal.
///
/// @param[in] lhs The <code>FloatSize</code> on the left-hand side of the
/// equation.
/// @param[in] rhs The <code>FloatSize</code> on the right-hand side of the
/// equation.
///
/// @return true if they are equal, false if unequal.
inline bool operator==(const pp::FloatSize& lhs, const pp::FloatSize& rhs) {
  return lhs.width() == rhs.width() && lhs.height() == rhs.height();
}

/// This function determines whether two <code>FloatSizes</code> are not equal.
///
/// @param[in] lhs The <code>FloatSize</code> on the left-hand side of the
/// equation.
/// @param[in] rhs The <code>FloatSize</code> on the right-hand side of the
/// equation.
///
/// @return true if the <code>FloatSize</code> of lhs are equal to the
/// <code>FloatSize</code> of rhs, otherwise false.
inline bool operator!=(const pp::FloatSize& lhs, const pp::FloatSize& rhs) {
  return !(lhs == rhs);
}

#endif  // PPAPI_CPP_SIZE_H_