Move YG_ENABLE_EVENTS checks to event.h

Summary:
Instead of checking whether `YG_ENABLE_EVENTS` is defined for every publish, we simply wrap the body of the `publish` function macro that delegates to the method that actually publishes the event.

This way we get

1. easier to write code where we publish events
2. more type safety when editing, enabling editors/IDEs to show errors without knowing about `YG_ENABLE_EVENTS`

Reviewed By: SidharthGuglani

Differential Revision: D16049888

fbshipit-source-id: cbf362d6f7be5053c3f377125d303b7137d6a241
This commit is contained in:
David Aurelio
2019-06-28 09:53:15 -07:00
committed by Facebook Github Bot
parent c6ae314202
commit cd5324378d
2 changed files with 2 additions and 19 deletions

View File

@@ -213,9 +213,7 @@ WIN_EXPORT YGNodeRef YGNodeNewWithConfig(const YGConfigRef config) {
const YGNodeRef node = new YGNode{config}; const YGNodeRef node = new YGNode{config};
YGAssertWithConfig( YGAssertWithConfig(
config, node != nullptr, "Could not allocate memory for node"); config, node != nullptr, "Could not allocate memory for node");
#ifdef YG_ENABLE_EVENTS
Event::publish<Event::NodeAllocation>(node, {config}); Event::publish<Event::NodeAllocation>(node, {config});
#endif
return node; return node;
} }
@@ -235,9 +233,7 @@ YGNodeRef YGNodeClone(YGNodeRef oldNode) {
oldNode->getConfig(), oldNode->getConfig(),
node != nullptr, node != nullptr,
"Could not allocate memory for node"); "Could not allocate memory for node");
#ifdef YG_ENABLE_EVENTS
Event::publish<Event::NodeAllocation>(node, {node->getConfig()}); Event::publish<Event::NodeAllocation>(node, {node->getConfig()});
#endif
node->setOwner(nullptr); node->setOwner(nullptr);
return node; return node;
} }
@@ -256,9 +252,7 @@ static YGNodeRef YGNodeDeepClone(YGNodeRef oldNode) {
auto config = YGConfigClone(*oldNode->getConfig()); auto config = YGConfigClone(*oldNode->getConfig());
auto node = new YGNode{*oldNode, config}; auto node = new YGNode{*oldNode, config};
node->setOwner(nullptr); node->setOwner(nullptr);
#ifdef YG_ENABLE_EVENTS
Event::publish<Event::NodeAllocation>(node, {node->getConfig()}); Event::publish<Event::NodeAllocation>(node, {node->getConfig()});
#endif
YGVector vec = YGVector(); YGVector vec = YGVector();
vec.reserve(oldNode->getChildren().size()); vec.reserve(oldNode->getChildren().size());
@@ -286,9 +280,7 @@ void YGNodeFree(const YGNodeRef node) {
} }
node->clearChildren(); node->clearChildren();
#ifdef YG_ENABLE_EVENTS
Event::publish<Event::NodeDeallocation>(node, {node->getConfig()}); Event::publish<Event::NodeDeallocation>(node, {node->getConfig()});
#endif
delete node; delete node;
} }
@@ -1637,9 +1629,7 @@ static void YGNodeWithMeasureFuncSetMeasuredDimensions(
ownerWidth), ownerWidth),
YGDimensionHeight); YGDimensionHeight);
} else { } else {
#ifdef YG_ENABLE_EVENTS
Event::publish<Event::MeasureCallbackStart>(node); Event::publish<Event::MeasureCallbackStart>(node);
#endif
// Measure the text under the current constraints. // Measure the text under the current constraints.
const YGSize measuredSize = marker::MarkerSection<YGMarkerMeasure>::wrap( const YGSize measuredSize = marker::MarkerSection<YGMarkerMeasure>::wrap(
@@ -1653,7 +1643,6 @@ static void YGNodeWithMeasureFuncSetMeasuredDimensions(
layoutMarkerData.measureCallbacks += 1; layoutMarkerData.measureCallbacks += 1;
#ifdef YG_ENABLE_EVENTS
Event::publish<Event::MeasureCallbackEnd>( Event::publish<Event::MeasureCallbackEnd>(
node, node,
{layoutContext, {layoutContext,
@@ -1663,7 +1652,6 @@ static void YGNodeWithMeasureFuncSetMeasuredDimensions(
heightMeasureMode, heightMeasureMode,
measuredSize.width, measuredSize.width,
measuredSize.height}); measuredSize.height});
#endif
node->setLayoutMeasuredDimension( node->setLayoutMeasuredDimension(
YGNodeBoundAxis( YGNodeBoundAxis(
@@ -3956,7 +3944,6 @@ bool YGLayoutNodeInternal(
layout->generationCount = generationCount; layout->generationCount = generationCount;
#ifdef YG_ENABLE_EVENTS
LayoutType layoutType; LayoutType layoutType;
if (performLayout) { if (performLayout) {
layoutType = !needToVisitNode && cachedResults == &layout->cachedLayout layoutType = !needToVisitNode && cachedResults == &layout->cachedLayout
@@ -3967,7 +3954,6 @@ bool YGLayoutNodeInternal(
: LayoutType::kMeasure; : LayoutType::kMeasure;
} }
Event::publish<Event::NodeLayout>(node, {layoutType, layoutContext}); Event::publish<Event::NodeLayout>(node, {layoutType, layoutContext});
#endif
return (needToVisitNode || cachedResults == nullptr); return (needToVisitNode || cachedResults == nullptr);
} }
@@ -4076,9 +4062,7 @@ void YGNodeCalculateLayoutWithContext(
const YGDirection ownerDirection, const YGDirection ownerDirection,
void* layoutContext) { void* layoutContext) {
#ifdef YG_ENABLE_EVENTS
Event::publish<Event::LayoutPassStart>(node, {layoutContext}); Event::publish<Event::LayoutPassStart>(node, {layoutContext});
#endif
marker::MarkerSection<YGMarkerLayout> marker{node}; marker::MarkerSection<YGMarkerLayout> marker{node};
// Increment the generation count. This will force the recursive routine to // Increment the generation count. This will force the recursive routine to
@@ -4158,10 +4142,7 @@ void YGNodeCalculateLayoutWithContext(
} }
marker.end(); marker.end();
#ifdef YG_ENABLE_EVENTS
Event::publish<Event::LayoutPassEnd>(node, {layoutContext, &marker.data}); Event::publish<Event::LayoutPassEnd>(node, {layoutContext, &marker.data});
#endif
// We want to get rid off `useLegacyStretchBehaviour` from YGConfig. But we // We want to get rid off `useLegacyStretchBehaviour` from YGConfig. But we
// aren't sure whether client's of yoga have gotten rid off this flag or not. // aren't sure whether client's of yoga have gotten rid off this flag or not.

View File

@@ -60,7 +60,9 @@ struct Event {
template <Type E> template <Type E>
static void publish(const YGNode& node, const TypedData<E>& eventData = {}) { static void publish(const YGNode& node, const TypedData<E>& eventData = {}) {
#ifdef YG_ENABLE_EVENTS
publish(node, E, Data{eventData}); publish(node, E, Data{eventData});
#endif
} }
template <Type E> template <Type E>