#ifndef TENSORFLOW_LIB_CORE_ERRORS_H_
#define TENSORFLOW_LIB_CORE_ERRORS_H_
+#include <sstream>
+
#include "tensorflow/core/lib/core/status.h"
#include "tensorflow/core/lib/strings/strcat.h"
#include "tensorflow/core/platform/logging.h"
typedef ::tensorflow::error::Code Code;
+namespace internal {
+
+// The DECLARE_ERROR macro below only supports types that can be converted
+// into StrCat's AlphaNum. For the other types we rely on a slower path
+// through std::stringstream. To add support of a new type, it is enough to
+// make sure there is an operator<<() for it:
+//
+// std::ostream& operator<<(std::ostream& os, const MyType& foo) {
+// os << foo.ToString();
+// return os;
+// }
+// Eventually absl::strings will have native support for this and we will be
+// able to completely remove PrepareForStrCat().
+template <typename T>
+typename std::enable_if<!std::is_convertible<T, strings::AlphaNum>::value,
+ string>::type
+PrepareForStrCat(const T& t) {
+ std::stringstream ss;
+ ss << t;
+ return ss.str();
+}
+inline const strings::AlphaNum& PrepareForStrCat(const strings::AlphaNum& a) {
+ return a;
+}
+
+} // namespace internal
+
// Append some context to an error message. Each time we append
// context put it on a new line, since it is possible for there
// to be several layers of additional context.
#define DECLARE_ERROR(FUNC, CONST) \
template <typename... Args> \
::tensorflow::Status FUNC(Args... args) { \
- return ::tensorflow::Status(::tensorflow::error::CONST, \
- ::tensorflow::strings::StrCat(args...)); \
+ return ::tensorflow::Status( \
+ ::tensorflow::error::CONST, \
+ ::tensorflow::strings::StrCat( \
+ ::tensorflow::errors::internal::PrepareForStrCat(args)...)); \
} \
inline bool Is##FUNC(const ::tensorflow::Status& status) { \
return status.code() == ::tensorflow::error::CONST; \
AlphaNum(float f) // NOLINT(runtime/explicit)
: piece_(digits_, strlen(FloatToBuffer(f, digits_))) {}
- AlphaNum(bfloat16 f) // NOLINT(runtime/explicit)
- : piece_(digits_, strlen(FloatToBuffer(static_cast<float>(f), digits_))) {
- }
AlphaNum(double f) // NOLINT(runtime/explicit)
: piece_(digits_, strlen(DoubleToBuffer(f, digits_))) {}