Skip to content

Commit

Permalink
bitflags: Be explicit in binary operators and such.
Browse files Browse the repository at this point in the history
This allows having explicit + default constructor without build issues.
  • Loading branch information
emilio committed Jun 7, 2022
1 parent 9855f90 commit 80da604
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 18 deletions.
5 changes: 3 additions & 2 deletions src/bindgen/ir/structure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,8 @@ impl Struct {
out.open_brace();
write!(
out,
"return {{static_cast<decltype(bits)>(this->bits {} {}.bits)}};",
"return {} {{ static_cast<decltype(bits)>(this->bits {} {}.bits) }};",
self.export_name(),
operator, other
);
out.close_brace(false);
Expand Down Expand Up @@ -553,7 +554,7 @@ impl Source for Struct {
self.export_name()
);
out.open_brace();
write!(out, "return {{static_cast<decltype(bits)>(~bits)}};");
write!(out, "return {} {{ static_cast<decltype(bits)>(~bits) }};", self.export_name());
out.close_brace(false);
self.emit_bitflags_binop(constexpr_prefix, '|', &other, out);
self.emit_bitflags_binop(constexpr_prefix, '&', &other, out);
Expand Down
8 changes: 4 additions & 4 deletions tests/expectations/associated_in_body.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,24 @@ struct StyleAlignFlags {
return !!bits;
}
constexpr StyleAlignFlags operator~() const {
return {static_cast<decltype(bits)>(~bits)};
return StyleAlignFlags { static_cast<decltype(bits)>(~bits) };
}
constexpr StyleAlignFlags operator|(const StyleAlignFlags& other) const {
return {static_cast<decltype(bits)>(this->bits | other.bits)};
return StyleAlignFlags { static_cast<decltype(bits)>(this->bits | other.bits) };
}
StyleAlignFlags& operator|=(const StyleAlignFlags& other) {
*this = (*this | other);
return *this;
}
constexpr StyleAlignFlags operator&(const StyleAlignFlags& other) const {
return {static_cast<decltype(bits)>(this->bits & other.bits)};
return StyleAlignFlags { static_cast<decltype(bits)>(this->bits & other.bits) };
}
StyleAlignFlags& operator&=(const StyleAlignFlags& other) {
*this = (*this & other);
return *this;
}
constexpr StyleAlignFlags operator^(const StyleAlignFlags& other) const {
return {static_cast<decltype(bits)>(this->bits ^ other.bits)};
return StyleAlignFlags { static_cast<decltype(bits)>(this->bits ^ other.bits) };
}
StyleAlignFlags& operator^=(const StyleAlignFlags& other) {
*this = (*this ^ other);
Expand Down
24 changes: 12 additions & 12 deletions tests/expectations/bitflags.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,24 @@ struct AlignFlags {
return !!bits;
}
constexpr AlignFlags operator~() const {
return {static_cast<decltype(bits)>(~bits)};
return AlignFlags { static_cast<decltype(bits)>(~bits) };
}
constexpr AlignFlags operator|(const AlignFlags& other) const {
return {static_cast<decltype(bits)>(this->bits | other.bits)};
return AlignFlags { static_cast<decltype(bits)>(this->bits | other.bits) };
}
AlignFlags& operator|=(const AlignFlags& other) {
*this = (*this | other);
return *this;
}
constexpr AlignFlags operator&(const AlignFlags& other) const {
return {static_cast<decltype(bits)>(this->bits & other.bits)};
return AlignFlags { static_cast<decltype(bits)>(this->bits & other.bits) };
}
AlignFlags& operator&=(const AlignFlags& other) {
*this = (*this & other);
return *this;
}
constexpr AlignFlags operator^(const AlignFlags& other) const {
return {static_cast<decltype(bits)>(this->bits ^ other.bits)};
return AlignFlags { static_cast<decltype(bits)>(this->bits ^ other.bits) };
}
AlignFlags& operator^=(const AlignFlags& other) {
*this = (*this ^ other);
Expand Down Expand Up @@ -59,24 +59,24 @@ struct DebugFlags {
return !!bits;
}
constexpr DebugFlags operator~() const {
return {static_cast<decltype(bits)>(~bits)};
return DebugFlags { static_cast<decltype(bits)>(~bits) };
}
constexpr DebugFlags operator|(const DebugFlags& other) const {
return {static_cast<decltype(bits)>(this->bits | other.bits)};
return DebugFlags { static_cast<decltype(bits)>(this->bits | other.bits) };
}
DebugFlags& operator|=(const DebugFlags& other) {
*this = (*this | other);
return *this;
}
constexpr DebugFlags operator&(const DebugFlags& other) const {
return {static_cast<decltype(bits)>(this->bits & other.bits)};
return DebugFlags { static_cast<decltype(bits)>(this->bits & other.bits) };
}
DebugFlags& operator&=(const DebugFlags& other) {
*this = (*this & other);
return *this;
}
constexpr DebugFlags operator^(const DebugFlags& other) const {
return {static_cast<decltype(bits)>(this->bits ^ other.bits)};
return DebugFlags { static_cast<decltype(bits)>(this->bits ^ other.bits) };
}
DebugFlags& operator^=(const DebugFlags& other) {
*this = (*this ^ other);
Expand All @@ -93,24 +93,24 @@ struct LargeFlags {
return !!bits;
}
constexpr LargeFlags operator~() const {
return {static_cast<decltype(bits)>(~bits)};
return LargeFlags { static_cast<decltype(bits)>(~bits) };
}
constexpr LargeFlags operator|(const LargeFlags& other) const {
return {static_cast<decltype(bits)>(this->bits | other.bits)};
return LargeFlags { static_cast<decltype(bits)>(this->bits | other.bits) };
}
LargeFlags& operator|=(const LargeFlags& other) {
*this = (*this | other);
return *this;
}
constexpr LargeFlags operator&(const LargeFlags& other) const {
return {static_cast<decltype(bits)>(this->bits & other.bits)};
return LargeFlags { static_cast<decltype(bits)>(this->bits & other.bits) };
}
LargeFlags& operator&=(const LargeFlags& other) {
*this = (*this & other);
return *this;
}
constexpr LargeFlags operator^(const LargeFlags& other) const {
return {static_cast<decltype(bits)>(this->bits ^ other.bits)};
return LargeFlags { static_cast<decltype(bits)>(this->bits ^ other.bits) };
}
LargeFlags& operator^=(const LargeFlags& other) {
*this = (*this ^ other);
Expand Down

0 comments on commit 80da604

Please sign in to comment.