Files
react-native/ReactCommon/react/renderer/attributedstring/AttributedStringBox.cpp
T
Samuel Susla dce1863bf9 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
2021-01-31 15:10:58 -08:00

86 lines
2.4 KiB
C++

/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "AttributedStringBox.h"
namespace facebook {
namespace react {
AttributedStringBox::AttributedStringBox()
: mode_(Mode::Value),
value_(std::make_shared<AttributedString const>(AttributedString{})),
opaquePointer_({}){};
AttributedStringBox::AttributedStringBox(AttributedString const &value)
: mode_(Mode::Value),
value_(std::make_shared<AttributedString const>(value)),
opaquePointer_({}){};
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_;
}
AttributedString const &AttributedStringBox::getValue() const {
assert(mode_ == AttributedStringBox::Mode::Value);
assert(value_);
return *value_;
}
std::shared_ptr<void> AttributedStringBox::getOpaquePointer() const {
assert(mode_ == AttributedStringBox::Mode::OpaquePointer);
assert(opaquePointer_);
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) {
if (lhs.getMode() != rhs.getMode()) {
return false;
}
switch (lhs.getMode()) {
case AttributedStringBox::Mode::Value:
return lhs.getValue() == rhs.getValue();
case AttributedStringBox::Mode::OpaquePointer:
return lhs.getOpaquePointer() == rhs.getOpaquePointer();
}
}
bool operator!=(
AttributedStringBox const &lhs,
AttributedStringBox const &rhs) {
return !(lhs == rhs);
}
} // namespace react
} // namespace facebook