blob: c730be65f749473bf1943d14bca0334fcc6bab78 [file] [log] [blame]
[email protected]5fe733de2009-02-11 18:59:201// 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]0daaebfe2014-03-15 00:09:058#include "base/files/file.h"
9
[email protected]5fe733de2009-02-11 18:59:2010namespace base {
11
12// -----------------------------------------------------------------------------
13// We introduct a special structure for file descriptors in order that we are
14// able to use template specialisation to special-case their handling.
[email protected]2749885f2009-03-05 21:40:1115//
16// WARNING: (Chromium only) There are subtleties to consider if serialising
[email protected]946d1b22009-07-22 23:57:2117// these objects over IPC. See comments in ipc/ipc_message_utils.h
[email protected]82e5ee82009-04-03 02:29:4518// above the template specialisation for this structure.
[email protected]5fe733de2009-02-11 18:59:2019// -----------------------------------------------------------------------------
20struct FileDescriptor {
[email protected]0daaebfe2014-03-15 00:09:0521 FileDescriptor() : fd(-1), auto_close(false) {}
[email protected]5fe733de2009-02-11 18:59:2022
[email protected]0daaebfe2014-03-15 00:09:0523 FileDescriptor(int ifd, bool iauto_close) : fd(ifd), auto_close(iauto_close) {
24 }
25
26 FileDescriptor(File file) : fd(file.TakePlatformFile()), auto_close(true) {}
[email protected]5fe733de2009-02-11 18:59:2027
[email protected]d65adb12010-04-28 17:26:4928 bool operator==(const FileDescriptor& other) const {
29 return (fd == other.fd && auto_close == other.auto_close);
30 }
31
[email protected]af58f482014-03-19 14:50:2132 bool operator!=(const FileDescriptor& other) const {
33 return !operator==(other);
34 }
35
[email protected]d65adb12010-04-28 17:26:4936 // A comparison operator so that we can use these as keys in a std::map.
37 bool operator<(const FileDescriptor& other) const {
38 return other.fd < fd;
39 }
40
[email protected]5fe733de2009-02-11 18:59:2041 int fd;
42 // If true, this file descriptor should be closed after it has been used. For
43 // example an IPC system might interpret this flag as indicating that the
44 // file descriptor it has been given should be closed after use.
45 bool auto_close;
46};
47
48} // namespace base
49
50#endif // BASE_FILE_DESCRIPTOR_POSIX_H_