[email protected] | 5fe733de | 2009-02-11 18:59:20 | [diff] [blame] | 1 | // Copyright (c) 2006-2009 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 BASE_FILE_DESCRIPTOR_POSIX_H_ | ||||
6 | #define BASE_FILE_DESCRIPTOR_POSIX_H_ | ||||
7 | |||||
[email protected] | 0daaebfe | 2014-03-15 00:09:05 | [diff] [blame] | 8 | #include "base/files/file.h" |
morrita | a409ccc | 2014-10-20 23:53:25 | [diff] [blame^] | 9 | #include "base/files/scoped_file.h" |
[email protected] | 0daaebfe | 2014-03-15 00:09:05 | [diff] [blame] | 10 | |
[email protected] | 5fe733de | 2009-02-11 18:59:20 | [diff] [blame] | 11 | namespace base { |
12 | |||||
13 | // ----------------------------------------------------------------------------- | ||||
14 | // We introduct a special structure for file descriptors in order that we are | ||||
15 | // able to use template specialisation to special-case their handling. | ||||
[email protected] | 2749885f | 2009-03-05 21:40:11 | [diff] [blame] | 16 | // |
17 | // WARNING: (Chromium only) There are subtleties to consider if serialising | ||||
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 18 | // these objects over IPC. See comments in ipc/ipc_message_utils.h |
[email protected] | 82e5ee8 | 2009-04-03 02:29:45 | [diff] [blame] | 19 | // above the template specialisation for this structure. |
[email protected] | 5fe733de | 2009-02-11 18:59:20 | [diff] [blame] | 20 | // ----------------------------------------------------------------------------- |
21 | struct FileDescriptor { | ||||
[email protected] | 0daaebfe | 2014-03-15 00:09:05 | [diff] [blame] | 22 | FileDescriptor() : fd(-1), auto_close(false) {} |
[email protected] | 5fe733de | 2009-02-11 18:59:20 | [diff] [blame] | 23 | |
[email protected] | 0daaebfe | 2014-03-15 00:09:05 | [diff] [blame] | 24 | FileDescriptor(int ifd, bool iauto_close) : fd(ifd), auto_close(iauto_close) { |
25 | } | ||||
26 | |||||
27 | FileDescriptor(File file) : fd(file.TakePlatformFile()), auto_close(true) {} | ||||
morrita | a409ccc | 2014-10-20 23:53:25 | [diff] [blame^] | 28 | explicit FileDescriptor(ScopedFD fd) : fd(fd.release()), auto_close(true) {} |
[email protected] | 5fe733de | 2009-02-11 18:59:20 | [diff] [blame] | 29 | |
[email protected] | d65adb1 | 2010-04-28 17:26:49 | [diff] [blame] | 30 | bool operator==(const FileDescriptor& other) const { |
31 | return (fd == other.fd && auto_close == other.auto_close); | ||||
32 | } | ||||
33 | |||||
[email protected] | af58f48 | 2014-03-19 14:50:21 | [diff] [blame] | 34 | bool operator!=(const FileDescriptor& other) const { |
35 | return !operator==(other); | ||||
36 | } | ||||
37 | |||||
[email protected] | d65adb1 | 2010-04-28 17:26:49 | [diff] [blame] | 38 | // A comparison operator so that we can use these as keys in a std::map. |
39 | bool operator<(const FileDescriptor& other) const { | ||||
40 | return other.fd < fd; | ||||
41 | } | ||||
42 | |||||
[email protected] | 5fe733de | 2009-02-11 18:59:20 | [diff] [blame] | 43 | int fd; |
44 | // If true, this file descriptor should be closed after it has been used. For | ||||
45 | // example an IPC system might interpret this flag as indicating that the | ||||
46 | // file descriptor it has been given should be closed after use. | ||||
47 | bool auto_close; | ||||
48 | }; | ||||
49 | |||||
50 | } // namespace base | ||||
51 | |||||
52 | #endif // BASE_FILE_DESCRIPTOR_POSIX_H_ |