1/*
2 * Copyright (C) 2016 The Qt Company Ltd.
3 * Copyright (c) Meta Platforms, Inc. and affiliates.
4 *
5 * SPDX-License-Identifier: MIT
6 */
7
8#pragma once
9
10#include <algorithm>
11#include <array>
12#include <cmath>
13#include <vector>
14
15#include <yoga/Yoga.h>
16
17#include "CompactValue.h"
18
19USE_QT_YOGA_NAMESPACE
20
21using YGVector = std::vector<YGNodeRef>;
22
23QT_YOGA_NAMESPACE_BEGIN
24
25YG_EXTERN_C_BEGIN
26
27void YGNodeCalculateLayoutWithContext(
28 YGNodeRef node,
29 float availableWidth,
30 float availableHeight,
31 YGDirection ownerDirection,
32 void* layoutContext);
33
34// Deallocates a Yoga Node. Unlike YGNodeFree, does not remove the node from
35// its parent or children.
36void YGNodeDeallocate(YGNodeRef node);
37
38YG_EXTERN_C_END
39
40namespace facebook {
41namespace yoga {
42
43inline bool isUndefined(float value) {
44 return std::isnan(x: value);
45}
46
47inline bool isUndefined(double value) {
48 return std::isnan(x: value);
49}
50
51void throwLogicalErrorWithMessage(const char* message);
52
53} // namespace yoga
54} // namespace facebook
55
56extern const std::array<YGEdge, 4> trailing;
57extern const std::array<YGEdge, 4> leading;
58extern const YGValue YGValueUndefined;
59extern const YGValue YGValueAuto;
60extern const YGValue YGValueZero;
61
62struct YGCachedMeasurement {
63 float availableWidth;
64 float availableHeight;
65 YGMeasureMode widthMeasureMode;
66 YGMeasureMode heightMeasureMode;
67
68 float computedWidth;
69 float computedHeight;
70
71 YGCachedMeasurement()
72 : availableWidth(-1),
73 availableHeight(-1),
74 widthMeasureMode(YGMeasureModeUndefined),
75 heightMeasureMode(YGMeasureModeUndefined),
76 computedWidth(-1),
77 computedHeight(-1) {}
78
79 bool operator==(YGCachedMeasurement measurement) const {
80 using namespace facebook;
81
82 bool isEqual = widthMeasureMode == measurement.widthMeasureMode &&
83 heightMeasureMode == measurement.heightMeasureMode;
84
85 if (!yoga::isUndefined(value: availableWidth) ||
86 !yoga::isUndefined(value: measurement.availableWidth)) {
87 isEqual = isEqual && availableWidth == measurement.availableWidth;
88 }
89 if (!yoga::isUndefined(value: availableHeight) ||
90 !yoga::isUndefined(value: measurement.availableHeight)) {
91 isEqual = isEqual && availableHeight == measurement.availableHeight;
92 }
93 if (!yoga::isUndefined(value: computedWidth) ||
94 !yoga::isUndefined(value: measurement.computedWidth)) {
95 isEqual = isEqual && computedWidth == measurement.computedWidth;
96 }
97 if (!yoga::isUndefined(value: computedHeight) ||
98 !yoga::isUndefined(value: measurement.computedHeight)) {
99 isEqual = isEqual && computedHeight == measurement.computedHeight;
100 }
101
102 return isEqual;
103 }
104};
105
106// This value was chosen based on empirical data:
107// 98% of analyzed layouts require less than 8 entries.
108#define YG_MAX_CACHED_RESULT_COUNT 8
109
110namespace facebook {
111namespace yoga {
112namespace detail {
113
114template <size_t Size>
115class Values {
116private:
117 std::array<CompactValue, Size> values_;
118
119public:
120 Values() = default;
121 Values(const Values& other) = default;
122
123 explicit Values(const YGValue& defaultValue) noexcept {
124 values_.fill(defaultValue);
125 }
126
127 const CompactValue& operator[](size_t i) const noexcept { return values_[i]; }
128 CompactValue& operator[](size_t i) noexcept { return values_[i]; }
129
130 template <size_t I>
131 YGValue get() const noexcept {
132 return std::get<I>(values_);
133 }
134
135 template <size_t I>
136 void set(YGValue& value) noexcept {
137 std::get<I>(values_) = value;
138 }
139
140 template <size_t I>
141 void set(YGValue&& value) noexcept {
142 set<I>(value);
143 }
144
145 bool operator==(const Values& other) const noexcept {
146 for (size_t i = 0; i < Size; ++i) {
147 if (values_[i] != other.values_[i]) {
148 return false;
149 }
150 }
151 return true;
152 }
153
154 Values& operator=(const Values& other) = default;
155};
156} // namespace detail
157} // namespace yoga
158} // namespace facebook
159
160static const float kDefaultFlexGrow = 0.0f;
161static const float kDefaultFlexShrink = 0.0f;
162static const float kWebDefaultFlexShrink = 1.0f;
163
164extern bool YGFloatsEqual(const float a, const float b);
165
166QT_YOGA_NAMESPACE_END
167

source code of qtdeclarative/src/3rdparty/yoga/Yoga-internal.h