C++ Cleanup 1/N: Reorganize YGStyle (#1349)

Summary:
X-link: https://github.com/facebook/react-native/pull/39221

Pull Request resolved: https://github.com/facebook/yoga/pull/1349

X-link: https://github.com/facebook/react-native/pull/39171

## This diff

This diff adds a `style` directory for code related to storing and manipulating styles. `YGStyle`, which is not a public API, is renamed to `yoga::Style` and moved into this folder, alongside `CompactValue`. We will eventually add `ValuePool` alongside this for the next generation style representation.

## This stack

The organization of the C++ internals of Yoga are in need of attention.
1. Some of the C++ internals are namespaced, but others not.
2. Some of the namespaces include `detail`, but are meant to be used outside of the translation unit (FB Clang Tidy rules warn on any usage of these)
2. Most of the files are in a flat hierarchy, except for event tracing in its own folder
3. Some files and functions begin with YG, others don’t
4. Some functions are uppercase, others are not
5. Almost all of the interesting logic is in Yoga.cpp, and the file is too large to reason about
6. There are multiple grab bag files where folks put random functions they need in (Utils, BitUtils, Yoga-Internal.h)
7. There is no clear indication from file structure or type naming what is private vs not
8. Handles like `YGNodeRef` and `YGConfigRef` can be used to access internals just by importing headers

This stack does some much needed spring cleaning:
1. All non-public headers and C++ implementation details are in separate folders from the root level `yoga`. This will give us room to split up logic and add more files without too large a flat hierarchy
3. All private C++ internals are under the `facebook::yoga` namespace. Details namespaces are only ever used within the same header, as they are intended
4. Utils files are split
5. Most C++ internals drop the YG prefix
6. Most C++ internal function names are all lower camel case
7. We start to split up Yoga.cpp
8. Every header beginning with YG or at the top-level directory is public and C only, with the exception of Yoga-Internal.h which has non-public functions for bindings
9. It is not possible to use private APIs without static casting handles to internal classes

This will give us more leeway to continue splitting monolithic files, and consistent guidelines for style in new files as well.

These changes should not be breaking to any project using only public Yoga headers. This includes every usage of Yoga in fbsource except for RN Fabric which is currently tied to internals. This refactor should make that boundary clearer.

Changelog: [Internal]

Reviewed By: shwanton

Differential Revision: D48847261

fbshipit-source-id: 0fc8c6991e19079f3f0d55d368574757e453fe93
This commit is contained in:
Nick Gerleman
2023-08-30 16:27:32 -07:00
committed by Facebook GitHub Bot
parent 2a32637c56
commit 65d7f95901
15 changed files with 182 additions and 183 deletions

239
yoga/style/Style.h Normal file
View File

@@ -0,0 +1,239 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <algorithm>
#include <array>
#include <cstdint>
#include <type_traits>
#include <yoga/Yoga.h>
#include <yoga/YGFloatOptional.h>
#include <yoga/Yoga-internal.h>
#include <yoga/BitUtils.h>
#include <yoga/style/CompactValue.h>
namespace facebook::yoga {
class YOGA_EXPORT Style {
template <typename Enum>
using Values = detail::Values<enums::count<Enum>()>;
public:
using Dimensions = Values<YGDimension>;
using Edges = Values<YGEdge>;
using Gutters = Values<YGGutter>;
template <typename T>
struct BitfieldRef {
Style& style;
size_t offset;
operator T() const {
return facebook::yoga::detail::getEnumData<T>(style.flags, offset);
}
BitfieldRef<T>& operator=(T x) {
facebook::yoga::detail::setEnumData<T>(style.flags, offset, x);
return *this;
}
};
template <typename T, T Style::*Prop>
struct Ref {
Style& style;
operator T() const { return style.*Prop; }
Ref<T, Prop>& operator=(T value) {
style.*Prop = value;
return *this;
}
};
template <typename Idx, Values<Idx> Style::*Prop>
struct IdxRef {
struct Ref {
Style& style;
Idx idx;
operator CompactValue() const { return (style.*Prop)[idx]; }
operator YGValue() const { return (style.*Prop)[idx]; }
Ref& operator=(CompactValue value) {
(style.*Prop)[idx] = value;
return *this;
}
};
Style& style;
IdxRef<Idx, Prop>& operator=(const Values<Idx>& values) {
style.*Prop = values;
return *this;
}
operator const Values<Idx>&() const { return style.*Prop; }
Ref operator[](Idx idx) { return {style, idx}; }
CompactValue operator[](Idx idx) const { return (style.*Prop)[idx]; }
};
Style() {
alignContent() = YGAlignFlexStart;
alignItems() = YGAlignStretch;
}
~Style() = default;
private:
static constexpr size_t directionOffset = 0;
static constexpr size_t flexdirectionOffset =
directionOffset + facebook::yoga::detail::bitWidthFn<YGDirection>();
static constexpr size_t justifyContentOffset = flexdirectionOffset +
facebook::yoga::detail::bitWidthFn<YGFlexDirection>();
static constexpr size_t alignContentOffset =
justifyContentOffset + facebook::yoga::detail::bitWidthFn<YGJustify>();
static constexpr size_t alignItemsOffset =
alignContentOffset + facebook::yoga::detail::bitWidthFn<YGAlign>();
static constexpr size_t alignSelfOffset =
alignItemsOffset + facebook::yoga::detail::bitWidthFn<YGAlign>();
static constexpr size_t positionTypeOffset =
alignSelfOffset + facebook::yoga::detail::bitWidthFn<YGAlign>();
static constexpr size_t flexWrapOffset =
positionTypeOffset + facebook::yoga::detail::bitWidthFn<YGPositionType>();
static constexpr size_t overflowOffset =
flexWrapOffset + facebook::yoga::detail::bitWidthFn<YGWrap>();
static constexpr size_t displayOffset =
overflowOffset + facebook::yoga::detail::bitWidthFn<YGOverflow>();
uint32_t flags = 0;
YGFloatOptional flex_ = {};
YGFloatOptional flexGrow_ = {};
YGFloatOptional flexShrink_ = {};
CompactValue flexBasis_ = CompactValue::ofAuto();
Edges margin_ = {};
Edges position_ = {};
Edges padding_ = {};
Edges border_ = {};
Gutters gap_ = {};
Dimensions dimensions_{CompactValue::ofAuto()};
Dimensions minDimensions_ = {};
Dimensions maxDimensions_ = {};
// Yoga specific properties, not compatible with flexbox specification
YGFloatOptional aspectRatio_ = {};
public:
// for library users needing a type
using ValueRepr = std::remove_reference<decltype(margin_[0])>::type;
YGDirection direction() const {
return facebook::yoga::detail::getEnumData<YGDirection>(
flags, directionOffset);
}
BitfieldRef<YGDirection> direction() { return {*this, directionOffset}; }
YGFlexDirection flexDirection() const {
return facebook::yoga::detail::getEnumData<YGFlexDirection>(
flags, flexdirectionOffset);
}
BitfieldRef<YGFlexDirection> flexDirection() {
return {*this, flexdirectionOffset};
}
YGJustify justifyContent() const {
return facebook::yoga::detail::getEnumData<YGJustify>(
flags, justifyContentOffset);
}
BitfieldRef<YGJustify> justifyContent() {
return {*this, justifyContentOffset};
}
YGAlign alignContent() const {
return facebook::yoga::detail::getEnumData<YGAlign>(
flags, alignContentOffset);
}
BitfieldRef<YGAlign> alignContent() { return {*this, alignContentOffset}; }
YGAlign alignItems() const {
return facebook::yoga::detail::getEnumData<YGAlign>(
flags, alignItemsOffset);
}
BitfieldRef<YGAlign> alignItems() { return {*this, alignItemsOffset}; }
YGAlign alignSelf() const {
return facebook::yoga::detail::getEnumData<YGAlign>(flags, alignSelfOffset);
}
BitfieldRef<YGAlign> alignSelf() { return {*this, alignSelfOffset}; }
YGPositionType positionType() const {
return facebook::yoga::detail::getEnumData<YGPositionType>(
flags, positionTypeOffset);
}
BitfieldRef<YGPositionType> positionType() {
return {*this, positionTypeOffset};
}
YGWrap flexWrap() const {
return facebook::yoga::detail::getEnumData<YGWrap>(flags, flexWrapOffset);
}
BitfieldRef<YGWrap> flexWrap() { return {*this, flexWrapOffset}; }
YGOverflow overflow() const {
return facebook::yoga::detail::getEnumData<YGOverflow>(
flags, overflowOffset);
}
BitfieldRef<YGOverflow> overflow() { return {*this, overflowOffset}; }
YGDisplay display() const {
return facebook::yoga::detail::getEnumData<YGDisplay>(flags, displayOffset);
}
BitfieldRef<YGDisplay> display() { return {*this, displayOffset}; }
YGFloatOptional flex() const { return flex_; }
Ref<YGFloatOptional, &Style::flex_> flex() { return {*this}; }
YGFloatOptional flexGrow() const { return flexGrow_; }
Ref<YGFloatOptional, &Style::flexGrow_> flexGrow() { return {*this}; }
YGFloatOptional flexShrink() const { return flexShrink_; }
Ref<YGFloatOptional, &Style::flexShrink_> flexShrink() { return {*this}; }
CompactValue flexBasis() const { return flexBasis_; }
Ref<CompactValue, &Style::flexBasis_> flexBasis() { return {*this}; }
const Edges& margin() const { return margin_; }
IdxRef<YGEdge, &Style::margin_> margin() { return {*this}; }
const Edges& position() const { return position_; }
IdxRef<YGEdge, &Style::position_> position() { return {*this}; }
const Edges& padding() const { return padding_; }
IdxRef<YGEdge, &Style::padding_> padding() { return {*this}; }
const Edges& border() const { return border_; }
IdxRef<YGEdge, &Style::border_> border() { return {*this}; }
const Gutters& gap() const { return gap_; }
IdxRef<YGGutter, &Style::gap_> gap() { return {*this}; }
const Dimensions& dimensions() const { return dimensions_; }
IdxRef<YGDimension, &Style::dimensions_> dimensions() { return {*this}; }
const Dimensions& minDimensions() const { return minDimensions_; }
IdxRef<YGDimension, &Style::minDimensions_> minDimensions() {
return {*this};
}
const Dimensions& maxDimensions() const { return maxDimensions_; }
IdxRef<YGDimension, &Style::maxDimensions_> maxDimensions() {
return {*this};
}
// Yoga specific properties, not compatible with flexbox specification
YGFloatOptional aspectRatio() const { return aspectRatio_; }
Ref<YGFloatOptional, &Style::aspectRatio_> aspectRatio() { return {*this}; }
};
YOGA_EXPORT bool operator==(const Style& lhs, const Style& rhs);
YOGA_EXPORT inline bool operator!=(const Style& lhs, const Style& rhs) {
return !(lhs == rhs);
}
} // namespace facebook::yoga