OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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_SCOPED_CLEAR_ERRNO_H_ | 5 #ifndef BASE_SCOPED_CLEAR_ERRNO_H_ |
6 #define BASE_SCOPED_CLEAR_ERRNO_H_ | 6 #define BASE_SCOPED_CLEAR_ERRNO_H_ |
7 | 7 |
8 #include <errno.h> | 8 #include <errno.h> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/macros.h" |
11 | 11 |
12 namespace base { | 12 namespace base { |
13 | 13 |
14 // Simple scoper that saves the current value of errno, resets it to 0, and on | 14 // Simple scoper that saves the current value of errno, resets it to 0, and on |
15 // destruction puts the old value back. | 15 // destruction puts the old value back. |
16 class ScopedClearErrno { | 16 class ScopedClearErrno { |
17 public: | 17 public: |
18 ScopedClearErrno() : old_errno_(errno) { | 18 ScopedClearErrno() : old_errno_(errno) { |
19 errno = 0; | 19 errno = 0; |
20 } | 20 } |
21 ~ScopedClearErrno() { | 21 ~ScopedClearErrno() { |
22 if (errno == 0) | 22 if (errno == 0) |
23 errno = old_errno_; | 23 errno = old_errno_; |
24 } | 24 } |
25 | 25 |
26 private: | 26 private: |
27 const int old_errno_; | 27 const int old_errno_; |
28 | 28 |
29 DISALLOW_COPY_AND_ASSIGN(ScopedClearErrno); | 29 DISALLOW_COPY_AND_ASSIGN(ScopedClearErrno); |
30 }; | 30 }; |
31 | 31 |
32 } // namespace base | 32 } // namespace base |
33 | 33 |
34 #endif // BASE_SCOPED_CLEAR_ERRNO_H_ | 34 #endif // BASE_SCOPED_CLEAR_ERRNO_H_ |
OLD | NEW |