Commit 21476d05 authored by Lei Zhang's avatar Lei Zhang Committed by Commit Bot

Use unique_ptrs in MessagePumpLibevent::FileDescriptorWatcher.

Change-Id: I14e783b5adb905cf27731c55e224f7b0311e2b8e
Reviewed-on: https://chromium-review.googlesource.com/936010Reviewed-by: default avatarBen Chan <benchan@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
Cr-Commit-Position: refs/heads/master@{#546022}
parent 5a9bcc28
......@@ -7,7 +7,7 @@
#include <errno.h>
#include <unistd.h>
#include <memory>
#include <utility>
#include "base/auto_reset.h"
#include "base/compiler_specific.h"
......@@ -45,11 +45,7 @@ namespace base {
MessagePumpLibevent::FileDescriptorWatcher::FileDescriptorWatcher(
const Location& from_here)
: event_(nullptr),
pump_(nullptr),
watcher_(nullptr),
was_destroyed_(nullptr),
created_from_location_(from_here) {}
: created_from_location_(from_here) {}
MessagePumpLibevent::FileDescriptorWatcher::~FileDescriptorWatcher() {
if (event_) {
......@@ -62,29 +58,28 @@ MessagePumpLibevent::FileDescriptorWatcher::~FileDescriptorWatcher() {
}
bool MessagePumpLibevent::FileDescriptorWatcher::StopWatchingFileDescriptor() {
event* e = ReleaseEvent();
if (e == nullptr)
std::unique_ptr<event> e = ReleaseEvent();
if (!e)
return true;
// event_del() is a no-op if the event isn't active.
int rv = event_del(e);
delete e;
int rv = event_del(e.get());
pump_ = nullptr;
watcher_ = nullptr;
return (rv == 0);
}
void MessagePumpLibevent::FileDescriptorWatcher::Init(event* e) {
void MessagePumpLibevent::FileDescriptorWatcher::Init(
std::unique_ptr<event> e) {
DCHECK(e);
DCHECK(!event_);
event_ = e;
event_ = std::move(e);
}
event* MessagePumpLibevent::FileDescriptorWatcher::ReleaseEvent() {
struct event* e = event_;
event_ = nullptr;
return e;
std::unique_ptr<event>
MessagePumpLibevent::FileDescriptorWatcher::ReleaseEvent() {
return std::move(event_);
}
void MessagePumpLibevent::FileDescriptorWatcher::OnFileCanReadWithoutBlocking(
......@@ -189,12 +184,9 @@ bool MessagePumpLibevent::WatchFileDescriptor(int fd,
return false;
}
// Transfer ownership of evt to controller.
controller->Init(evt.release());
controller->Init(std::move(evt));
controller->set_watcher(delegate);
controller->set_pump(this);
return true;
}
......
......@@ -5,6 +5,8 @@
#ifndef BASE_MESSAGE_LOOP_MESSAGE_PUMP_LIBEVENT_H_
#define BASE_MESSAGE_LOOP_MESSAGE_PUMP_LIBEVENT_H_
#include <memory>
#include "base/compiler_specific.h"
#include "base/location.h"
#include "base/macros.h"
......@@ -54,12 +56,11 @@ class BASE_EXPORT MessagePumpLibevent : public MessagePump {
friend class MessagePumpLibevent;
friend class MessagePumpLibeventTest;
// Called by MessagePumpLibevent, ownership of |e| is transferred to this
// object.
void Init(event* e);
// Called by MessagePumpLibevent.
void Init(std::unique_ptr<event> e);
// Used by MessagePumpLibevent to take ownership of event_.
event* ReleaseEvent();
// Used by MessagePumpLibevent to take ownership of |event_|.
std::unique_ptr<event> ReleaseEvent();
void set_pump(MessagePumpLibevent* pump) { pump_ = pump; }
MessagePumpLibevent* pump() const { return pump_; }
......@@ -69,12 +70,12 @@ class BASE_EXPORT MessagePumpLibevent : public MessagePump {
void OnFileCanReadWithoutBlocking(int fd, MessagePumpLibevent* pump);
void OnFileCanWriteWithoutBlocking(int fd, MessagePumpLibevent* pump);
event* event_;
MessagePumpLibevent* pump_;
Watcher* watcher_;
std::unique_ptr<event> event_;
MessagePumpLibevent* pump_ = nullptr;
Watcher* watcher_ = nullptr;
// If this pointer is non-NULL, the pointee is set to true in the
// destructor.
bool* was_destroyed_;
bool* was_destroyed_ = nullptr;
const Location created_from_location_;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment