Put moved from AttributedStringBox into consistent state

Summary:
Changelog: [internal]

Fixes an inconsistency that `AttributedStringBox` can get into.

Example of inconsitency:
After `AttributedStringBox` is moved (move constructor or move assignment operator), moved from `AttributedStringBox` needs to be set into blank state. Its mode needs to be `Value`, `opaquePointer_` should be nullptr and `value_` empty AttributedString. This was not the case before as the default move constructor and operator would leave `mode_` as `OpaquePointer` but ivar representing opaquePointer would be nullptr.

Reviewed By: JoshuaGross

Differential Revision: D26168142

fbshipit-source-id: eed2a7c3a165ae5e1f269822c12042c6ccbd3388
This commit is contained in:
Samuel Susla
2021-01-31 15:10:58 -08:00
committed by Facebook GitHub Bot
parent 9d4096b296
commit dce1863bf9
2 changed files with 24 additions and 4 deletions
@@ -24,6 +24,14 @@ AttributedStringBox::AttributedStringBox(
std::shared_ptr<void> const &opaquePointer)
: mode_(Mode::OpaquePointer), value_({}), opaquePointer_(opaquePointer) {}
AttributedStringBox::AttributedStringBox(AttributedStringBox &&other) noexcept
: mode_(other.mode_),
value_(std::move(other.value_)),
opaquePointer_(std::move(other.opaquePointer_)) {
other.mode_ = AttributedStringBox::Mode::Value;
other.value_ = std::make_shared<AttributedString const>(AttributedString{});
}
AttributedStringBox::Mode AttributedStringBox::getMode() const {
return mode_;
}
@@ -40,6 +48,18 @@ std::shared_ptr<void> AttributedStringBox::getOpaquePointer() const {
return opaquePointer_;
}
AttributedStringBox &AttributedStringBox::operator=(
AttributedStringBox &&other) {
if (this != &other) {
mode_ = other.mode_;
value_ = std::move(other.value_);
opaquePointer_ = std::move(other.opaquePointer_);
other.mode_ = AttributedStringBox::Mode::Value;
other.value_ = std::make_shared<AttributedString const>(AttributedString{});
}
return *this;
}
bool operator==(
AttributedStringBox const &lhs,
AttributedStringBox const &rhs) {
@@ -48,9 +68,9 @@ bool operator==(
}
switch (lhs.getMode()) {
case facebook::react::AttributedStringBox::Mode::Value:
case AttributedStringBox::Mode::Value:
return lhs.getValue() == rhs.getValue();
case facebook::react::AttributedStringBox::Mode::OpaquePointer:
case AttributedStringBox::Mode::OpaquePointer:
return lhs.getOpaquePointer() == rhs.getOpaquePointer();
}
}