Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(369)

Side by Side Diff: dbus/file_descriptor.cc

Issue 1170283005: Make dbus::FileDescriptor a Pass()-movable type. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Simplify by swapping values. Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « dbus/file_descriptor.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <algorithm>
6
5 #include "base/bind.h" 7 #include "base/bind.h"
6 #include "base/files/file.h" 8 #include "base/files/file.h"
7 #include "base/location.h" 9 #include "base/location.h"
8 #include "base/logging.h" 10 #include "base/logging.h"
9 #include "base/threading/worker_pool.h" 11 #include "base/threading/worker_pool.h"
10 #include "dbus/file_descriptor.h" 12 #include "dbus/file_descriptor.h"
11 13
14 using std::swap;
15
12 namespace dbus { 16 namespace dbus {
13 17
14 void CHROME_DBUS_EXPORT FileDescriptor::Deleter::operator()( 18 void CHROME_DBUS_EXPORT FileDescriptor::Deleter::operator()(
15 FileDescriptor* fd) { 19 FileDescriptor* fd) {
16 base::WorkerPool::PostTask( 20 base::WorkerPool::PostTask(
17 FROM_HERE, base::Bind(&base::DeletePointer<FileDescriptor>, fd), false); 21 FROM_HERE, base::Bind(&base::DeletePointer<FileDescriptor>, fd), false);
18 } 22 }
19 23
24 FileDescriptor::FileDescriptor(RValue other)
25 : value_(-1), owner_(false), valid_(false) {
26 Swap(other.object);
27 }
28
20 FileDescriptor::~FileDescriptor() { 29 FileDescriptor::~FileDescriptor() {
21 if (owner_) 30 if (owner_)
22 base::File auto_closer(value_); 31 base::File auto_closer(value_);
23 } 32 }
24 33
34 FileDescriptor& FileDescriptor::operator=(RValue other) {
35 Swap(other.object);
36 return *this;
37 }
38
25 int FileDescriptor::value() const { 39 int FileDescriptor::value() const {
26 CHECK(valid_); 40 CHECK(valid_);
27 return value_; 41 return value_;
28 } 42 }
29 43
30 int FileDescriptor::TakeValue() { 44 int FileDescriptor::TakeValue() {
31 CHECK(valid_); // NB: check first so owner_ is unchanged if this triggers 45 CHECK(valid_); // NB: check first so owner_ is unchanged if this triggers
32 owner_ = false; 46 owner_ = false;
33 return value_; 47 return value_;
34 } 48 }
35 49
36 void FileDescriptor::CheckValidity() { 50 void FileDescriptor::CheckValidity() {
37 base::File file(value_); 51 base::File file(value_);
38 base::File::Info info; 52 base::File::Info info;
39 bool ok = file.GetInfo(&info); 53 bool ok = file.GetInfo(&info);
40 file.TakePlatformFile(); // Prevent |value_| from being closed by |file|. 54 file.TakePlatformFile(); // Prevent |value_| from being closed by |file|.
41 valid_ = (ok && !info.is_directory); 55 valid_ = (ok && !info.is_directory);
42 } 56 }
43 57
58 void FileDescriptor::Swap(FileDescriptor* other) {
59 swap(value_, other->value_);
60 swap(owner_, other->owner_);
61 swap(valid_, other->valid_);
62 }
63
44 } // namespace dbus 64 } // namespace dbus
OLDNEW
« no previous file with comments | « dbus/file_descriptor.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698