[go: nahoru, domu]

blob: 633e9454ed4fb7bc68641cebcaad5ced510168c9 [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2018 The Chromium Authors
Etienne Pierre-Dorayd120ebf2018-09-14 23:38:212// 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"
Etienne Pierre-Dorayd120ebf2018-09-14 23:38:2111#include "build/build_config.h"
12
13namespace base {
Etienne Pierre-Dorayd120ebf2018-09-14 23:38:2114
15// ScopedClearLastError stores and resets the value of thread local error codes
16// (errno, GetLastError()), and restores them in the destructor. This is useful
17// to avoid side effects on these values in instrumentation functions that
18// interact with the OS.
19
20// Common implementation of ScopedClearLastError for all platforms. Use
21// ScopedClearLastError instead.
22class BASE_EXPORT ScopedClearLastErrorBase {
23 public:
24 ScopedClearLastErrorBase() : last_errno_(errno) { errno = 0; }
David Bienvenu5f4d4f02020-09-27 16:55:0325 ScopedClearLastErrorBase(const ScopedClearLastErrorBase&) = delete;
26 ScopedClearLastErrorBase& operator=(const ScopedClearLastErrorBase&) = delete;
Etienne Pierre-Dorayd120ebf2018-09-14 23:38:2127 ~ScopedClearLastErrorBase() { errno = last_errno_; }
28
29 private:
30 const int last_errno_;
Etienne Pierre-Dorayd120ebf2018-09-14 23:38:2131};
32
Xiaohan Wang38e4ebb2022-01-19 06:57:4333#if BUILDFLAG(IS_WIN)
Etienne Pierre-Dorayd120ebf2018-09-14 23:38:2134
35// Windows specific implementation of ScopedClearLastError.
36class BASE_EXPORT ScopedClearLastError : public ScopedClearLastErrorBase {
37 public:
38 ScopedClearLastError();
David Bienvenu5f4d4f02020-09-27 16:55:0339 ScopedClearLastError(const ScopedClearLastError&) = delete;
40 ScopedClearLastError& operator=(const ScopedClearLastError&) = delete;
Etienne Pierre-Dorayd120ebf2018-09-14 23:38:2141 ~ScopedClearLastError();
42
43 private:
Joshua Perazab427af262020-04-13 21:54:4244 const unsigned long last_system_error_;
Etienne Pierre-Dorayd120ebf2018-09-14 23:38:2145};
46
Xiaohan Wang38e4ebb2022-01-19 06:57:4347#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
Etienne Pierre-Dorayd120ebf2018-09-14 23:38:2148
49using ScopedClearLastError = ScopedClearLastErrorBase;
50
Xiaohan Wang38e4ebb2022-01-19 06:57:4351#endif // BUILDFLAG(IS_WIN)
Etienne Pierre-Dorayd120ebf2018-09-14 23:38:2152
Etienne Pierre-Dorayd120ebf2018-09-14 23:38:2153} // namespace base
54
55#endif // BASE_SCOPED_CLEAR_LAST_ERROR_H_