Add ability to return moved value from FunctorMessageHandler, Optional.
This functionality is desired for this CL: https://codereview.webrtc.org/2675173003/ BUG=None Review-Url: https://codereview.webrtc.org/2681283002 Cr-Commit-Position: refs/heads/master@{#16546}
This commit is contained in:
@ -45,25 +45,15 @@ class FunctorMessageHandler : public MessageHandler {
|
||||
}
|
||||
const ReturnT& result() const { return result_; }
|
||||
|
||||
// Returns moved result. Should not call result() or MoveResult() again
|
||||
// after this.
|
||||
ReturnT MoveResult() { return std::move(result_); }
|
||||
|
||||
private:
|
||||
FunctorT functor_;
|
||||
ReturnT result_;
|
||||
};
|
||||
|
||||
// Specialization for std::unique_ptr<ReturnT>.
|
||||
template <class ReturnT, class FunctorT>
|
||||
class FunctorMessageHandler<class std::unique_ptr<ReturnT>, FunctorT>
|
||||
: public MessageHandler {
|
||||
public:
|
||||
explicit FunctorMessageHandler(const FunctorT& functor) : functor_(functor) {}
|
||||
virtual void OnMessage(Message* msg) { result_ = std::move(functor_()); }
|
||||
std::unique_ptr<ReturnT> result() { return std::move(result_); }
|
||||
|
||||
private:
|
||||
FunctorT functor_;
|
||||
std::unique_ptr<ReturnT> result_;
|
||||
};
|
||||
|
||||
// Specialization for ReturnT of void.
|
||||
template <class FunctorT>
|
||||
class FunctorMessageHandler<void, FunctorT> : public MessageHandler {
|
||||
@ -74,6 +64,7 @@ class FunctorMessageHandler<void, FunctorT> : public MessageHandler {
|
||||
functor_();
|
||||
}
|
||||
void result() const {}
|
||||
void MoveResult() {}
|
||||
|
||||
private:
|
||||
FunctorT functor_;
|
||||
|
||||
@ -239,6 +239,12 @@ class Optional final {
|
||||
: default_val;
|
||||
}
|
||||
|
||||
// Dereference and move value.
|
||||
T MoveValue() {
|
||||
RTC_DCHECK(has_value_);
|
||||
return std::move(value_);
|
||||
}
|
||||
|
||||
// Equality tests. Two Optionals are equal if they contain equivalent values,
|
||||
// or if they're both empty.
|
||||
friend bool operator==(const Optional& m1, const Optional& m2) {
|
||||
|
||||
@ -725,4 +725,19 @@ TEST(OptionalTest, TestSwap) {
|
||||
*log);
|
||||
}
|
||||
|
||||
TEST(OptionalTest, TestMoveValue) {
|
||||
auto log = Logger::Setup();
|
||||
{
|
||||
Optional<Logger> x(Logger(42));
|
||||
log->push_back("---");
|
||||
Logger moved = x.MoveValue();
|
||||
log->push_back("---");
|
||||
}
|
||||
EXPECT_EQ(
|
||||
V("0:42. explicit constructor", "1:42. move constructor (from 0:42)",
|
||||
"0:42. destructor", "---", "2:42. move constructor (from 1:42)", "---",
|
||||
"2:42. destructor", "1:42. destructor"),
|
||||
*log);
|
||||
}
|
||||
|
||||
} // namespace rtc
|
||||
|
||||
@ -168,7 +168,7 @@ class LOCKABLE Thread : public MessageQueue {
|
||||
ReturnT Invoke(const Location& posted_from, const FunctorT& functor) {
|
||||
FunctorMessageHandler<ReturnT, FunctorT> handler(functor);
|
||||
InvokeInternal(posted_from, &handler);
|
||||
return handler.result();
|
||||
return handler.MoveResult();
|
||||
}
|
||||
|
||||
// From MessageQueue
|
||||
|
||||
Reference in New Issue
Block a user