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 <stddef.h> | 8 #include <stddef.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 | 10 |
(...skipping 27 matching lines...) Expand all Loading... |
38 // methods return true. Otherwise, false is returned to indicate that the | 38 // methods return true. Otherwise, false is returned to indicate that the |
39 // result could not be extracted. It is not possible to read from the iterator | 39 // result could not be extracted. It is not possible to read from the iterator |
40 // after that. | 40 // after that. |
41 bool ReadBool(bool* result) WARN_UNUSED_RESULT; | 41 bool ReadBool(bool* result) WARN_UNUSED_RESULT; |
42 bool ReadInt(int* result) WARN_UNUSED_RESULT; | 42 bool ReadInt(int* result) WARN_UNUSED_RESULT; |
43 bool ReadLong(long* result) WARN_UNUSED_RESULT; | 43 bool ReadLong(long* result) WARN_UNUSED_RESULT; |
44 bool ReadUInt16(uint16_t* result) WARN_UNUSED_RESULT; | 44 bool ReadUInt16(uint16_t* result) WARN_UNUSED_RESULT; |
45 bool ReadUInt32(uint32_t* result) WARN_UNUSED_RESULT; | 45 bool ReadUInt32(uint32_t* result) WARN_UNUSED_RESULT; |
46 bool ReadInt64(int64_t* result) WARN_UNUSED_RESULT; | 46 bool ReadInt64(int64_t* result) WARN_UNUSED_RESULT; |
47 bool ReadUInt64(uint64_t* result) WARN_UNUSED_RESULT; | 47 bool ReadUInt64(uint64_t* result) WARN_UNUSED_RESULT; |
48 bool ReadSizeT(size_t* result) WARN_UNUSED_RESULT; | |
49 bool ReadFloat(float* result) WARN_UNUSED_RESULT; | 48 bool ReadFloat(float* result) WARN_UNUSED_RESULT; |
50 bool ReadDouble(double* result) WARN_UNUSED_RESULT; | 49 bool ReadDouble(double* result) WARN_UNUSED_RESULT; |
51 bool ReadString(std::string* result) WARN_UNUSED_RESULT; | 50 bool ReadString(std::string* result) WARN_UNUSED_RESULT; |
52 // The StringPiece data will only be valid for the lifetime of the message. | 51 // The StringPiece data will only be valid for the lifetime of the message. |
53 bool ReadStringPiece(StringPiece* result) WARN_UNUSED_RESULT; | 52 bool ReadStringPiece(StringPiece* result) WARN_UNUSED_RESULT; |
54 bool ReadString16(string16* result) WARN_UNUSED_RESULT; | 53 bool ReadString16(string16* result) WARN_UNUSED_RESULT; |
55 // The StringPiece16 data will only be valid for the lifetime of the message. | 54 // The StringPiece16 data will only be valid for the lifetime of the message. |
56 bool ReadStringPiece16(StringPiece16* result) WARN_UNUSED_RESULT; | 55 bool ReadStringPiece16(StringPiece16* result) WARN_UNUSED_RESULT; |
57 | 56 |
58 // A pointer to the data will be placed in |*data|, and the length will be | 57 // A pointer to the data will be placed in |*data|, and the length will be |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 class BASE_EXPORT PickleSizer { | 114 class BASE_EXPORT PickleSizer { |
116 public: | 115 public: |
117 PickleSizer(); | 116 PickleSizer(); |
118 ~PickleSizer(); | 117 ~PickleSizer(); |
119 | 118 |
120 // Returns the computed size of the payload. | 119 // Returns the computed size of the payload. |
121 size_t payload_size() const { return payload_size_; } | 120 size_t payload_size() const { return payload_size_; } |
122 | 121 |
123 void AddBool() { return AddInt(); } | 122 void AddBool() { return AddInt(); } |
124 void AddInt() { AddPOD<int>(); } | 123 void AddInt() { AddPOD<int>(); } |
125 void AddLongUsingDangerousNonPortableLessPersistableForm() { AddPOD<long>(); } | 124 void AddLong() { AddPOD<uint64_t>(); } |
126 void AddUInt16() { return AddPOD<uint16_t>(); } | 125 void AddUInt16() { return AddPOD<uint16_t>(); } |
127 void AddUInt32() { return AddPOD<uint32_t>(); } | 126 void AddUInt32() { return AddPOD<uint32_t>(); } |
128 void AddInt64() { return AddPOD<int64_t>(); } | 127 void AddInt64() { return AddPOD<int64_t>(); } |
129 void AddUInt64() { return AddPOD<uint64_t>(); } | 128 void AddUInt64() { return AddPOD<uint64_t>(); } |
130 void AddSizeT() { return AddPOD<uint64_t>(); } | |
131 void AddFloat() { return AddPOD<float>(); } | 129 void AddFloat() { return AddPOD<float>(); } |
132 void AddDouble() { return AddPOD<double>(); } | 130 void AddDouble() { return AddPOD<double>(); } |
133 void AddString(const StringPiece& value); | 131 void AddString(const StringPiece& value); |
134 void AddString16(const StringPiece16& value); | 132 void AddString16(const StringPiece16& value); |
135 void AddData(int length); | 133 void AddData(int length); |
136 void AddBytes(int length); | 134 void AddBytes(int length); |
137 | 135 |
138 private: | 136 private: |
139 // Just like AddBytes() but with a compile-time size for performance. | 137 // Just like AddBytes() but with a compile-time size for performance. |
140 template<size_t length> void BASE_EXPORT AddBytesStatic(); | 138 template<size_t length> void BASE_EXPORT AddBytesStatic(); |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
222 // appended to the end of the Pickle's payload. When reading values from a | 220 // appended to the end of the Pickle's payload. When reading values from a |
223 // Pickle, it is important to read them in the order in which they were added | 221 // Pickle, it is important to read them in the order in which they were added |
224 // to the Pickle. | 222 // to the Pickle. |
225 | 223 |
226 bool WriteBool(bool value) { | 224 bool WriteBool(bool value) { |
227 return WriteInt(value ? 1 : 0); | 225 return WriteInt(value ? 1 : 0); |
228 } | 226 } |
229 bool WriteInt(int value) { | 227 bool WriteInt(int value) { |
230 return WritePOD(value); | 228 return WritePOD(value); |
231 } | 229 } |
232 // WARNING: DO NOT USE THIS METHOD IF PICKLES ARE PERSISTED IN ANY WAY. | 230 bool WriteLong(long value) { |
233 // It will write whatever a "long" is on this architecture. On 32-bit | 231 // Always write long as a 64-bit value to ensure compatibility between |
234 // platforms, it is 32 bits. On 64-bit platforms, it is 64 bits. If persisted | 232 // 32-bit and 64-bit processes. |
235 // pickles are still around after upgrading to 64-bit, or if they are copied | 233 return WritePOD(static_cast<int64_t>(value)); |
236 // between dissimilar systems, YOUR PICKLES WILL HAVE GONE BAD. | |
237 bool WriteLongUsingDangerousNonPortableLessPersistableForm(long value) { | |
238 return WritePOD(value); | |
239 } | 234 } |
240 bool WriteUInt16(uint16_t value) { return WritePOD(value); } | 235 bool WriteUInt16(uint16_t value) { return WritePOD(value); } |
241 bool WriteUInt32(uint32_t value) { return WritePOD(value); } | 236 bool WriteUInt32(uint32_t value) { return WritePOD(value); } |
242 bool WriteInt64(int64_t value) { return WritePOD(value); } | 237 bool WriteInt64(int64_t value) { return WritePOD(value); } |
243 bool WriteUInt64(uint64_t value) { return WritePOD(value); } | 238 bool WriteUInt64(uint64_t value) { return WritePOD(value); } |
244 bool WriteSizeT(size_t value) { | |
245 // Always write size_t as a 64-bit value to ensure compatibility between | |
246 // 32-bit and 64-bit processes. | |
247 return WritePOD(static_cast<uint64_t>(value)); | |
248 } | |
249 bool WriteFloat(float value) { | 239 bool WriteFloat(float value) { |
250 return WritePOD(value); | 240 return WritePOD(value); |
251 } | 241 } |
252 bool WriteDouble(double value) { | 242 bool WriteDouble(double value) { |
253 return WritePOD(value); | 243 return WritePOD(value); |
254 } | 244 } |
255 bool WriteString(const StringPiece& value); | 245 bool WriteString(const StringPiece& value); |
256 bool WriteString16(const StringPiece16& value); | 246 bool WriteString16(const StringPiece16& value); |
257 // "Data" is a blob with a length. When you read it out you will be given the | 247 // "Data" is a blob with a length. When you read it out you will be given the |
258 // length. See also WriteBytes. | 248 // length. See also WriteBytes. |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
385 FRIEND_TEST_ALL_PREFIXES(PickleTest, PeekNext); | 375 FRIEND_TEST_ALL_PREFIXES(PickleTest, PeekNext); |
386 FRIEND_TEST_ALL_PREFIXES(PickleTest, PeekNextOverflow); | 376 FRIEND_TEST_ALL_PREFIXES(PickleTest, PeekNextOverflow); |
387 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNext); | 377 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNext); |
388 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNextWithIncompleteHeader); | 378 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNextWithIncompleteHeader); |
389 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNextOverflow); | 379 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNextOverflow); |
390 }; | 380 }; |
391 | 381 |
392 } // namespace base | 382 } // namespace base |
393 | 383 |
394 #endif // BASE_PICKLE_H_ | 384 #endif // BASE_PICKLE_H_ |
OLD | NEW |