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

Side by Side Diff: dbus/file_descriptor.h

Issue 9700072: dbus: add support for passing file descriptors (Closed) Base URL: http://git.chromium.org/git/chromium/src@master
Patch Set: unit test nits Created 8 years, 8 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/dbus.gyp ('k') | dbus/file_descriptor.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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.
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) {
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_
OLDNEW
« no previous file with comments | « dbus/dbus.gyp ('k') | dbus/file_descriptor.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698