1 // Copyright 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.
5 #ifndef BASE_SCOPED_CLEAR_LAST_ERROR_H_
6 #define BASE_SCOPED_CLEAR_LAST_ERROR_H_
10 #include "base/base_export.h"
11 #include "base/macros.h"
12 #include "build/build_config.h"
16 // ScopedClearLastError stores and resets the value of thread local error codes
17 // (errno, GetLastError()), and restores them in the destructor. This is useful
18 // to avoid side effects on these values in instrumentation functions that
19 // interact with the OS.
21 // Common implementation of ScopedClearLastError for all platforms. Use
22 // ScopedClearLastError instead.
23 class BASE_EXPORT ScopedClearLastErrorBase {
25 ScopedClearLastErrorBase() : last_errno_(errno) { errno = 0; }
26 ~ScopedClearLastErrorBase() { errno = last_errno_; }
29 const int last_errno_;
31 DISALLOW_COPY_AND_ASSIGN(ScopedClearLastErrorBase);
36 // Windows specific implementation of ScopedClearLastError.
37 class BASE_EXPORT ScopedClearLastError : public ScopedClearLastErrorBase {
39 ScopedClearLastError();
40 ~ScopedClearLastError();
43 const unsigned long last_system_error_;
45 DISALLOW_COPY_AND_ASSIGN(ScopedClearLastError);
48 #elif defined(OS_POSIX) || defined(OS_FUCHSIA)
50 using ScopedClearLastError = ScopedClearLastErrorBase;
52 #endif // defined(OS_WIN)
56 #endif // BASE_SCOPED_CLEAR_LAST_ERROR_H_