Value: Disambiguate std::string&& and StringRef.

Making the former a template causes the compiler to prefer the latter
when given a bare C string.
This commit is contained in:
Peter Johnson
2015-07-22 22:08:13 -07:00
parent 4c14f7823a
commit 9c204533e8
3 changed files with 22 additions and 21 deletions

View File

@@ -91,7 +91,8 @@ class Value {
val->m_val.data.v_string.len = val->m_string.size();
return val;
}
static std::shared_ptr<Value> MakeString(std::string&& value) {
template <typename T>
static std::shared_ptr<Value> MakeString(T&& value) {
auto val = std::make_shared<Value>(NT_STRING, private_init());
val->m_string = std::move(value);
val->m_val.data.v_string.str = const_cast<char*>(val->m_string.c_str());
@@ -105,7 +106,8 @@ class Value {
val->m_val.data.v_raw.len = val->m_string.size();
return val;
}
static std::shared_ptr<Value> MakeRaw(std::string&& value) {
template <typename T>
static std::shared_ptr<Value> MakeRaw(T&& value) {
auto val = std::make_shared<Value>(NT_RAW, private_init());
val->m_string = std::move(value);
val->m_val.data.v_raw.str = const_cast<char*>(val->m_string.c_str());
@@ -119,7 +121,8 @@ class Value {
val->m_val.data.v_raw.len = val->m_string.size();
return val;
}
static std::shared_ptr<Value> MakeRpc(std::string&& value) {
template <typename T>
static std::shared_ptr<Value> MakeRpc(T&& value) {
auto val = std::make_shared<Value>(NT_RPC, private_init());
val->m_string = std::move(value);
val->m_val.data.v_raw.str = const_cast<char*>(val->m_string.c_str());

View File

@@ -51,13 +51,12 @@ class StorageTestPersistent : public StorageTest {
storage.SetEntryTypeValue("double/neg", Value::MakeDouble(-1.5));
storage.SetEntryTypeValue("double/zero", Value::MakeDouble(0.0));
storage.SetEntryTypeValue("double/big", Value::MakeDouble(1.3e8));
storage.SetEntryTypeValue("string/empty", Value::MakeString(StringRef("")));
storage.SetEntryTypeValue("string/normal",
Value::MakeString(StringRef("hello")));
storage.SetEntryTypeValue("string/empty", Value::MakeString(""));
storage.SetEntryTypeValue("string/normal", Value::MakeString("hello"));
storage.SetEntryTypeValue("string/special",
Value::MakeString(StringRef("\0\3\5\n", 4)));
storage.SetEntryTypeValue("raw/empty", Value::MakeRaw(StringRef("")));
storage.SetEntryTypeValue("raw/normal", Value::MakeRaw(StringRef("hello")));
storage.SetEntryTypeValue("raw/empty", Value::MakeRaw(""));
storage.SetEntryTypeValue("raw/normal", Value::MakeRaw("hello"));
storage.SetEntryTypeValue("raw/special",
Value::MakeRaw(StringRef("\0\3\5\n", 4)));
storage.SetEntryTypeValue("booleanarr/empty",
@@ -578,15 +577,14 @@ TEST_F(StorageTest, LoadPersistent) {
EXPECT_EQ(*Value::MakeDouble(-1.5), *storage.GetEntryValue("double/neg"));
EXPECT_EQ(*Value::MakeDouble(0.0), *storage.GetEntryValue("double/zero"));
EXPECT_EQ(*Value::MakeDouble(1.3e8), *storage.GetEntryValue("double/big"));
EXPECT_EQ(*Value::MakeString(StringRef("")),
*storage.GetEntryValue("string/empty"));
EXPECT_EQ(*Value::MakeString(StringRef("hello")),
EXPECT_EQ(*Value::MakeString(""), *storage.GetEntryValue("string/empty"));
EXPECT_EQ(*Value::MakeString("hello"),
*storage.GetEntryValue("string/normal"));
EXPECT_EQ(*Value::MakeString(StringRef("\0\3\5\n", 4)),
*storage.GetEntryValue("string/special"));
EXPECT_EQ(*Value::MakeRaw(StringRef("")),
EXPECT_EQ(*Value::MakeRaw(""),
*storage.GetEntryValue("raw/empty"));
EXPECT_EQ(*Value::MakeRaw(StringRef("hello")),
EXPECT_EQ(*Value::MakeRaw("hello"),
*storage.GetEntryValue("raw/normal"));
EXPECT_EQ(*Value::MakeRaw(StringRef("\0\3\5\n", 4)),
*storage.GetEntryValue("raw/special"));

View File

@@ -62,7 +62,7 @@ TEST_F(ValueTest, Double) {
}
TEST_F(ValueTest, String) {
auto v = Value::MakeString(llvm::StringRef("hello"));
auto v = Value::MakeString("hello");
ASSERT_EQ(NT_STRING, v->type());
ASSERT_EQ("hello", v->GetString());
NT_Value cv;
@@ -72,7 +72,7 @@ TEST_F(ValueTest, String) {
ASSERT_EQ(llvm::StringRef("hello"), cv.data.v_string.str);
ASSERT_EQ(5u, cv.data.v_string.len);
v = Value::MakeString(llvm::StringRef("goodbye"));
v = Value::MakeString("goodbye");
ASSERT_EQ(NT_STRING, v->type());
ASSERT_EQ("goodbye", v->GetString());
ConvertToC(*v, &cv);
@@ -84,7 +84,7 @@ TEST_F(ValueTest, String) {
}
TEST_F(ValueTest, Raw) {
auto v = Value::MakeRaw(llvm::StringRef("hello"));
auto v = Value::MakeRaw("hello");
ASSERT_EQ(NT_RAW, v->type());
ASSERT_EQ("hello", v->GetRaw());
NT_Value cv;
@@ -94,7 +94,7 @@ TEST_F(ValueTest, Raw) {
ASSERT_EQ(llvm::StringRef("hello"), cv.data.v_string.str);
ASSERT_EQ(5u, cv.data.v_string.len);
v = Value::MakeRaw(llvm::StringRef("goodbye"));
v = Value::MakeRaw("goodbye");
ASSERT_EQ(NT_RAW, v->type());
ASSERT_EQ("goodbye", v->GetRaw());
ConvertToC(*v, &cv);
@@ -282,12 +282,12 @@ TEST_F(ValueTest, DoubleComparison) {
}
TEST_F(ValueTest, StringComparison) {
auto v1 = Value::MakeString(llvm::StringRef("hello"));
auto v2 = Value::MakeString(llvm::StringRef("hello"));
auto v1 = Value::MakeString("hello");
auto v2 = Value::MakeString("hello");
ASSERT_EQ(*v1, *v2);
v2 = Value::MakeString(llvm::StringRef("world")); // different contents
v2 = Value::MakeString("world"); // different contents
ASSERT_NE(*v1, *v2);
v2 = Value::MakeString(llvm::StringRef("goodbye")); // different size
v2 = Value::MakeString("goodbye"); // different size
ASSERT_NE(*v1, *v2);
}