| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef BASE_FILES_SCOPED_FILE_H_ | 5 #ifndef BASE_FILES_SCOPED_FILE_H_ |
| 6 #define BASE_FILES_SCOPED_FILE_H_ | 6 #define BASE_FILES_SCOPED_FILE_H_ |
| 7 | 7 |
| 8 #include <stdio.h> | 8 #include <stdio.h> |
| 9 | 9 |
| 10 #include "base/base_export.h" | 10 #include "base/base_export.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/scoped_generic.h" | 13 #include "base/scoped_generic.h" |
| 13 #include "build/build_config.h" | 14 #include "build/build_config.h" |
| 14 | 15 |
| 15 namespace base { | 16 namespace base { |
| 16 | 17 |
| 17 namespace internal { | 18 namespace internal { |
| 18 | 19 |
| 19 #if defined(OS_POSIX) | 20 #if defined(OS_POSIX) |
| 20 struct BASE_EXPORT ScopedFDCloseTraits { | 21 struct BASE_EXPORT ScopedFDCloseTraits { |
| 21 static int InvalidValue() { | 22 static int InvalidValue() { |
| 22 return -1; | 23 return -1; |
| 23 } | 24 } |
| 24 static void Free(int fd); | 25 static void Free(int fd); |
| 25 }; | 26 }; |
| 26 #endif | 27 #endif |
| 27 | 28 |
| 29 // Functor for |ScopedFILE| (below). |
| 30 struct ScopedFILECloser { |
| 31 inline void operator()(FILE* x) const { |
| 32 if (x) |
| 33 fclose(x); |
| 34 } |
| 35 }; |
| 36 |
| 28 } // namespace internal | 37 } // namespace internal |
| 29 | 38 |
| 39 // ----------------------------------------------------------------------------- |
| 40 |
| 30 #if defined(OS_POSIX) | 41 #if defined(OS_POSIX) |
| 31 // A low-level Posix file descriptor closer class. Use this when writing | 42 // A low-level Posix file descriptor closer class. Use this when writing |
| 32 // platform-specific code, especially that does non-file-like things with the | 43 // platform-specific code, especially that does non-file-like things with the |
| 33 // FD (like sockets). | 44 // FD (like sockets). |
| 34 // | 45 // |
| 35 // If you're writing low-level Windows code, see base/win/scoped_handle.h | 46 // If you're writing low-level Windows code, see base/win/scoped_handle.h |
| 36 // which provides some additional functionality. | 47 // which provides some additional functionality. |
| 37 // | 48 // |
| 38 // If you're writing cross-platform code that deals with actual files, you | 49 // If you're writing cross-platform code that deals with actual files, you |
| 39 // should generally use base::File instead which can be constructed with a | 50 // should generally use base::File instead which can be constructed with a |
| 40 // handle, and in addition to handling ownership, has convenient cross-platform | 51 // handle, and in addition to handling ownership, has convenient cross-platform |
| 41 // file manipulation functions on it. | 52 // file manipulation functions on it. |
| 42 typedef ScopedGeneric<int, internal::ScopedFDCloseTraits> ScopedFD; | 53 typedef ScopedGeneric<int, internal::ScopedFDCloseTraits> ScopedFD; |
| 43 #endif | 54 #endif |
| 44 | 55 |
| 56 // Automatically closes |FILE*|s. |
| 57 typedef scoped_ptr<FILE, internal::ScopedFILECloser> ScopedFILE; |
| 58 |
| 45 } // namespace base | 59 } // namespace base |
| 46 | 60 |
| 47 #endif // BASE_FILES_SCOPED_FILE_H_ | 61 #endif // BASE_FILES_SCOPED_FILE_H_ |
| OLD | NEW |