Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef BASE_PICKLE_H__ | 5 #ifndef BASE_PICKLE_H__ |
| 6 #define BASE_PICKLE_H__ | 6 #define BASE_PICKLE_H__ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/base_export.h" | 10 #include "base/base_export.h" |
| (...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 209 } | 209 } |
| 210 | 210 |
| 211 // Methods for adding to the payload of the Pickle. These values are | 211 // Methods for adding to the payload of the Pickle. These values are |
| 212 // appended to the end of the Pickle's payload. When reading values from a | 212 // appended to the end of the Pickle's payload. When reading values from a |
| 213 // Pickle, it is important to read them in the order in which they were added | 213 // Pickle, it is important to read them in the order in which they were added |
| 214 // to the Pickle. | 214 // to the Pickle. |
| 215 bool WriteBool(bool value) { | 215 bool WriteBool(bool value) { |
| 216 return WriteInt(value ? 1 : 0); | 216 return WriteInt(value ? 1 : 0); |
| 217 } | 217 } |
| 218 bool WriteInt(int value) { | 218 bool WriteInt(int value) { |
| 219 return WriteBytes(&value, sizeof(value)); | 219 WriteBytesStatic<sizeof(value)>(&value); |
| 220 return true; | |
| 220 } | 221 } |
| 221 // WARNING: DO NOT USE THIS METHOD IF PICKLES ARE PERSISTED IN ANY WAY. | 222 // WARNING: DO NOT USE THIS METHOD IF PICKLES ARE PERSISTED IN ANY WAY. |
| 222 // It will write whatever a "long" is on this architecture. On 32-bit | 223 // It will write whatever a "long" is on this architecture. On 32-bit |
| 223 // platforms, it is 32 bits. On 64-bit platforms, it is 64 bits. If persisted | 224 // platforms, it is 32 bits. On 64-bit platforms, it is 64 bits. If persisted |
| 224 // pickles are still around after upgrading to 64-bit, or if they are copied | 225 // pickles are still around after upgrading to 64-bit, or if they are copied |
| 225 // between dissimilar systems, YOUR PICKLES WILL HAVE GONE BAD. | 226 // between dissimilar systems, YOUR PICKLES WILL HAVE GONE BAD. |
| 226 bool WriteLongUsingDangerousNonPortableLessPersistableForm(long value) { | 227 bool WriteLongUsingDangerousNonPortableLessPersistableForm(long value) { |
| 227 return WriteBytes(&value, sizeof(value)); | 228 WriteBytesStatic<sizeof(value)>(&value); |
| 229 return true; | |
| 228 } | 230 } |
| 229 bool WriteUInt16(uint16 value) { | 231 bool WriteUInt16(uint16 value) { |
| 230 return WriteBytes(&value, sizeof(value)); | 232 WriteBytesStatic<sizeof(value)>(&value); |
| 233 return true; | |
| 231 } | 234 } |
| 232 bool WriteUInt32(uint32 value) { | 235 bool WriteUInt32(uint32 value) { |
| 233 return WriteBytes(&value, sizeof(value)); | 236 WriteBytesStatic<sizeof(value)>(&value); |
| 237 return true; | |
| 234 } | 238 } |
| 235 bool WriteInt64(int64 value) { | 239 bool WriteInt64(int64 value) { |
| 236 return WriteBytes(&value, sizeof(value)); | 240 WriteBytesStatic<sizeof(value)>(&value); |
| 241 return true; | |
| 237 } | 242 } |
| 238 bool WriteUInt64(uint64 value) { | 243 bool WriteUInt64(uint64 value) { |
| 239 return WriteBytes(&value, sizeof(value)); | 244 WriteBytesStatic<sizeof(value)>(&value); |
| 245 return true; | |
| 240 } | 246 } |
| 241 bool WriteFloat(float value) { | 247 bool WriteFloat(float value) { |
| 242 return WriteBytes(&value, sizeof(value)); | 248 WriteBytesStatic<sizeof(value)>(&value); |
| 249 return true; | |
| 243 } | 250 } |
| 244 bool WriteString(const std::string& value); | 251 bool WriteString(const std::string& value); |
| 245 bool WriteWString(const std::wstring& value); | 252 bool WriteWString(const std::wstring& value); |
| 246 bool WriteString16(const string16& value); | 253 bool WriteString16(const string16& value); |
| 247 // "Data" is a blob with a length. When you read it out you will be given the | 254 // "Data" is a blob with a length. When you read it out you will be given the |
| 248 // length. See also WriteBytes. | 255 // length. See also WriteBytes. |
| 249 bool WriteData(const char* data, int length); | 256 bool WriteData(const char* data, int length); |
| 250 // "Bytes" is a blob with no length. The caller must specify the lenght both | 257 // "Bytes" is a blob with no length. The caller must specify the lenght both |
| 251 // when reading and writing. It is normally used to serialize PoD types of a | 258 // when reading and writing. It is normally used to serialize PoD types of a |
| 252 // known size. See also WriteData. | 259 // known size. See also WriteData. |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 266 // with BeginWriteData), the Pickle can | 273 // with BeginWriteData), the Pickle can |
| 267 // be 'trimmed' if the amount of data required is less than originally | 274 // be 'trimmed' if the amount of data required is less than originally |
| 268 // requested. For example, you may have created a buffer with 10K of data, | 275 // requested. For example, you may have created a buffer with 10K of data, |
| 269 // but decided to only fill 10 bytes of that data. Use this function | 276 // but decided to only fill 10 bytes of that data. Use this function |
| 270 // to trim the buffer so that we don't send 9990 bytes of unused data. | 277 // to trim the buffer so that we don't send 9990 bytes of unused data. |
| 271 // You cannot increase the size of the variable buffer; only shrink it. | 278 // You cannot increase the size of the variable buffer; only shrink it. |
| 272 // This function assumes that the length of the variable buffer has | 279 // This function assumes that the length of the variable buffer has |
| 273 // not been changed. | 280 // not been changed. |
| 274 void TrimWriteData(int length); | 281 void TrimWriteData(int length); |
| 275 | 282 |
| 283 void Reserve(size_t size); | |
| 284 | |
| 276 // Payload follows after allocation of Header (header size is customizable). | 285 // Payload follows after allocation of Header (header size is customizable). |
| 277 struct Header { | 286 struct Header { |
| 278 uint32 payload_size; // Specifies the size of the payload. | 287 uint32 payload_size; // Specifies the size of the payload. |
| 279 }; | 288 }; |
| 280 | 289 |
| 281 // Returns the header, cast to a user-specified type T. The type T must be a | 290 // Returns the header, cast to a user-specified type T. The type T must be a |
| 282 // subclass of Header and its size must correspond to the header_size passed | 291 // subclass of Header and its size must correspond to the header_size passed |
| 283 // to the Pickle constructor. | 292 // to the Pickle constructor. |
| 284 template <class T> | 293 template <class T> |
| 285 T* headerT() { | 294 T* headerT() { |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 347 static const int kPayloadUnit; | 356 static const int kPayloadUnit; |
| 348 | 357 |
| 349 private: | 358 private: |
| 350 friend class PickleIterator; | 359 friend class PickleIterator; |
| 351 | 360 |
| 352 Header* header_; | 361 Header* header_; |
| 353 size_t header_size_; // Supports extra data between header and payload. | 362 size_t header_size_; // Supports extra data between header and payload. |
| 354 // Allocation size of payload (or -1 if allocation is const). | 363 // Allocation size of payload (or -1 if allocation is const). |
| 355 size_t capacity_; | 364 size_t capacity_; |
| 356 size_t variable_buffer_offset_; // IF non-zero, then offset to a buffer. | 365 size_t variable_buffer_offset_; // IF non-zero, then offset to a buffer. |
| 366 size_t offset_; | |
| 367 | |
| 368 template<size_t length> void WriteBytesStatic(const void* data); | |
| 357 | 369 |
| 358 FRIEND_TEST_ALL_PREFIXES(PickleTest, Resize); | 370 FRIEND_TEST_ALL_PREFIXES(PickleTest, Resize); |
| 359 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNext); | 371 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNext); |
| 360 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNextWithIncompleteHeader); | 372 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNextWithIncompleteHeader); |
| 361 }; | 373 }; |
| 362 | 374 |
| 375 #if 0 | |
| 376 template <size_t length> void Pickle::WriteBytesStatic(const void* data) { | |
| 377 //DCHECK_NE(kCapacityReadOnly, capacity_) << "oops: pickle is readonly"; | |
| 378 #ifdef ARCH_CPU_64_BITS | |
| 379 DCHECK_LE(length, kuint32max); | |
|
danakj
2013/10/22 17:22:05
how come? and why ifdefed only on 64bit?
piman
2013/10/24 06:17:14
This comes from the original BeginWriteData.
I ass
| |
| 380 #endif | |
| 381 size_t data_len = AlignInt(length, sizeof(uint32)); | |
| 382 size_t new_size = offset_ + data_len; | |
| 383 if (new_size > capacity_) { | |
| 384 Resize(std::max(capacity_ * 2, new_size)); | |
| 385 } | |
| 386 | |
| 387 char* write = mutable_payload() + offset_; | |
| 388 memcpy(write, data, length); | |
| 389 memset(write + length, 0, data_len - length); | |
| 390 offset_ = new_size; | |
| 391 header_->payload_size = new_size; | |
| 392 } | |
| 393 #endif | |
| 394 | |
| 363 #endif // BASE_PICKLE_H__ | 395 #endif // BASE_PICKLE_H__ |
| OLD | NEW |