Add support to Pickle for reading and writing floats
BUG=136034
Review URL: https://chromiumcodereview.appspot.com/11416150
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@169957 0039d316-1c4b-4281-b951-d872f2087c98
CrOS-Libchrome-Original-Commit: b1f61b03866ff2cccb0d9d07645d6780b719b0b9
diff --git a/base/pickle.cc b/base/pickle.cc
index 0e44131..00c6ef0 100644
--- a/base/pickle.cc
+++ b/base/pickle.cc
@@ -90,6 +90,10 @@
return ReadBuiltinType(result);
}
+bool PickleIterator::ReadFloat(float* result) {
+ return ReadBuiltinType(result);
+}
+
bool PickleIterator::ReadString(std::string* result) {
int len;
if (!ReadInt(&len))
diff --git a/base/pickle.h b/base/pickle.h
index a92915f..cd587de 100644
--- a/base/pickle.h
+++ b/base/pickle.h
@@ -34,6 +34,7 @@
bool ReadUInt32(uint32* result) WARN_UNUSED_RESULT;
bool ReadInt64(int64* result) WARN_UNUSED_RESULT;
bool ReadUInt64(uint64* result) WARN_UNUSED_RESULT;
+ bool ReadFloat(float* result) WARN_UNUSED_RESULT;
bool ReadString(std::string* result) WARN_UNUSED_RESULT;
bool ReadWString(std::wstring* result) WARN_UNUSED_RESULT;
bool ReadString16(string16* result) WARN_UNUSED_RESULT;
@@ -158,6 +159,9 @@
bool ReadUInt64(PickleIterator* iter, uint64* result) const {
return iter->ReadUInt64(result);
}
+ bool ReadFloat(PickleIterator* iter, float* result) const {
+ return iter->ReadFloat(result);
+ }
bool ReadString(PickleIterator* iter, std::string* result) const {
return iter->ReadString(result);
}
@@ -218,6 +222,9 @@
bool WriteUInt64(uint64 value) {
return WriteBytes(&value, sizeof(value));
}
+ bool WriteFloat(float value) {
+ return WriteBytes(&value, sizeof(value));
+ }
bool WriteString(const std::string& value);
bool WriteWString(const std::wstring& value);
bool WriteString16(const string16& value);
diff --git a/base/pickle_unittest.cc b/base/pickle_unittest.cc
index 8cb1d2a..c866acd 100644
--- a/base/pickle_unittest.cc
+++ b/base/pickle_unittest.cc
@@ -20,6 +20,7 @@
const bool testbool1 = false;
const bool testbool2 = true;
const uint16 testuint16 = 32123;
+const float testfloat = 3.1415926935f;
// checks that the result
void VerifyResult(const Pickle& pickle) {
@@ -47,6 +48,10 @@
EXPECT_TRUE(pickle.ReadUInt16(&iter, &outuint16));
EXPECT_EQ(testuint16, outuint16);
+ float outfloat;
+ EXPECT_TRUE(pickle.ReadFloat(&iter, &outfloat));
+ EXPECT_EQ(testfloat, outfloat);
+
const char* outdata;
int outdatalen;
EXPECT_TRUE(pickle.ReadData(&iter, &outdata, &outdatalen));
@@ -72,6 +77,7 @@
EXPECT_TRUE(pickle.WriteBool(testbool1));
EXPECT_TRUE(pickle.WriteBool(testbool2));
EXPECT_TRUE(pickle.WriteUInt16(testuint16));
+ EXPECT_TRUE(pickle.WriteFloat(testfloat));
EXPECT_TRUE(pickle.WriteData(testdata, testdatalen));
// Over allocate BeginWriteData so we can test TrimWriteData.