file_descriptor_posix.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. #ifndef BUTIL_FILE_DESCRIPTOR_POSIX_H_
  5. #define BUTIL_FILE_DESCRIPTOR_POSIX_H_
  6. #include "butil/files/file.h"
  7. namespace butil {
  8. // -----------------------------------------------------------------------------
  9. // We introduct a special structure for file descriptors in order that we are
  10. // able to use template specialisation to special-case their handling.
  11. //
  12. // WARNING: (Chromium only) There are subtleties to consider if serialising
  13. // these objects over IPC. See comments in ipc/ipc_message_utils.h
  14. // above the template specialisation for this structure.
  15. // -----------------------------------------------------------------------------
  16. struct FileDescriptor {
  17. FileDescriptor() : fd(-1), auto_close(false) {}
  18. FileDescriptor(int ifd, bool iauto_close) : fd(ifd), auto_close(iauto_close) {
  19. }
  20. FileDescriptor(File file) : fd(file.TakePlatformFile()), auto_close(true) {}
  21. bool operator==(const FileDescriptor& other) const {
  22. return (fd == other.fd && auto_close == other.auto_close);
  23. }
  24. bool operator!=(const FileDescriptor& other) const {
  25. return !operator==(other);
  26. }
  27. // A comparison operator so that we can use these as keys in a std::map.
  28. bool operator<(const FileDescriptor& other) const {
  29. return other.fd < fd;
  30. }
  31. int fd;
  32. // If true, this file descriptor should be closed after it has been used. For
  33. // example an IPC system might interpret this flag as indicating that the
  34. // file descriptor it has been given should be closed after use.
  35. bool auto_close;
  36. };
  37. } // namespace butil
  38. #endif // BUTIL_FILE_DESCRIPTOR_POSIX_H_