• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
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 namespace base {
11 
12 // Simple scoper that saves the current value of errno, resets it to 0, and on
13 // destruction puts the old value back.
14 class ScopedClearErrno {
15  public:
ScopedClearErrno()16   ScopedClearErrno() : old_errno_(errno) { errno = 0; }
~ScopedClearErrno()17   ~ScopedClearErrno() {
18     if (errno == 0)
19       errno = old_errno_;
20   }
21 
22  private:
23   const int old_errno_;
24 
25   ScopedClearErrno(const ScopedClearErrno&) = delete;
26   ScopedClearErrno& operator=(const ScopedClearErrno&) = delete;
27 };
28 
29 }  // namespace base
30 
31 #endif  // BASE_SCOPED_CLEAR_ERRNO_H_
32