Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame^] | 1 | #ifndef ANDROID_PDX_ERRNO_GUARD_H_ |
| 2 | #define ANDROID_PDX_ERRNO_GUARD_H_ |
| 3 | |
| 4 | #include <errno.h> |
| 5 | |
| 6 | namespace android { |
| 7 | namespace pdx { |
| 8 | |
| 9 | // Automatically saves and restores the system errno for API implementations to |
| 10 | // prevent internal use errno from affecting API callers. |
| 11 | class 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. |
| 27 | inline 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_ |