blob: c730be65f749473bf1943d14bca0334fcc6bab78 [file] [log] [blame]
agl@chromium.org42bbb992009-02-12 03:59:20 +09001// 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
rvargas@chromium.org5710e6d2014-03-15 09:09:05 +09008#include "base/files/file.h"
9
agl@chromium.org42bbb992009-02-12 03:59:20 +090010namespace 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.
agl@chromium.orgb208b522009-03-06 06:40:11 +090015//
16// WARNING: (Chromium only) There are subtleties to consider if serialising
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090017// these objects over IPC. See comments in ipc/ipc_message_utils.h
agl@chromium.org9f966822009-04-03 11:29:45 +090018// above the template specialisation for this structure.
agl@chromium.org42bbb992009-02-12 03:59:20 +090019// -----------------------------------------------------------------------------
20struct FileDescriptor {
rvargas@chromium.org5710e6d2014-03-15 09:09:05 +090021 FileDescriptor() : fd(-1), auto_close(false) {}
agl@chromium.org42bbb992009-02-12 03:59:20 +090022
rvargas@chromium.org5710e6d2014-03-15 09:09:05 +090023 FileDescriptor(int ifd, bool iauto_close) : fd(ifd), auto_close(iauto_close) {
24 }
25
26 FileDescriptor(File file) : fd(file.TakePlatformFile()), auto_close(true) {}
agl@chromium.org42bbb992009-02-12 03:59:20 +090027
gspencer@chromium.orga3d12be2010-04-29 02:26:49 +090028 bool operator==(const FileDescriptor& other) const {
29 return (fd == other.fd && auto_close == other.auto_close);
30 }
31
viettrungluu@chromium.org6c4960b2014-03-19 23:50:21 +090032 bool operator!=(const FileDescriptor& other) const {
33 return !operator==(other);
34 }
35
gspencer@chromium.orga3d12be2010-04-29 02:26:49 +090036 // 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
agl@chromium.org42bbb992009-02-12 03:59:20 +090041 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_