summaryrefslogtreecommitdiffstats
path: root/ppapi/cpp/point.h
blob: ce1c5a40877c34bc0234306df0d936c99a67d6f6 (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
// Copyright (c) 2010 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_POINT_H_
#define PPAPI_CPP_POINT_H_

#include "ppapi/c/pp_point.h"

namespace pp {

// A point has an x and y coordinate.
class Point {
 public:
  Point() {
    point_.x = 0;
    point_.y = 0;
  }
  Point(int32_t in_x, int32_t in_y) {
    point_.x = in_x;
    point_.y = in_y;
  }
  Point(const PP_Point& point) {  // Implicit.
    point_.x = point.x;
    point_.y = point.y;
  }

  ~Point() {
  }

  operator PP_Point() const {
    return point_;
  }
  const PP_Point& pp_point() const {
    return point_;
  }
  PP_Point& pp_point() {
    return point_;
  }

  int32_t x() const { return point_.x; }
  void set_x(int32_t in_x) {
    point_.x = in_x;
  }

  int32_t y() const { return point_.y; }
  void set_y(int32_t in_y) {
    point_.y = in_y;
  }

  Point operator+(const Point& other) const {
    return Point(x() + other.x(), y() + other.y());
  }
  Point operator-(const Point& other) const {
    return Point(x() - other.x(), y() - other.y());
  }

  Point& operator+=(const Point& other) {
    point_.x += other.x();
    point_.y += other.y();
    return *this;
  }
  Point& operator-=(const Point& other) {
    point_.x -= other.x();
    point_.y -= other.y();
    return *this;
  }

  void swap(Point& other) {
    int32_t x = point_.x;
    int32_t y = point_.y;
    point_.x = other.point_.x;
    point_.y = other.point_.y;
    other.point_.x = x;
    other.point_.y = y;
  }

 private:
  PP_Point point_;
};

}  // namespace pp

inline bool operator==(const pp::Point& lhs, const pp::Point& rhs) {
  return lhs.x() == rhs.x() && lhs.y() == rhs.y();
}

inline bool operator!=(const pp::Point& lhs, const pp::Point& rhs) {
  return !(lhs == rhs);
}

#endif  // PPAPI_CPP_POINT_H_