Skip to content

Commit

Permalink
YGStyle: Make getters/setters template args of BitfieldRef
Browse files Browse the repository at this point in the history
Summary:
@public

Makes bitfield getters/setters part of the bitfield ref template.
Since we introduced the tracking bit as template parameter in D14933022, every bitfield ref is an individual class anyway, and having function pointers doesn’t potentially lead to less code generation anyway.

Furthermore, this change can (in the absence of tracking bits) avoid less specialized templates dealing with refs, and to dynamic dispatch as a consequence.

Reviewed By: SidharthGuglani

Differential Revision: D15085495

fbshipit-source-id: 0dd70fa05e9d43a29e38a619cddb642c9ca3f7ab
  • Loading branch information
davidaurelio authored and facebook-github-bot committed May 1, 2019
1 parent 0e17af9 commit 0c7376c
Showing 1 changed file with 35 additions and 46 deletions.
81 changes: 35 additions & 46 deletions ReactCommon/yoga/yoga/YGStyle.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@
decltype(FIELD##_) get_##FIELD() const { return FIELD##_; } \
void set_##FIELD(decltype(FIELD##_) x) { FIELD##_ = x; }

#define BITFIELD_REF(FIELD) \
{ *this, &YGStyle::get_##FIELD, &YGStyle::set_##FIELD }
#define BITFIELD_REF(FIELD) \
BitfieldRef< \
decltype(FIELD##_), \
&YGStyle::get_##FIELD, \
&YGStyle::set_##FIELD, \
FIELD##Bit>

class YGStyle {
template <typename Enum>
Expand Down Expand Up @@ -82,15 +86,17 @@ class YGStyle {
CompactValue operator[](Idx idx) const { return (style.*Prop)[idx]; }
};

template <typename T, int PropBit>
template <
typename T,
T (YGStyle::*Get)() const,
void (YGStyle::*Set)(T),
int PropBit>
struct BitfieldRef {
YGStyle& style;
T (YGStyle::*get)() const;
void (YGStyle::*set)(T);

operator T() const { return (style.*get)(); }
BitfieldRef<T, PropBit>& operator=(T x) {
(style.*set)(x);
operator T() const { return (style.*Get)(); }
BitfieldRef<T, Get, Set, PropBit>& operator=(T x) {
(style.*Set)(x);
style.assignedProps_.set(PropBit);
return *this;
}
Expand Down Expand Up @@ -168,6 +174,17 @@ class YGStyle {
// Yoga specific properties, not compatible with flexbox specification
YGFloatOptional aspectRatio_ = {};

BITFIELD_ACCESSORS(direction)
BITFIELD_ACCESSORS(flexDirection)
BITFIELD_ACCESSORS(justifyContent)
BITFIELD_ACCESSORS(alignContent);
BITFIELD_ACCESSORS(alignItems);
BITFIELD_ACCESSORS(alignSelf);
BITFIELD_ACCESSORS(positionType);
BITFIELD_ACCESSORS(flexWrap);
BITFIELD_ACCESSORS(overflow);
BITFIELD_ACCESSORS(display);

public:
const decltype(assignedProps_)& assignedProps() const {
return assignedProps_;
Expand All @@ -177,50 +194,34 @@ class YGStyle {
using ValueRepr = std::remove_reference<decltype(margin_[0])>::type;

YGDirection direction() const { return direction_; }
BitfieldRef<YGDirection, directionBit> direction() {
return BITFIELD_REF(direction);
}
BITFIELD_REF(direction) direction() { return {*this}; }

YGFlexDirection flexDirection() const { return flexDirection_; }
BitfieldRef<YGFlexDirection, flexDirectionBit> flexDirection() {
return BITFIELD_REF(flexDirection);
}
BITFIELD_REF(flexDirection) flexDirection() { return {*this}; }

YGJustify justifyContent() const { return justifyContent_; }
BitfieldRef<YGJustify, justifyContentBit> justifyContent() {
return BITFIELD_REF(justifyContent);
}
BITFIELD_REF(justifyContent) justifyContent() { return {*this}; }

YGAlign alignContent() const { return alignContent_; }
BitfieldRef<YGAlign, alignContentBit> alignContent() {
return BITFIELD_REF(alignContent);
}
BITFIELD_REF(alignContent) alignContent() { return {*this}; }

YGAlign alignItems() const { return alignItems_; }
BitfieldRef<YGAlign, alignItemsBit> alignItems() {
return BITFIELD_REF(alignItems);
}
BITFIELD_REF(alignItems) alignItems() { return {*this}; }

YGAlign alignSelf() const { return alignSelf_; }
BitfieldRef<YGAlign, alignSelfBit> alignSelf() {
return BITFIELD_REF(alignSelf);
}
BITFIELD_REF(alignSelf) alignSelf() { return {*this}; }

YGPositionType positionType() const { return positionType_; }
BitfieldRef<YGPositionType, positionTypeBit> positionType() {
return BITFIELD_REF(positionType);
}
BITFIELD_REF(positionType) positionType() { return {*this}; }

YGWrap flexWrap() const { return flexWrap_; }
BitfieldRef<YGWrap, flexWrapBit> flexWrap() { return BITFIELD_REF(flexWrap); }
BITFIELD_REF(flexWrap) flexWrap() { return {*this}; }

YGOverflow overflow() const { return overflow_; }
BitfieldRef<YGOverflow, overflowBit> overflow() {
return BITFIELD_REF(overflow);
}
BITFIELD_REF(overflow) overflow() { return {*this}; }

YGDisplay display() const { return display_; }
BitfieldRef<YGDisplay, displayBit> display() { return BITFIELD_REF(display); }
BITFIELD_REF(display) display() { return {*this}; }

YGFloatOptional flex() const { return flex_; }
Ref<YGFloatOptional, &YGStyle::flex_, flexBit> flex() { return {*this}; }
Expand Down Expand Up @@ -276,18 +277,6 @@ class YGStyle {
Ref<YGFloatOptional, &YGStyle::aspectRatio_, aspectRatioBit> aspectRatio() {
return {*this};
}

private:
BITFIELD_ACCESSORS(direction)
BITFIELD_ACCESSORS(flexDirection)
BITFIELD_ACCESSORS(justifyContent)
BITFIELD_ACCESSORS(alignContent);
BITFIELD_ACCESSORS(alignItems);
BITFIELD_ACCESSORS(alignSelf);
BITFIELD_ACCESSORS(positionType);
BITFIELD_ACCESSORS(flexWrap);
BITFIELD_ACCESSORS(overflow);
BITFIELD_ACCESSORS(display);
};

bool operator==(const YGStyle& lhs, const YGStyle& rhs);
Expand Down

0 comments on commit 0c7376c

Please sign in to comment.