| 6 | #define BASE_FILES_SCOPED_FILE_H_ |
| 7 |
| 8 | #include <stdio.h> |
| 9 |
| 10 | #include <memory> |
| 11 |
| 12 | #include "base/base_export.h" |
| 13 | #include "base/logging.h" |
| 14 | #include "base/scoped_generic.h" |
| 15 | #include "build/build_config.h" |
| 16 |
| 17 | namespace base { |
| 18 |
| 19 | namespace internal { |
| 20 |
| 21 | #if defined(OS_ANDROID) |
| 22 | // Use fdsan on android. |
| 23 | struct BASE_EXPORT ScopedFDCloseTraits : public ScopedGenericOwnershipTracking { |
| 24 | static int InvalidValue() { return -1; } |
| 25 | static void Free(int); |
| 26 | static void Acquire(const ScopedGeneric<int, ScopedFDCloseTraits>&, int); |
| 27 | static void Release(const ScopedGeneric<int, ScopedFDCloseTraits>&, int); |
| 28 | }; |
| 29 | #elif defined(OS_POSIX) || defined(OS_FUCHSIA) |
| 30 | struct BASE_EXPORT ScopedFDCloseTraits { |
| 31 | static int InvalidValue() { |
| 32 | return -1; |
| 33 | } |
| 34 | static void Free(int fd); |
| 35 | }; |
| 36 | #endif |
| 37 |
| 38 | // Functor for |ScopedFILE| (below). |
| 39 | struct ScopedFILECloser { |
| 40 | inline void operator()(FILE* x) const { |
| 41 | if (x) |
| 42 | fclose(x); |
| 43 | } |
| 44 | }; |
| 45 |
| 46 | } // namespace internal |
| 47 |
| 48 | // ----------------------------------------------------------------------------- |
| 49 |
| 50 | #if defined(OS_POSIX) || defined(OS_FUCHSIA) |
| 51 | // A low-level Posix file descriptor closer class. Use this when writing |
| 52 | // platform-specific code, especially that does non-file-like things with the |
| 53 | // FD (like sockets). |
| 54 | // |
| 55 | // If you're writing low-level Windows code, see base/win/scoped_handle.h |
| 56 | // which provides some additional functionality. |
| 57 | // |
| 58 | // If you're writing cross-platform code that deals with actual files, you |
| 59 | // should generally use base::File instead which can be constructed with a |
| 60 | // handle, and in addition to handling ownership, has convenient cross-platform |
| 61 | // file manipulation functions on it. |
| 62 | typedef ScopedGeneric<int, internal::ScopedFDCloseTraits> ScopedFD; |
| 63 | #endif |
| 64 |
| 65 | // Automatically closes |FILE*|s. |
| 66 | typedef std::unique_ptr<FILE, internal::ScopedFILECloser> ScopedFILE; |
| 67 |
| 68 | } // namespace base |
| 69 |
| 70 | #endif // BASE_FILES_SCOPED_FILE_H_ |
| 71 |