diff --git a/yoga/YGStyle.h b/yoga/YGStyle.h index d8e805e1..d900d6c0 100644 --- a/yoga/YGStyle.h +++ b/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);