From 2a942b7ae8d50020a00bdf5bc45d31d187ba8ce6 Mon Sep 17 00:00:00 2001 From: Valentin Shergin Date: Mon, 3 Sep 2018 22:53:13 -0700 Subject: [PATCH] Fabric: Defined `+` and `*` operators for Point and Size geometric types Summary: @public Trivial. Those operations are very useful in layout algorithms. Reviewed By: mdvacca Differential Revision: D9403566 fbshipit-source-id: e76967aaaac3a36bf6d3e7a468b5ae7769a4dcac --- ReactCommon/fabric/graphics/Geometry.h | 46 ++++++++++++++++++-------- 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/ReactCommon/fabric/graphics/Geometry.h b/ReactCommon/fabric/graphics/Geometry.h index 3db7908860..0bd5448791 100644 --- a/ReactCommon/fabric/graphics/Geometry.h +++ b/ReactCommon/fabric/graphics/Geometry.h @@ -20,23 +20,29 @@ struct Point { Float x {0}; Float y {0}; - Point& operator += (const Point& rhs) { - x += rhs.x; - y += rhs.y; + Point &operator+=(const Point &point) { + x += point.x; + y += point.y; return *this; } - friend Point operator + (Point lhs, const Point& rhs) { + Point &operator*=(const Point &point) { + x *= point.x; + y *= point.y; + return *this; + } + + friend Point operator+(Point lhs, const Point &rhs) { return lhs += rhs; } - bool operator ==(const Point& rhs) const { + bool operator==(const Point &rhs) const { return std::tie(this->x, this->y) == std::tie(rhs.x, rhs.y); } - bool operator !=(const Point& rhs) const { + bool operator!=(const Point &rhs) const { return !(*this == rhs); } }; @@ -48,13 +54,25 @@ struct Size { Float width {0}; Float height {0}; - bool operator ==(const Size& rhs) const { + Size &operator+=(const Point &point) { + width += point.x; + height += point.y; + return *this; + } + + Size &operator*=(const Point &point) { + width *= point.x; + height *= point.y; + return *this; + } + + bool operator==(const Size &rhs) const { return std::tie(this->width, this->height) == std::tie(rhs.width, rhs.height); } - bool operator !=(const Size& rhs) const { + bool operator!=(const Size &rhs) const { return !(*this == rhs); } }; @@ -66,13 +84,13 @@ struct Rect { Point origin {0, 0}; Size size {0, 0}; - bool operator ==(const Rect& rhs) const { + bool operator==(const Rect &rhs) const { return std::tie(this->origin, this->size) == std::tie(rhs.origin, rhs.size); } - bool operator !=(const Rect& rhs) const { + bool operator!=(const Rect &rhs) const { return !(*this == rhs); } @@ -100,13 +118,13 @@ struct EdgeInsets { Float right {0}; Float bottom {0}; - bool operator ==(const EdgeInsets& rhs) const { + bool operator==(const EdgeInsets &rhs) const { return std::tie(this->left, this->top, this->right, this->bottom) == std::tie(rhs.left, rhs.top, rhs.right, rhs.bottom); } - bool operator !=(const EdgeInsets& rhs) const { + bool operator!=(const EdgeInsets &rhs) const { return !(*this == rhs); } @@ -126,13 +144,13 @@ struct CornerInsets { Float bottomLeft {0}; Float bottomRight {0}; - bool operator ==(const CornerInsets& rhs) const { + bool operator==(const CornerInsets &rhs) const { return std::tie(this->topLeft, this->topRight, this->bottomLeft, this->bottomRight) == std::tie(rhs.topLeft, rhs.topRight, rhs.bottomLeft, rhs.bottomRight); } - bool operator !=(const CornerInsets& rhs) const { + bool operator!=(const CornerInsets &rhs) const { return !(*this == rhs); }