scoped_clear_errno.h 750 Bytes
Newer Older
gejun's avatar
gejun committed
1 2 3 4
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
#ifndef BUTIL_SCOPED_CLEAR_ERRNO_H_
#define BUTIL_SCOPED_CLEAR_ERRNO_H_
gejun's avatar
gejun committed
7 8 9

#include <errno.h>

10
#include "butil/basictypes.h"
gejun's avatar
gejun committed
11

12
namespace butil {
gejun's avatar
gejun committed
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

// Simple scoper that saves the current value of errno, resets it to 0, and on
// destruction puts the old value back.
class ScopedClearErrno {
 public:
  ScopedClearErrno() : old_errno_(errno) {
    errno = 0;
  }
  ~ScopedClearErrno() {
    if (errno == 0)
      errno = old_errno_;
  }

 private:
  const int old_errno_;

  DISALLOW_COPY_AND_ASSIGN(ScopedClearErrno);
};

32
}  // namespace butil
gejun's avatar
gejun committed
33

34
#endif  // BUTIL_SCOPED_CLEAR_ERRNO_H_