X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=folly%2FException.h;h=19d7509d15a64768c4d1da557d104f6f0ee93531;hb=HEAD;hp=f0547c02545adf2797dba310d5a510a0d914958b;hpb=ce64f0f685111ac24c7a321ea56d0c3524621df1;p=folly.git diff --git a/folly/Exception.h b/folly/Exception.h index f0547c02..19d7509d 100644 --- a/folly/Exception.h +++ b/folly/Exception.h @@ -1,5 +1,5 @@ /* - * Copyright 2014 Facebook, Inc. + * Copyright 2013-present 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,8 +14,7 @@ * limitations under the License. */ -#ifndef FOLLY_EXCEPTION_H_ -#define FOLLY_EXCEPTION_H_ +#pragma once #include @@ -36,25 +35,45 @@ namespace folly { // // The *Explicit functions take an explicit value for errno. -// Helper to throw std::system_error -FOLLY_NORETURN void throwSystemErrorExplicit(int err, const char*); -inline void throwSystemErrorExplicit(int err, const char* msg) { - throw std::system_error(err, std::system_category(), msg); +inline std::system_error makeSystemErrorExplicit(int err, const char* msg) { + // TODO: The C++ standard indicates that std::generic_category() should be + // used for POSIX errno codes. + // + // We should ideally change this to use std::generic_category() instead of + // std::system_category(). However, undertaking this change will require + // updating existing call sites that currently catch exceptions thrown by + // this code and currently expect std::system_category. + return std::system_error(err, std::system_category(), msg); } template -FOLLY_NORETURN void throwSystemErrorExplicit(int, Args&&... args); -template -void throwSystemErrorExplicit(int err, Args&&... args) { - throwSystemErrorExplicit( +std::system_error makeSystemErrorExplicit(int err, Args&&... args) { + return makeSystemErrorExplicit( err, to(std::forward(args)...).c_str()); } -// Helper to throw std::system_error from errno and components of a string +inline std::system_error makeSystemError(const char* msg) { + return makeSystemErrorExplicit(errno, msg); +} + template -FOLLY_NORETURN void throwSystemError(Args&&... args); +std::system_error makeSystemError(Args&&... args) { + return makeSystemErrorExplicit(errno, std::forward(args)...); +} + +// Helper to throw std::system_error +[[noreturn]] inline void throwSystemErrorExplicit(int err, const char* msg) { + throw makeSystemErrorExplicit(err, msg); +} + +template +[[noreturn]] void throwSystemErrorExplicit(int err, Args&&... args) { + throw makeSystemErrorExplicit(err, std::forward(args)...); +} + +// Helper to throw std::system_error from errno and components of a string template -void throwSystemError(Args&&... args) { +[[noreturn]] void throwSystemError(Args&&... args) { throwSystemErrorExplicit(errno, std::forward(args)...); } @@ -72,7 +91,7 @@ void checkPosixError(int err, Args&&... args) { template void checkKernelError(ssize_t ret, Args&&... args) { if (UNLIKELY(ret < 0)) { - throwSystemErrorExplicit(-ret, std::forward(args)...); + throwSystemErrorExplicit(int(-ret), std::forward(args)...); } } @@ -109,7 +128,15 @@ void checkFopenErrorExplicit(FILE* fp, int savedErrno, Args&&... args) { } } -} // namespace folly - -#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