2022-06-24 01:45:36 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
|
|
*
|
|
|
|
* This software may be used and distributed according to the terms of the
|
|
|
|
* GNU General Public License version 2.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <fmt/core.h>
|
|
|
|
#include <folly/Conv.h>
|
|
|
|
|
|
|
|
namespace facebook::eden {
|
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deduplicate the codegen for exception allocation and __cxa_throw /
|
|
|
|
* _CxxThrowException.
|
|
|
|
*/
|
|
|
|
template <typename E>
|
2022-07-08 23:30:53 +03:00
|
|
|
[[noreturn]] void throw_dedup(const char* what) {
|
2022-06-24 01:45:36 +03:00
|
|
|
throw E{what};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deduplicate the codegen for exception allocation and __cxa_throw /
|
|
|
|
* _CxxThrowException.
|
|
|
|
*/
|
|
|
|
template <typename E>
|
2022-07-08 23:30:53 +03:00
|
|
|
[[noreturn]] void throw_dedup(const std::string& what) {
|
2022-06-24 01:45:36 +03:00
|
|
|
throw E{what};
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
/**
|
|
|
|
* `throw_<runtime_error>(a, b, c)` is equivalent to `throw
|
|
|
|
* std::runtime_error(folly::to<std::string>(a, b, c))` but shorter and
|
|
|
|
* generates less code.
|
|
|
|
*
|
|
|
|
* May be very slightly more efficient than `throwf` because it only supports
|
|
|
|
* concatenation and does not parse a format string.
|
|
|
|
*/
|
|
|
|
template <typename E, typename... T>
|
|
|
|
[[noreturn]] void throw_(T&&... args) {
|
2022-07-08 23:30:53 +03:00
|
|
|
detail::throw_dedup<E>(folly::to<std::string>(std::forward<T>(args)...));
|
2022-06-24 01:45:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* `throwf<runtime_error>("error: {}", x)` is equivalent to
|
|
|
|
* `throw std::runtime_error(fmt::format("error: {}", x))` but shorter and
|
|
|
|
* generates less code.
|
|
|
|
*/
|
|
|
|
template <typename E, typename... T>
|
|
|
|
[[noreturn]] void throwf(fmt::format_string<T...> fmt, T&&... args) {
|
2022-07-08 23:30:53 +03:00
|
|
|
detail::throw_dedup<E>(fmt::format(std::move(fmt), std::forward<T>(args)...));
|
2022-06-24 01:45:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace facebook::eden
|