blob: 44a0c6274ba39e010babb7e288fe4482ba4c14c1 [file] [log] [blame]
Scott Graham66962112018-06-08 12:42:08 -07001// 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
3// found in the LICENSE file.
4
5#ifndef BASE_SCOPED_CLEAR_ERRNO_H_
6#define BASE_SCOPED_CLEAR_ERRNO_H_
7
8#include <errno.h>
9
10#include "base/macros.h"
11
12namespace base {
13
14// Simple scoper that saves the current value of errno, resets it to 0, and on
15// destruction puts the old value back.
16class ScopedClearErrno {
17 public:
Scott Graham98cd3ca2018-06-14 22:26:55 -070018 ScopedClearErrno() : old_errno_(errno) { errno = 0; }
Scott Graham66962112018-06-08 12:42:08 -070019 ~ScopedClearErrno() {
20 if (errno == 0)
21 errno = old_errno_;
22 }
23
24 private:
25 const int old_errno_;
26
27 DISALLOW_COPY_AND_ASSIGN(ScopedClearErrno);
28};
29
30} // namespace base
31
32#endif // BASE_SCOPED_CLEAR_ERRNO_H_