Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef DBUS_FILE_DESCRIPTOR_H_ | |
| 6 #define DBUS_FILE_DESCRIPTOR_H_ | |
| 7 #pragma once | |
| 8 | |
| 9 namespace dbus { | |
| 10 | |
| 11 // FileDescriptor is a type used to encapsulate D-Bus file descriptors | |
| 12 // and to follow the RAII idiom appropiate for use with message operations | |
| 13 // where the descriptor might be easily leaked. To guard against this the | |
| 14 // descriptor is closed when an instance is destroyed if it is owned. | |
| 15 // Ownership is asserted only when PutValue is used and TakeValue can be | |
| 16 // used to take ownership. | |
| 17 // | |
| 18 // For example, in the following | |
| 19 // FileDescriptor fd; | |
| 20 // if (!reader->PopString(&name) || | |
| 21 // !reader->PopFileDescriptor(&fd) || | |
| 22 // !reader->PopUint32(&flags)) { | |
| 23 // the descriptor in fd will be closed if the PopUint32 fails. But | |
| 24 // writer.AppendFileDescriptor(dbus::FileDescriptor(1)); | |
| 25 // will not automatically close "1" because it is not owned. | |
|
satorux1
2012/03/28 20:49:37
Oh I see. This comment explains why owner_ is need
| |
| 26 class FileDescriptor { | |
| 27 public: | |
| 28 // Permits initialization without a value for passing to | |
| 29 // dbus::MessageReader::PopFileDescriptor to fill in and from int values. | |
| 30 FileDescriptor() : value_(-1), owner_(false) {} | |
| 31 explicit FileDescriptor(int value) : value_(value), owner_(false) {} | |
| 32 | |
| 33 virtual ~FileDescriptor(); | |
| 34 | |
| 35 // Retrieves value as an int without affecting ownership. | |
| 36 int value() const { return value_; } | |
| 37 | |
| 38 // Sets the value and assign ownership. | |
| 39 void PutValue(int value) { | |
|
satorux1
2012/03/28 20:49:37
I found it confusing. For this, TakeValue() sounds
satorux1
2012/03/29 02:46:48
This question wasn't answered. I wanted it to be f
| |
| 40 value_ = value; | |
| 41 owner_ = true; | |
| 42 } | |
| 43 | |
| 44 // Takes the value and ownership. | |
| 45 int TakeValue() { | |
| 46 owner_ = false; | |
| 47 return value_; | |
| 48 } | |
| 49 | |
| 50 private: | |
| 51 int value_; | |
| 52 bool owner_; | |
| 53 | |
| 54 DISALLOW_COPY_AND_ASSIGN(FileDescriptor); | |
| 55 }; | |
| 56 | |
| 57 } // namespace dbus | |
| 58 | |
| 59 #endif // DBUS_FILE_DESCRIPTOR_H_ | |
| OLD | NEW |