/* * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ #pragma once #include #include #include #include namespace facebook::react { /* * Generic data structure describes some values associated with *edges* * of a rectangle. */ template struct RectangleEdges { T left{}; T top{}; T right{}; T bottom{}; bool operator==(const RectangleEdges& rhs) const noexcept { return std::tie(this->left, this->top, this->right, this->bottom) == std::tie(rhs.left, rhs.top, rhs.right, rhs.bottom); } bool operator!=(const RectangleEdges& rhs) const noexcept { return !(*this == rhs); } bool isUniform() const noexcept { return left == top && left == right && left == bottom; } static const RectangleEdges ZERO; }; template const RectangleEdges RectangleEdges::ZERO = {}; template RectangleEdges operator+( const RectangleEdges& lhs, const RectangleEdges& rhs) noexcept { return RectangleEdges{ lhs.left + rhs.left, lhs.top + rhs.top, lhs.right + rhs.right, lhs.bottom + rhs.bottom}; } template RectangleEdges operator-( const RectangleEdges& lhs, const RectangleEdges& rhs) noexcept { return RectangleEdges{ lhs.left - rhs.left, lhs.top - rhs.top, lhs.right - rhs.right, lhs.bottom - rhs.bottom}; } /* * EdgeInsets */ using EdgeInsets = RectangleEdges; /* * Adjusts a rectangle by the given edge insets. */ inline Rect insetBy(const Rect& rect, const EdgeInsets& insets) noexcept { return Rect{ {rect.origin.x + insets.left, rect.origin.y + insets.top}, {rect.size.width - insets.left - insets.right, rect.size.height - insets.top - insets.bottom}}; } /* * Adjusts a rectangle by the given edge outsets. */ inline Rect outsetBy(const Rect& rect, const EdgeInsets& outsets) noexcept { return Rect{ {rect.origin.x - outsets.left, rect.origin.y - outsets.top}, {rect.size.width + outsets.left + outsets.right, rect.size.height + outsets.top + outsets.bottom}}; } } // namespace facebook::react namespace std { template struct hash> { size_t operator()( const facebook::react::RectangleEdges& edges) const noexcept { return facebook::react::hash_combine( edges.left, edges.right, edges.top, edges.bottom); } }; } // namespace std