Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(29)

Unified Diff: base/pickle.h

Issue 39463002: Introduce Pickle::{Read,Write}PODArray to serialize arrays of POD (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | base/pickle.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/pickle.h
diff --git a/base/pickle.h b/base/pickle.h
index 952ef0dae539f9df1b90bdcea96216c703832e9e..60c97bc2e879e3d9329e4d83eb516494b2a6657a 100644
--- a/base/pickle.h
+++ b/base/pickle.h
@@ -40,6 +40,14 @@ class BASE_EXPORT PickleIterator {
bool ReadString16(string16* result) WARN_UNUSED_RESULT;
bool ReadData(const char** data, int* length) WARN_UNUSED_RESULT;
bool ReadBytes(const char** data, int length) WARN_UNUSED_RESULT;
+ template<size_t n, typename T>
+ bool ReadPODArray(const T** data) WARN_UNUSED_RESULT {
+ const char* char_data;
+ if (!ReadBytes(&char_data, sizeof(T[n])))
+ return false;
+ *data = reinterpret_cast<const T*>(char_data);
+ return true;
+ }
// Safer version of ReadInt() checks for the result not being negative.
// Use it for reading the object sizes.
@@ -200,6 +208,20 @@ class BASE_EXPORT Pickle {
int length) const WARN_UNUSED_RESULT {
return iter->ReadBytes(data, length);
}
+ // A pointer to the data will be placed in *data. The caller specifies the
+ // number of objects to read as a template parameter. The returned buffer will
+ // be into the message's buffer so will be scoped to the lifetime of the
+ // message (or until the message data is mutated).
+ // Example usage:
+ // const Type* data;
+ // if (pickle.ReadPODArray<4>(&data)) {
+ // DoSomething(data[0], data[1], data[2], data[3]);
+ // }
+ template<size_t n, typename T> bool ReadPODArray(
+ PickleIterator* iter,
+ const T** data) const WARN_UNUSED_RESULT {
+ return iter->ReadPODArray<n>(data);
+ }
// Safer version of ReadInt() checks for the result not being negative.
// Use it for reading the object sizes.
@@ -252,6 +274,13 @@ class BASE_EXPORT Pickle {
// known size. See also WriteData.
bool WriteBytes(const void* data, int length);
+ // Writes a fixed-size POD array by copying its bytes. Use as:
+ // Type data[4] = {...}
+ // pickle.WritePODArray<4>(data);
+ template <size_t n, typename T> void WritePODArray(const T* data) {
+ WriteBytesStatic<sizeof(T[n])>(data);
+ }
+
// Reserves space for upcoming writes when multiple writes will be made and
// their sizes are computed in advance. It can be significantly faster to call
// Reserve() before calling WriteFoo() multiple times.
« no previous file with comments | « no previous file | base/pickle.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698