Add rtc::Optional::reset
BUG=None Review-Url: https://codereview.webrtc.org/2426473004 Cr-Commit-Position: refs/heads/master@{#14651}
This commit is contained in:
@ -132,10 +132,8 @@ class Optional final {
|
||||
new (&value_) T(m.value_); // T's copy constructor.
|
||||
has_value_ = true;
|
||||
}
|
||||
} else if (has_value_) {
|
||||
value_.~T();
|
||||
has_value_ = false;
|
||||
PoisonValue();
|
||||
} else {
|
||||
reset();
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
@ -152,10 +150,8 @@ class Optional final {
|
||||
new (&value_) T(std::move(m.value_)); // T's move constructor.
|
||||
has_value_ = true;
|
||||
}
|
||||
} else if (has_value_) {
|
||||
value_.~T();
|
||||
has_value_ = false;
|
||||
PoisonValue();
|
||||
} else {
|
||||
reset();
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
@ -188,6 +184,15 @@ class Optional final {
|
||||
}
|
||||
}
|
||||
|
||||
// Destroy any contained value. Has no effect if we have no value.
|
||||
void reset() {
|
||||
if (!has_value_)
|
||||
return;
|
||||
value_.~T();
|
||||
has_value_ = false;
|
||||
PoisonValue();
|
||||
}
|
||||
|
||||
// Conversion to bool to test if we have a value.
|
||||
explicit operator bool() const { return has_value_; }
|
||||
|
||||
|
||||
@ -369,6 +369,29 @@ TEST(OptionalTest, TestMoveAssignToFullFromT) {
|
||||
*log);
|
||||
}
|
||||
|
||||
TEST(OptionalTest, TestResetEmpty) {
|
||||
auto log = Logger::Setup();
|
||||
{
|
||||
Optional<Logger> x;
|
||||
x.reset();
|
||||
}
|
||||
EXPECT_EQ(V(), *log);
|
||||
}
|
||||
|
||||
TEST(OptionalTest, TestResetFull) {
|
||||
auto log = Logger::Setup();
|
||||
{
|
||||
Optional<Logger> x(Logger(17));
|
||||
log->push_back("---");
|
||||
x.reset();
|
||||
log->push_back("---");
|
||||
}
|
||||
EXPECT_EQ(
|
||||
V("0:17. explicit constructor", "1:17. move constructor (from 0:17)",
|
||||
"0:17. destructor", "---", "1:17. destructor", "---"),
|
||||
*log);
|
||||
}
|
||||
|
||||
TEST(OptionalTest, TestDereference) {
|
||||
auto log = Logger::Setup();
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user