X-Git-Url: http://plrg.eecs.uci.edu/git/?p=folly.git;a=blobdiff_plain;f=folly%2FException.h;h=379f269bd45983cc8c8056226face91d59dd42ad;hp=5549c4bdcdf83900ce96f34362e8c2125aac6016;hb=65d63573f08eef7b847ebf38ec74d52a61d3b936;hpb=04223d27403bf4f46774c7cc86119d55328d418a diff --git a/folly/Exception.h b/folly/Exception.h index 5549c4bd..379f269b 100644 --- a/folly/Exception.h +++ b/folly/Exception.h @@ -1,5 +1,5 @@ /* - * Copyright 2013 Facebook, Inc. + * Copyright 2017 Facebook, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,60 +14,104 @@ * limitations under the License. */ -#ifndef FOLLY_EXCEPTION_H_ -#define FOLLY_EXCEPTION_H_ +#pragma once #include +#include #include #include -#include "folly/Likely.h" +#include +#include +#include +#include namespace folly { +// Various helpers to throw appropriate std::system_error exceptions from C +// library errors (returned in errno, as positive return values (many POSIX +// functions), or as negative return values (Linux syscalls)) +// +// The *Explicit functions take an explicit value for errno. + // Helper to throw std::system_error -void throwSystemError(int err, const char* msg) __attribute__((noreturn)); -inline void throwSystemError(int err, const char* msg) { +[[noreturn]] inline void throwSystemErrorExplicit(int err, const char* msg) { throw std::system_error(err, std::system_category(), msg); } -// Helper to throw std::system_error from errno -void throwSystemError(const char* msg) __attribute__((noreturn)); -inline void throwSystemError(const char* msg) { - throwSystemError(errno, msg); +template +[[noreturn]] void throwSystemErrorExplicit(int err, Args&&... args) { + throwSystemErrorExplicit( + err, to(std::forward(args)...).c_str()); +} + +// Helper to throw std::system_error from errno and components of a string +template +[[noreturn]] void throwSystemError(Args&&... args) { + throwSystemErrorExplicit(errno, std::forward(args)...); } // Check a Posix return code (0 on success, error number on error), throw // on error. -inline void checkPosixError(int err, const char* msg) { +template +void checkPosixError(int err, Args&&... args) { if (UNLIKELY(err != 0)) { - throwSystemError(err, msg); + throwSystemErrorExplicit(err, std::forward(args)...); } } // Check a Linux kernel-style return code (>= 0 on success, negative error // number on error), throw on error. -inline void checkKernelError(ssize_t ret, const char* msg) { +template +void checkKernelError(ssize_t ret, Args&&... args) { if (UNLIKELY(ret < 0)) { - throwSystemError(-ret, msg); + throwSystemErrorExplicit(int(-ret), std::forward(args)...); } } // Check a traditional Unix return code (-1 and sets errno on error), throw // on error. -inline void checkUnixError(ssize_t ret, const char* msg) { +template +void checkUnixError(ssize_t ret, Args&&... args) { if (UNLIKELY(ret == -1)) { - throwSystemError(msg); + throwSystemError(std::forward(args)...); } } -inline void checkUnixError(ssize_t ret, int savedErrno, const char* msg) { + +template +void checkUnixErrorExplicit(ssize_t ret, int savedErrno, Args&&... args) { if (UNLIKELY(ret == -1)) { - throwSystemError(savedErrno, msg); + throwSystemErrorExplicit(savedErrno, std::forward(args)...); + } +} + +// Check the return code from a fopen-style function (returns a non-nullptr +// FILE* on success, nullptr on error, sets errno). Works with fopen, fdopen, +// freopen, tmpfile, etc. +template +void checkFopenError(FILE* fp, Args&&... args) { + if (UNLIKELY(!fp)) { + throwSystemError(std::forward(args)...); } } -} // namespace folly +template +void checkFopenErrorExplicit(FILE* fp, int savedErrno, Args&&... args) { + if (UNLIKELY(!fp)) { + throwSystemErrorExplicit(savedErrno, std::forward(args)...); + } +} -#endif /* FOLLY_EXCEPTION_H_ */ +/** + * If cond is not true, raise an exception of type E. E must have a ctor that + * works with const char* (a description of the failure). + */ +#define CHECK_THROW(cond, E) \ + do { \ + if (!(cond)) { \ + throw E("Check failed: " #cond); \ + } \ + } while (0) +} // namespace folly