From 500f3598bb9e351b0aaf86a68c009ce8b815b979 Mon Sep 17 00:00:00 2001 From: Marcus Holland-Moritz Date: Thu, 9 Jun 2016 23:40:54 -0700 Subject: [PATCH] Get rid of circular dependency when including ExceptionWrapper.h from Conv.h Summary: This is needed in order to be able to use folly::Try<> with folly::to<> for non-throwing conversions. Reviewed By: meyering Differential Revision: D3411003 fbshipit-source-id: 1f20e7871b9cedda2b35527dac70381579abd708 --- folly/ExceptionString.h | 69 ++++++++++++++++++++++++++++++++++++++++ folly/ExceptionWrapper.h | 2 +- folly/Makefile.am | 1 + folly/String.h | 41 +----------------------- 4 files changed, 72 insertions(+), 41 deletions(-) create mode 100644 folly/ExceptionString.h diff --git a/folly/ExceptionString.h b/folly/ExceptionString.h new file mode 100644 index 00000000..a5037b1d --- /dev/null +++ b/folly/ExceptionString.h @@ -0,0 +1,69 @@ +/* + * Copyright 2016 Facebook, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include +#include +#include + +#include +#include +#include + +namespace folly { + +/** + * Debug string for an exception: include type and what(), if + * defined. + */ +inline fbstring exceptionStr(const std::exception& e) { +#ifdef FOLLY_HAS_RTTI + fbstring rv(demangle(typeid(e))); + rv += ": "; +#else + fbstring rv("Exception (no RTTI available): "); +#endif + rv += e.what(); + return rv; +} + +// Empirically, this indicates if the runtime supports +// std::exception_ptr, as not all (arm, for instance) do. +#if defined(__GNUC__) && defined(__GCC_ATOMIC_INT_LOCK_FREE) && \ + __GCC_ATOMIC_INT_LOCK_FREE > 1 +inline fbstring exceptionStr(std::exception_ptr ep) { + try { + std::rethrow_exception(ep); + } catch (const std::exception& e) { + return exceptionStr(e); + } catch (...) { + return ""; + } +} +#endif + +template +auto exceptionStr(const E& e) -> typename std:: + enable_if::value, fbstring>::type { +#ifdef FOLLY_HAS_RTTI + return demangle(typeid(e)); +#else + return "Exception (no RTTI available)"; +#endif +} + +} // namespace folly diff --git a/folly/ExceptionWrapper.h b/folly/ExceptionWrapper.h index 22857a0b..ca57282c 100644 --- a/folly/ExceptionWrapper.h +++ b/folly/ExceptionWrapper.h @@ -19,7 +19,7 @@ #include #include #include -#include +#include #include namespace folly { diff --git a/folly/Makefile.am b/folly/Makefile.am index 60bd725d..45547c9d 100644 --- a/folly/Makefile.am +++ b/folly/Makefile.am @@ -83,6 +83,7 @@ nobase_follyinclude_HEADERS = \ dynamic.h \ dynamic-inl.h \ Exception.h \ + ExceptionString.h \ ExceptionWrapper.h \ Executor.h \ EvictingCacheMap.h \ diff --git a/folly/String.h b/folly/String.h index d900e785..fc552f11 100644 --- a/folly/String.h +++ b/folly/String.h @@ -34,7 +34,7 @@ #include #include -#include +#include #include #include #include @@ -386,45 +386,6 @@ std::string hexDump(const void* ptr, size_t size); */ fbstring errnoStr(int err); -/** - * Debug string for an exception: include type and what(), if - * defined. - */ -inline fbstring exceptionStr(const std::exception& e) { -#ifdef FOLLY_HAS_RTTI - return folly::to(demangle(typeid(e)), ": ", e.what()); -#else - return folly::to("Exception (no RTTI available): ", e.what()); -#endif -} - -// Empirically, this indicates if the runtime supports -// std::exception_ptr, as not all (arm, for instance) do. -#if defined(__GNUC__) && defined(__GCC_ATOMIC_INT_LOCK_FREE) && \ - __GCC_ATOMIC_INT_LOCK_FREE > 1 -inline fbstring exceptionStr(std::exception_ptr ep) { - try { - std::rethrow_exception(ep); - } catch (const std::exception& e) { - return exceptionStr(e); - } catch (...) { - return ""; - } -} -#endif - -template -auto exceptionStr(const E& e) - -> typename std::enable_if::value, - fbstring>::type -{ -#ifdef FOLLY_HAS_RTTI - return folly::to(demangle(typeid(e))); -#else - return "Exception (no RTTI available)"; -#endif -} - /* * Split a string into a list of tokens by delimiter. * -- 2.34.1