/* * 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 { namespace 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==(RectangleEdges const &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!=(RectangleEdges const &rhs) const noexcept { return !(*this == rhs); } bool isUniform() const noexcept { return left == top && left == right && left == bottom; } static RectangleEdges const ZERO; }; template constexpr RectangleEdges const RectangleEdges::ZERO = {}; template RectangleEdges operator+( RectangleEdges const &lhs, RectangleEdges const &rhs) noexcept { return RectangleEdges{ lhs.left + rhs.left, lhs.top + rhs.top, lhs.right + rhs.right, lhs.bottom + rhs.bottom}; } template RectangleEdges operator-( RectangleEdges const &lhs, RectangleEdges const &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(Rect const &rect, EdgeInsets const &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}}; } } // namespace react } // namespace facebook namespace std { template struct hash> { size_t operator()( facebook::react::RectangleEdges const &edges) const noexcept { return folly::hash::hash_combine( 0, edges.left, edges.right, edges.top, edges.bottom); } }; } // namespace std