From 0c7376c7ce460362008d03e1abe07319368765df Mon Sep 17 00:00:00 2001 From: David Aurelio Date: Wed, 1 May 2019 06:47:31 -0700 Subject: [PATCH] `YGStyle`: Make getters/setters template args of `BitfieldRef` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- ReactCommon/yoga/yoga/YGStyle.h | 81 ++++++++++++++------------------- 1 file changed, 35 insertions(+), 46 deletions(-) diff --git a/ReactCommon/yoga/yoga/YGStyle.h b/ReactCommon/yoga/yoga/YGStyle.h index d8e805e1625..d900d6c0ddf 100644 --- a/ReactCommon/yoga/yoga/YGStyle.h +++ b/ReactCommon/yoga/yoga/YGStyle.h @@ -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 @@ -82,15 +86,17 @@ public: CompactValue operator[](Idx idx) const { return (style.*Prop)[idx]; } }; - template + 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& operator=(T x) { - (style.*set)(x); + operator T() const { return (style.*Get)(); } + BitfieldRef& operator=(T x) { + (style.*Set)(x); style.assignedProps_.set(PropBit); return *this; } @@ -168,6 +174,17 @@ private: // 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_; @@ -177,50 +194,34 @@ public: using ValueRepr = std::remove_reference::type; YGDirection direction() const { return direction_; } - BitfieldRef direction() { - return BITFIELD_REF(direction); - } + BITFIELD_REF(direction) direction() { return {*this}; } YGFlexDirection flexDirection() const { return flexDirection_; } - BitfieldRef flexDirection() { - return BITFIELD_REF(flexDirection); - } + BITFIELD_REF(flexDirection) flexDirection() { return {*this}; } YGJustify justifyContent() const { return justifyContent_; } - BitfieldRef justifyContent() { - return BITFIELD_REF(justifyContent); - } + BITFIELD_REF(justifyContent) justifyContent() { return {*this}; } YGAlign alignContent() const { return alignContent_; } - BitfieldRef alignContent() { - return BITFIELD_REF(alignContent); - } + BITFIELD_REF(alignContent) alignContent() { return {*this}; } YGAlign alignItems() const { return alignItems_; } - BitfieldRef alignItems() { - return BITFIELD_REF(alignItems); - } + BITFIELD_REF(alignItems) alignItems() { return {*this}; } YGAlign alignSelf() const { return alignSelf_; } - BitfieldRef alignSelf() { - return BITFIELD_REF(alignSelf); - } + BITFIELD_REF(alignSelf) alignSelf() { return {*this}; } YGPositionType positionType() const { return positionType_; } - BitfieldRef positionType() { - return BITFIELD_REF(positionType); - } + BITFIELD_REF(positionType) positionType() { return {*this}; } YGWrap flexWrap() const { return flexWrap_; } - BitfieldRef flexWrap() { return BITFIELD_REF(flexWrap); } + BITFIELD_REF(flexWrap) flexWrap() { return {*this}; } YGOverflow overflow() const { return overflow_; } - BitfieldRef overflow() { - return BITFIELD_REF(overflow); - } + BITFIELD_REF(overflow) overflow() { return {*this}; } YGDisplay display() const { return display_; } - BitfieldRef display() { return BITFIELD_REF(display); } + BITFIELD_REF(display) display() { return {*this}; } YGFloatOptional flex() const { return flex_; } Ref flex() { return {*this}; } @@ -276,18 +277,6 @@ public: Ref 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);