1 | // Copyright 2013 The Flutter 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 FLUTTER_FML_UNIQUE_FD_H_ |
6 | #define FLUTTER_FML_UNIQUE_FD_H_ |
7 | |
8 | #include "flutter/fml/build_config.h" |
9 | #include "flutter/fml/unique_object.h" |
10 | |
11 | #if OS_WIN |
12 | |
13 | #include <windows.h> |
14 | |
15 | #else // OS_WIN |
16 | |
17 | #include <dirent.h> |
18 | #include <unistd.h> |
19 | |
20 | #endif // OS_WIN |
21 | |
22 | namespace fml { |
23 | namespace internal { |
24 | |
25 | #if OS_WIN |
26 | |
27 | namespace os_win { |
28 | |
29 | struct UniqueFDTraits { |
30 | static HANDLE InvalidValue() { return INVALID_HANDLE_VALUE; } |
31 | static bool IsValid(HANDLE value) { return value != InvalidValue(); } |
32 | static void Free(HANDLE fd); |
33 | }; |
34 | |
35 | } // namespace os_win |
36 | |
37 | #else // OS_WIN |
38 | |
39 | namespace os_unix { |
40 | |
41 | struct UniqueFDTraits { |
42 | static int InvalidValue() { return -1; } |
43 | static bool IsValid(int value) { return value >= 0; } |
44 | static void Free(int fd); |
45 | }; |
46 | |
47 | struct UniqueDirTraits { |
48 | static DIR* InvalidValue() { return nullptr; } |
49 | static bool IsValid(DIR* value) { return value != nullptr; } |
50 | static void Free(DIR* dir); |
51 | }; |
52 | |
53 | } // namespace os_unix |
54 | |
55 | #endif // OS_WIN |
56 | |
57 | } // namespace internal |
58 | |
59 | #if OS_WIN |
60 | |
61 | using UniqueFD = UniqueObject<HANDLE, internal::os_win::UniqueFDTraits>; |
62 | |
63 | #else // OS_WIN |
64 | |
65 | using UniqueFD = UniqueObject<int, internal::os_unix::UniqueFDTraits>; |
66 | using UniqueDir = UniqueObject<DIR*, internal::os_unix::UniqueDirTraits>; |
67 | |
68 | #endif // OS_WIN |
69 | |
70 | } // namespace fml |
71 | |
72 | #endif // FLUTTER_FML_UNIQUE_FD_H_ |
73 | |