Skip to content

Commit

Permalink
Use constexpr in some geometry classes
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=273954
rdar://127819143

Reviewed by Wenson Hsieh and Matthieu Dubet.

Add constexpr to more constructors and functions on IntPoint/LayoutPoint/LayoutUnit to
allow for these types to be used in compile-time constants.

* Source/WebCore/platform/LayoutUnit.h:
(WebCore::LayoutUnit::LayoutUnit):
(WebCore::LayoutUnit::toInt const):
(WebCore::LayoutUnit::toFloat const):
(WebCore::LayoutUnit::toDouble const):
(WebCore::LayoutUnit::toUnsigned const):
(WebCore::LayoutUnit::operator int const):
(WebCore::LayoutUnit::operator float const):
(WebCore::LayoutUnit::operator double const):
(WebCore::LayoutUnit::operator bool const):
(WebCore::LayoutUnit::rawValue const):
* Source/WebCore/platform/graphics/IntPoint.h:
(WebCore::IntPoint::IntPoint):
(WebCore::IntPoint::zero):
(WebCore::IntPoint::isZero const):
(WebCore::IntPoint::x const):
(WebCore::IntPoint::y const):
(WebCore::IntPoint::expandedTo const):
(WebCore::IntPoint::shrunkTo const):
* Source/WebCore/platform/graphics/LayoutPoint.h:
(WebCore::LayoutPoint::LayoutPoint):
(WebCore::LayoutPoint::zero):
(WebCore::LayoutPoint::isZero const):

Canonical link: https://commits.webkit.org/278589@main
  • Loading branch information
smfr committed May 9, 2024
1 parent e8e3440 commit eb42c47
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 21 deletions.
21 changes: 11 additions & 10 deletions Source/WebCore/platform/LayoutUnit.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ const int intMinForLayoutUnit = INT_MIN / kFixedPointDenominator;

class LayoutUnit {
public:
LayoutUnit() : m_value(0) { }
constexpr LayoutUnit() : m_value(0) { }
LayoutUnit(const LayoutUnit&) = default;
LayoutUnit(int value) { setValue(value); }
LayoutUnit(unsigned short value) { setValue(value); }
Expand Down Expand Up @@ -117,23 +117,23 @@ class LayoutUnit {
return v;
}

int toInt() const { return m_value / kFixedPointDenominator; }
float toFloat() const { return static_cast<float>(m_value) / kFixedPointDenominator; }
double toDouble() const { return static_cast<double>(m_value) / kFixedPointDenominator; }
unsigned toUnsigned() const { REPORT_OVERFLOW(m_value >= 0); return toInt(); }
constexpr int toInt() const { return m_value / kFixedPointDenominator; }
constexpr float toFloat() const { return static_cast<float>(m_value) / kFixedPointDenominator; }
constexpr double toDouble() const { return static_cast<double>(m_value) / kFixedPointDenominator; }
constexpr unsigned toUnsigned() const { REPORT_OVERFLOW(m_value >= 0); return toInt(); }

operator int() const { return toInt(); }
operator float() const { return toFloat(); }
operator double() const { return toDouble(); }
explicit operator bool() const { return m_value; }
constexpr operator int() const { return toInt(); }
constexpr operator float() const { return toFloat(); }
constexpr operator double() const { return toDouble(); }
explicit constexpr operator bool() const { return m_value; }

LayoutUnit& operator++()
{
m_value += kFixedPointDenominator;
return *this;
}

inline int rawValue() const { return m_value; }
constexpr int rawValue() const { return m_value; }
inline void setRawValue(int value) { m_value = value; }
void setRawValue(long long value)
{
Expand All @@ -147,6 +147,7 @@ class LayoutUnit {
returnValue.setRawValue(::abs(m_value));
return returnValue;
}

int ceil() const
{
if (UNLIKELY(m_value >= INT_MAX - kFixedPointDenominator + 1))
Expand Down
16 changes: 8 additions & 8 deletions Source/WebCore/platform/graphics/IntPoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,16 @@ class IntRect;

class IntPoint {
public:
IntPoint() : m_x(0), m_y(0) { }
IntPoint(int x, int y) : m_x(x), m_y(y) { }
constexpr IntPoint() : m_x(0), m_y(0) { }
constexpr IntPoint(int x, int y) : m_x(x), m_y(y) { }
explicit IntPoint(const IntSize& size) : m_x(size.width()), m_y(size.height()) { }
WEBCORE_EXPORT explicit IntPoint(const FloatPoint&); // don't do this implicitly since it's lossy

static IntPoint zero() { return IntPoint(); }
bool isZero() const { return !m_x && !m_y; }
static constexpr IntPoint zero() { return IntPoint(); }
constexpr bool isZero() const { return !m_x && !m_y; }

int x() const { return m_x; }
int y() const { return m_y; }
constexpr int x() const { return m_x; }
constexpr int y() const { return m_y; }

void setX(int x) { m_x = x; }
void setY(int y) { m_y = y; }
Expand All @@ -86,15 +86,15 @@ class IntPoint {
this->scale(scale, scale);
}

IntPoint expandedTo(const IntPoint& other) const
constexpr IntPoint expandedTo(const IntPoint& other) const
{
return {
m_x > other.m_x ? m_x : other.m_x,
m_y > other.m_y ? m_y : other.m_y
};
}

IntPoint shrunkTo(const IntPoint& other) const
constexpr IntPoint shrunkTo(const IntPoint& other) const
{
return {
m_x < other.m_x ? m_x : other.m_x,
Expand Down
6 changes: 3 additions & 3 deletions Source/WebCore/platform/graphics/LayoutPoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ namespace WebCore {

class LayoutPoint {
public:
LayoutPoint() { }
constexpr LayoutPoint() { }
template<typename T, typename U> LayoutPoint(T x, U y) : m_x(x), m_y(y) { }
LayoutPoint(const IntPoint& point) : m_x(point.x()), m_y(point.y()) { }
explicit LayoutPoint(const FloatPoint& size) : m_x(size.x()), m_y(size.y()) { }
explicit LayoutPoint(const LayoutSize& size) : m_x(size.width()), m_y(size.height()) { }

static LayoutPoint zero() { return LayoutPoint(); }
bool isZero() const { return !m_x && !m_y; }
static constexpr LayoutPoint zero() { return LayoutPoint(); }
constexpr bool isZero() const { return !m_x && !m_y; }

LayoutUnit x() const { return m_x; }
LayoutUnit y() const { return m_y; }
Expand Down

0 comments on commit eb42c47

Please sign in to comment.