X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=folly%2FException.h;h=3985421a883e13125514d4e7be2be678bd28625c;hb=3d9eb7ffc32c2f5f878bb4aee61d30cb8c62a146;hp=30444b2bdaa193f17a0eb6764b20294cb95b45f4;hpb=9895ee085d445ae3ae6becdddfb9c8f9d8f6c1f5;p=folly.git diff --git a/folly/Exception.h b/folly/Exception.h index 30444b2b..3985421a 100644 --- a/folly/Exception.h +++ b/folly/Exception.h @@ -1,5 +1,5 @@ /* - * Copyright 2016 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,8 +14,7 @@ * limitations under the License. */ -#ifndef FOLLY_EXCEPTION_H_ -#define FOLLY_EXCEPTION_H_ +#pragma once #include @@ -36,15 +35,40 @@ namespace folly { // // The *Explicit functions take an explicit value for errno. +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 +std::system_error makeSystemErrorExplicit(int err, Args&&... args) { + return makeSystemErrorExplicit( + err, to(std::forward(args)...).c_str()); +} + +inline std::system_error makeSystemError(const char* msg) { + return makeSystemErrorExplicit(errno, msg); +} + +template +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 std::system_error(err, std::system_category(), msg); + throw makeSystemErrorExplicit(err, msg); } template [[noreturn]] void throwSystemErrorExplicit(int err, Args&&... args) { - throwSystemErrorExplicit( - err, to(std::forward(args)...).c_str()); + throw makeSystemErrorExplicit(err, std::forward(args)...); } // Helper to throw std::system_error from errno and components of a string @@ -67,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)...); } } @@ -104,20 +128,15 @@ void checkFopenErrorExplicit(FILE* fp, int savedErrno, Args&&... args) { } } -template -void throwOnFail(V&& value, Args&&... args) { - if (!value) { - throw E(std::forward(args)...); - } -} - /** * 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) \ - ::folly::throwOnFail((cond), "Check failed: " #cond) - -} // namespace folly - -#endif /* FOLLY_EXCEPTION_H_ */ +#define CHECK_THROW(cond, E) \ + do { \ + if (!(cond)) { \ + throw E("Check failed: " #cond); \ + } \ + } while (0) + +} // namespace folly