1// Copyright (C) 2016 The Qt Company Ltd.
2// Copyright (c) Meta Platforms, Inc. and affiliates.
3//
4// SPDX-License-Identifier: MIT
5
6#include "Utils.h"
7#include <stdexcept>
8#include <exception>
9
10QT_YOGA_NAMESPACE_BEGIN
11
12using namespace facebook;
13
14YGFlexDirection YGFlexDirectionCross(
15 const YGFlexDirection flexDirection,
16 const YGDirection direction) {
17 return YGFlexDirectionIsColumn(flexDirection)
18 ? YGResolveFlexDirection(flexDirection: YGFlexDirectionRow, direction)
19 : YGFlexDirectionColumn;
20}
21
22float YGFloatMax(const float a, const float b) {
23 if (!yoga::isUndefined(value: a) && !yoga::isUndefined(value: b)) {
24 return fmaxf(x: a, y: b);
25 }
26 return yoga::isUndefined(value: a) ? b : a;
27}
28
29float YGFloatMin(const float a, const float b) {
30 if (!yoga::isUndefined(value: a) && !yoga::isUndefined(value: b)) {
31 return fminf(x: a, y: b);
32 }
33
34 return yoga::isUndefined(value: a) ? b : a;
35}
36
37bool YGValueEqual(const YGValue& a, const YGValue& b) {
38 if (a.unit != b.unit) {
39 return false;
40 }
41
42 if (a.unit == YGUnitUndefined ||
43 (yoga::isUndefined(value: a.value) && yoga::isUndefined(value: b.value))) {
44 return true;
45 }
46
47 return fabs(x: a.value - b.value) < 0.0001f;
48}
49
50bool YGFloatsEqual(const float a, const float b) {
51 if (!yoga::isUndefined(value: a) && !yoga::isUndefined(value: b)) {
52 return fabs(x: a - b) < 0.0001f;
53 }
54 return yoga::isUndefined(value: a) && yoga::isUndefined(value: b);
55}
56
57bool YGDoubleEqual(const double a, const double b) {
58 if (!yoga::isUndefined(value: a) && !yoga::isUndefined(value: b)) {
59 return fabs(x: a - b) < 0.0001;
60 }
61 return yoga::isUndefined(value: a) && yoga::isUndefined(value: b);
62}
63
64float YGFloatSanitize(const float val) {
65 return yoga::isUndefined(value: val) ? 0 : val;
66}
67
68YGFloatOptional YGFloatOptionalMax(YGFloatOptional op1, YGFloatOptional op2) {
69 if (op1 >= op2) {
70 return op1;
71 }
72 if (op2 > op1) {
73 return op2;
74 }
75 return op1.isUndefined() ? op2 : op1;
76}
77
78void yoga::throwLogicalErrorWithMessage([[maybe_unused]]const char* message) {
79#if defined(__cpp_exceptions)
80 throw std::logic_error(message);
81#else // !defined(__cpp_exceptions)
82 std::terminate();
83#endif // defined(__cpp_exceptions)
84}
85
86QT_YOGA_NAMESPACE_END
87

source code of qtdeclarative/src/3rdparty/yoga/Utils.cpp