diff options
author | Mathias Agopian <mathias@google.com> | 2009-05-26 17:44:57 -0700 |
---|---|---|
committer | Mathias Agopian <mathias@google.com> | 2009-05-26 17:47:39 -0700 |
commit | f1472a79728324f4f6b5d66af986e9e9db24d0aa (patch) | |
tree | a2564a0aa4ee3ebb7ff4980ca86dbac09ecbec7e /include | |
parent | c29df57e5abb09c7e1ceed153e8f2b906f522d7b (diff) | |
download | frameworks_base-f1472a79728324f4f6b5d66af986e9e9db24d0aa.zip frameworks_base-f1472a79728324f4f6b5d66af986e9e9db24d0aa.tar.gz frameworks_base-f1472a79728324f4f6b5d66af986e9e9db24d0aa.tar.bz2 |
minor clean-up to Rect and Point.
- return "const" objects for overloaded operators to disallow constructs like: (a+b) = c;
- don't return references to non-static members, it's not always safe.
- Point.cpp was empty, so get rid of it
Diffstat (limited to 'include')
-rw-r--r-- | include/ui/Point.h | 21 | ||||
-rw-r--r-- | include/ui/Rect.h | 40 |
2 files changed, 27 insertions, 34 deletions
diff --git a/include/ui/Point.h b/include/ui/Point.h index dbbad1e..1653120 100644 --- a/include/ui/Point.h +++ b/include/ui/Point.h @@ -31,12 +31,9 @@ public: // because we want the compiler generated versions // Default constructor doesn't initialize the Point - inline Point() - { + inline Point() { } - - inline Point(int _x, int _y) : x(_x), y(_y) - { + inline Point(int x, int y) : x(x), y(y) { } inline bool operator == (const Point& rhs) const { @@ -57,8 +54,8 @@ public: } inline Point& operator - () { - x=-x; - y=-y; + x = -x; + y = -y; return *this; } @@ -73,11 +70,13 @@ public: return *this; } - Point operator + (const Point& rhs) const { - return Point(x+rhs.x, y+rhs.y); + const Point operator + (const Point& rhs) const { + const Point result(x+rhs.x, y+rhs.y); + return result; } - Point operator - (const Point& rhs) const { - return Point(x-rhs.x, y-rhs.y); + const Point operator - (const Point& rhs) const { + const Point result(x-rhs.x, y-rhs.y); + return result; } }; diff --git a/include/ui/Rect.h b/include/ui/Rect.h index d232847..da72944 100644 --- a/include/ui/Rect.h +++ b/include/ui/Rect.h @@ -33,23 +33,16 @@ public: // we don't provide copy-ctor and operator= on purpose // because we want the compiler generated versions - inline Rect() - { + inline Rect() { } - inline Rect(int w, int h) - : left(0), top(0), right(w), bottom(h) - { + : left(0), top(0), right(w), bottom(h) { } - inline Rect(int l, int t, int r, int b) - : left(l), top(t), right(r), bottom(b) - { + : left(l), top(t), right(r), bottom(b) { } - inline Rect(const Point& lt, const Point& rb) - : left(lt.x), top(lt.y), right(rb.x), bottom(rb.y) - { + : left(lt.x), top(lt.y), right(rb.x), bottom(rb.y) { } void makeInvalid(); @@ -78,21 +71,22 @@ public: return bottom-top; } - // returns left-top Point non-const reference, can be assigned - inline Point& leftTop() { - return reinterpret_cast<Point&>(left); + void setLeftTop(const Point& lt) { + left = lt.x; + top = lt.y; } - // returns right bottom non-const reference, can be assigned - inline Point& rightBottom() { - return reinterpret_cast<Point&>(right); + + void setRightBottom(const Point& rb) { + right = rb.x; + bottom = rb.y; } // the following 4 functions return the 4 corners of the rect as Point - inline const Point& leftTop() const { - return reinterpret_cast<const Point&>(left); + Point leftTop() const { + return Point(left, top); } - inline const Point& rightBottom() const { - return reinterpret_cast<const Point&>(right); + Point rightBottom() const { + return Point(right, bottom); } Point rightTop() const { return Point(right, top); @@ -133,8 +127,8 @@ public: Rect& operator -= (const Point& rhs) { return offsetBy(-rhs.x, -rhs.y); } - Rect operator + (const Point& rhs) const; - Rect operator - (const Point& rhs) const; + const Rect operator + (const Point& rhs) const; + const Rect operator - (const Point& rhs) const; void translate(int dx, int dy) { // legacy, don't use. offsetBy(dx, dy); |