Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C++ style enums 7/N: MeasureMode #1389

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .clang-format-ignore

This file was deleted.

50 changes: 48 additions & 2 deletions enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

import math
import os

ENUMS = {
Expand Down Expand Up @@ -88,7 +89,7 @@ def get_license(ext):
*/

// @{"generated"} by enums.py

{"// clang-format off" if ext == "cpp" else ""}
"""


Expand Down Expand Up @@ -121,7 +122,6 @@ def to_hyphenated_lower(symbol):
f.write(get_license("cpp"))
f.write("#pragma once\n")
f.write("#include <yoga/YGMacros.h>\n\n")
f.write("// clang-format off\n\n\n")

f.write("YG_EXTERN_C_BEGIN\n\n")
items = sorted(ENUMS.items())
Expand All @@ -146,6 +146,52 @@ def to_hyphenated_lower(symbol):
f.write("\n")
f.write("YG_EXTERN_C_END\n")

# Write out C++ scoped enums
for name, values in sorted(ENUMS.items()):
with open(f"{root}/yoga/enums/{name}.h", "w") as f:
f.write(get_license("cpp"))
f.write("#pragma once\n\n")

f.write("#include <cstdint>\n")
f.write("#include <yoga/YGEnums.h>\n")
f.write("#include <yoga/enums/YogaEnums.h>\n\n")

f.write("namespace facebook::yoga {\n\n")

width = "uint32_t" if name in BITSET_ENUMS else "uint8_t"
f.write(f"enum class {name} : {width} {{\n")
for value in values:
ordinal = value[0] if isinstance(value, tuple) else value
f.write(f" {ordinal} = YG{name}{ordinal},\n")
f.write("};\n\n")
f.write(
f"YG_DEFINE_ENUM_FLAG_OPERATORS({name})\n\n" if name in BITSET_ENUMS else ""
)

f.write("template <>\n")
f.write(f"constexpr inline int32_t ordinalCount<{name}>() {{\n")
f.write(f" return {len(values)};\n")
f.write("} \n\n")

f.write("template <>\n")
f.write(f"constexpr inline int32_t bitCount<{name}>() {{\n")
f.write(f" return {math.ceil(math.log(len(values), 2))};\n")
f.write("} \n\n")

f.write(f"constexpr inline {name} scopedEnum(YG{name} unscoped) {{\n")
f.write(f" return static_cast<{name}>(unscoped);\n")
f.write("}\n\n")

f.write(f"constexpr inline YG{name} unscopedEnum({name} scoped) {{\n")
f.write(f" return static_cast<YG{name}>(scoped);\n")
f.write("}\n\n")

f.write(f"inline const char* toString({name} e) {{\n")
f.write(f" return YG{name}ToString(unscopedEnum(e));\n")
f.write("}\n\n")

f.write("} // namespace facebook::yoga\n")

# write out C body for printing
with open(root + "/yoga/YGEnums.cpp", "w") as f:
f.write(get_license("cpp"))
Expand Down
2 changes: 1 addition & 1 deletion tests/YGNodeCallbackTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ TEST(Node, measure_with_measure_fn) {
});

ASSERT_EQ(
n.measure(23, YGMeasureModeExactly, 24, YGMeasureModeAtMost),
n.measure(23, MeasureMode::Exactly, 24, MeasureMode::AtMost),
(YGSize{23, 12}));
}

Expand Down
2 changes: 1 addition & 1 deletion yoga/YGEnums.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

// @generated by enums.py

// clang-format off
#include <yoga/YGEnums.h>

const char* YGAlignToString(const YGAlign value) {
Expand Down
5 changes: 1 addition & 4 deletions yoga/YGEnums.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@
*/

// @generated by enums.py

// clang-format off
#pragma once
#include <yoga/YGMacros.h>

// clang-format off


YG_EXTERN_C_BEGIN

YG_ENUM_SEQ_DECL(
Expand Down
28 changes: 13 additions & 15 deletions yoga/Yoga.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ void YGNodeSetHasNewLayout(YGNodeRef node, bool hasNewLayout) {
}

YGNodeType YGNodeGetNodeType(YGNodeConstRef node) {
return resolveRef(node)->getNodeType();
return unscopedEnum(resolveRef(node)->getNodeType());
}

void YGNodeSetNodeType(YGNodeRef node, YGNodeType nodeType) {
return resolveRef(node)->setNodeType(nodeType);
return resolveRef(node)->setNodeType(scopedEnum(nodeType));
}

bool YGNodeIsDirty(YGNodeConstRef node) {
Expand Down Expand Up @@ -801,11 +801,8 @@ YG_NODE_LAYOUT_RESOLVED_PROPERTY_IMPL(float, Border, border)
YG_NODE_LAYOUT_RESOLVED_PROPERTY_IMPL(float, Padding, padding)

#ifdef DEBUG
void YGNodePrint(const YGNodeConstRef nodeRef, const YGPrintOptions options) {
const auto node = resolveRef(nodeRef);
std::string str;
yoga::nodeToString(str, node, options, 0);
yoga::log(node, YGLogLevelDebug, str.c_str());
void YGNodePrint(const YGNodeConstRef node, const YGPrintOptions options) {
yoga::print(resolveRef(node), scopedEnum(options));
}
#endif

Expand Down Expand Up @@ -851,13 +848,14 @@ void YGConfigSetExperimentalFeatureEnabled(
const YGConfigRef config,
const YGExperimentalFeature feature,
const bool enabled) {
resolveRef(config)->setExperimentalFeatureEnabled(feature, enabled);
resolveRef(config)->setExperimentalFeatureEnabled(
scopedEnum(feature), enabled);
}

bool YGConfigIsExperimentalFeatureEnabled(
const YGConfigConstRef config,
const YGExperimentalFeature feature) {
return resolveRef(config)->isExperimentalFeatureEnabled(feature);
return resolveRef(config)->isExperimentalFeatureEnabled(scopedEnum(feature));
}

void YGConfigSetUseWebDefaults(const YGConfigRef config, const bool enabled) {
Expand All @@ -877,11 +875,11 @@ void* YGConfigGetContext(const YGConfigConstRef config) {
}

void YGConfigSetErrata(YGConfigRef config, YGErrata errata) {
resolveRef(config)->setErrata(errata);
resolveRef(config)->setErrata(scopedEnum(errata));
}

YGErrata YGConfigGetErrata(YGConfigConstRef config) {
return resolveRef(config)->getErrata();
return unscopedEnum(resolveRef(config)->getErrata());
}

void YGConfigSetCloneNodeFunc(
Expand All @@ -907,13 +905,13 @@ bool YGNodeCanUseCachedMeasurement(
float marginColumn,
YGConfigRef config) {
return yoga::canUseCachedMeasurement(
widthMode,
scopedEnum(widthMode),
availableWidth,
heightMode,
scopedEnum(heightMode),
availableHeight,
lastWidthMode,
scopedEnum(lastWidthMode),
lastAvailableWidth,
lastHeightMode,
scopedEnum(lastHeightMode),
lastAvailableHeight,
lastComputedWidth,
lastComputedHeight,
Expand Down
30 changes: 14 additions & 16 deletions yoga/algorithm/Cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,52 +5,50 @@
* LICENSE file in the root directory of this source tree.
*/

#include <yoga/Yoga.h>

#include <yoga/algorithm/Cache.h>
#include <yoga/algorithm/PixelGrid.h>
#include <yoga/numeric/Comparison.h>

namespace facebook::yoga {

static inline bool sizeIsExactAndMatchesOldMeasuredSize(
YGMeasureMode sizeMode,
MeasureMode sizeMode,
float size,
float lastComputedSize) {
return sizeMode == YGMeasureModeExactly &&
return sizeMode == MeasureMode::Exactly &&
yoga::inexactEquals(size, lastComputedSize);
}

static inline bool oldSizeIsUnspecifiedAndStillFits(
YGMeasureMode sizeMode,
MeasureMode sizeMode,
float size,
YGMeasureMode lastSizeMode,
MeasureMode lastSizeMode,
float lastComputedSize) {
return sizeMode == YGMeasureModeAtMost &&
lastSizeMode == YGMeasureModeUndefined &&
return sizeMode == MeasureMode::AtMost &&
lastSizeMode == MeasureMode::Undefined &&
(size >= lastComputedSize || yoga::inexactEquals(size, lastComputedSize));
}

static inline bool newMeasureSizeIsStricterAndStillValid(
YGMeasureMode sizeMode,
MeasureMode sizeMode,
float size,
YGMeasureMode lastSizeMode,
MeasureMode lastSizeMode,
float lastSize,
float lastComputedSize) {
return lastSizeMode == YGMeasureModeAtMost &&
sizeMode == YGMeasureModeAtMost && !std::isnan(lastSize) &&
return lastSizeMode == MeasureMode::AtMost &&
sizeMode == MeasureMode::AtMost && !std::isnan(lastSize) &&
!std::isnan(size) && !std::isnan(lastComputedSize) && lastSize > size &&
(lastComputedSize <= size || yoga::inexactEquals(size, lastComputedSize));
}

bool canUseCachedMeasurement(
const YGMeasureMode widthMode,
const MeasureMode widthMode,
const float availableWidth,
const YGMeasureMode heightMode,
const MeasureMode heightMode,
const float availableHeight,
const YGMeasureMode lastWidthMode,
const MeasureMode lastWidthMode,
const float lastAvailableWidth,
const YGMeasureMode lastHeightMode,
const MeasureMode lastHeightMode,
const float lastAvailableHeight,
const float lastComputedWidth,
const float lastComputedHeight,
Expand Down
10 changes: 5 additions & 5 deletions yoga/algorithm/Cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@

#pragma once

#include <yoga/Yoga.h>
#include <yoga/config/Config.h>
#include <yoga/enums/MeasureMode.h>

namespace facebook::yoga {

bool canUseCachedMeasurement(
YGMeasureMode widthMode,
MeasureMode widthMode,
float availableWidth,
YGMeasureMode heightMode,
MeasureMode heightMode,
float availableHeight,
YGMeasureMode lastWidthMode,
MeasureMode lastWidthMode,
float lastAvailableWidth,
YGMeasureMode lastHeightMode,
MeasureMode lastHeightMode,
float lastAvailableHeight,
float lastComputedWidth,
float lastComputedHeight,
Expand Down
Loading