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:
danilchap
2016-10-17 07:16:54 -07:00
committed by Commit bot
parent 9b9910dda7
commit c4fd23cdda
2 changed files with 36 additions and 8 deletions

View File

@ -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_; }

View File

@ -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();
{