template <class Tgt, class Src>
typename std::enable_if<
- std::is_enum<Src>::value && !std::is_same<Src, Tgt>::value,
+ std::is_enum<Src>::value && !std::is_same<Src, Tgt>::value &&
+ !std::is_convertible<Tgt, StringPiece>::value,
Expected<Tgt, ConversionCode>>::type
tryTo(const Src& value) {
using I = typename std::underlying_type<Src>::type;
template <class Tgt, class Src>
typename std::enable_if<
- std::is_enum<Tgt>::value && !std::is_same<Src, Tgt>::value,
+ !std::is_convertible<Src, StringPiece>::valuea &&
+ std::is_enum<Tgt>::value && !std::is_same<Src, Tgt>::value,
Expected<Tgt, ConversionCode>>::type
tryTo(const Src& value) {
using I = typename std::underlying_type<Tgt>::type;
template <class Tgt, class Src>
typename std::enable_if<
- std::is_enum<Src>::value && !std::is_same<Src, Tgt>::value,
+ std::is_enum<Src>::value && !std::is_same<Src, Tgt>::value &&
+ !std::is_convertible<Tgt, StringPiece>::value,
Tgt>::type
to(const Src& value) {
return to<Tgt>(static_cast<typename std::underlying_type<Src>::type>(value));
template <class Tgt, class Src>
typename std::enable_if<
- !IsSomeString<Src>::value && std::is_enum<Tgt>::value &&
+ !std::is_convertible<Src, StringPiece>::value && std::is_enum<Tgt>::value &&
!std::is_same<Src, Tgt>::value,
Tgt>::type
to(const Src& value) {
return 2000 + folly::estimateSpaceNeeded(in.w) +
folly::estimateSpaceNeeded(in.h);
}
+
+enum class SmallEnum {};
+
+Expected<StringPiece, ConversionCode> parseTo(StringPiece in, SmallEnum& out) {
+ out = {};
+ if (in == "SmallEnum") {
+ return in.removePrefix(in), in;
+ } else {
+ return makeUnexpected(ConversionCode::STRING_TO_FLOAT_ERROR);
+ }
+}
+
+template <class String>
+void toAppend(SmallEnum, String* result) {
+ folly::toAppend("SmallEnum", result);
+}
} // namespace my
TEST(Conv, custom_kkproviders) {
EXPECT_EQ("7x8|7x8", str);
}
+TEST(conv, custom_enumclass) {
+ EXPECT_EQ(my::SmallEnum{}, folly::to<my::SmallEnum>("SmallEnum"));
+ EXPECT_EQ(my::SmallEnum{}, folly::tryTo<my::SmallEnum>("SmallEnum").value());
+ auto str = to<string>(my::SmallEnum{});
+ toAppend("|", my::SmallEnum{}, &str);
+ EXPECT_EQ("SmallEnum|SmallEnum", str);
+}
+
TEST(Conv, TryToThenWithVoid) {
auto x = tryTo<int>("42").then([](int) {});
EXPECT_TRUE(x.hasValue());