blob: fc7dfdf4fda23e5d88d17d1726383e7d6d8e1fe9 [file] [log] [blame]
Alex Vakulenkoe4eec202017-01-27 14:41:04 -08001#ifndef ANDROID_PDX_ERRNO_GUARD_H_
2#define ANDROID_PDX_ERRNO_GUARD_H_
3
4#include <errno.h>
5
6namespace android {
7namespace pdx {
8
9// Automatically saves and restores the system errno for API implementations to
10// prevent internal use errno from affecting API callers.
11class ErrnoGuard {
12 public:
13 ErrnoGuard() : saved_errno_(errno) {}
14 ~ErrnoGuard() { errno = saved_errno_; }
15
16 int saved_errno() const { return saved_errno_; }
17
18 private:
19 int saved_errno_;
20
21 ErrnoGuard(const ErrnoGuard&) = delete;
22 void operator=(const ErrnoGuard&) = delete;
23};
24
25// Checks |return_code| and returns either it or the negated system errno based
26// on the return code value.
27inline int ReturnCodeOrError(int return_code) {
28 return return_code < 0 ? -errno : return_code;
29}
30
31} // namespace pdx
32} // namespace android
33
34#endif // ANDROID_PDX_ERRNO_GUARD_H_