Etienne Pierre-Doray | d120ebf | 2018-09-14 23:38:21 | [diff] [blame] | 1 | // Copyright (c) 2018 The Chromium 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 BASE_SCOPED_CLEAR_LAST_ERROR_H_ |
| 6 | #define BASE_SCOPED_CLEAR_LAST_ERROR_H_ |
| 7 | |
| 8 | #include <errno.h> |
| 9 | |
| 10 | #include "base/base_export.h" |
| 11 | #include "base/macros.h" |
| 12 | #include "build/build_config.h" |
| 13 | |
| 14 | namespace base { |
| 15 | namespace internal { |
| 16 | |
| 17 | // ScopedClearLastError stores and resets the value of thread local error codes |
| 18 | // (errno, GetLastError()), and restores them in the destructor. This is useful |
| 19 | // to avoid side effects on these values in instrumentation functions that |
| 20 | // interact with the OS. |
| 21 | |
| 22 | // Common implementation of ScopedClearLastError for all platforms. Use |
| 23 | // ScopedClearLastError instead. |
| 24 | class BASE_EXPORT ScopedClearLastErrorBase { |
| 25 | public: |
| 26 | ScopedClearLastErrorBase() : last_errno_(errno) { errno = 0; } |
| 27 | ~ScopedClearLastErrorBase() { errno = last_errno_; } |
| 28 | |
| 29 | private: |
| 30 | const int last_errno_; |
| 31 | |
| 32 | DISALLOW_COPY_AND_ASSIGN(ScopedClearLastErrorBase); |
| 33 | }; |
| 34 | |
| 35 | #if defined(OS_WIN) |
| 36 | |
| 37 | // Windows specific implementation of ScopedClearLastError. |
| 38 | class BASE_EXPORT ScopedClearLastError : public ScopedClearLastErrorBase { |
| 39 | public: |
| 40 | ScopedClearLastError(); |
| 41 | ~ScopedClearLastError(); |
| 42 | |
| 43 | private: |
| 44 | unsigned int last_system_error_; |
| 45 | |
| 46 | DISALLOW_COPY_AND_ASSIGN(ScopedClearLastError); |
| 47 | }; |
| 48 | |
| 49 | #elif defined(OS_POSIX) || defined(OS_FUCHSIA) |
| 50 | |
| 51 | using ScopedClearLastError = ScopedClearLastErrorBase; |
| 52 | |
| 53 | #endif // defined(OS_WIN) |
| 54 | |
| 55 | } // namespace internal |
| 56 | } // namespace base |
| 57 | |
| 58 | #endif // BASE_SCOPED_CLEAR_LAST_ERROR_H_ |