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