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" |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
13 #include "base/gtest_prod_util.h" | 13 #include "base/gtest_prod_util.h" |
14 #include "base/logging.h" | 14 #include "base/logging.h" |
15 #include "base/strings/string16.h" | 15 #include "base/strings/string16.h" |
| 16 #include "base/strings/string_piece.h" |
16 | 17 |
17 class Pickle; | 18 class Pickle; |
18 | 19 |
19 // PickleIterator reads data from a Pickle. The Pickle object must remain valid | 20 // PickleIterator reads data from a Pickle. The Pickle object must remain valid |
20 // while the PickleIterator object is in use. | 21 // while the PickleIterator object is in use. |
21 class BASE_EXPORT PickleIterator { | 22 class BASE_EXPORT PickleIterator { |
22 public: | 23 public: |
23 PickleIterator() : payload_(NULL), read_index_(0), end_index_(0) {} | 24 PickleIterator() : payload_(NULL), read_index_(0), end_index_(0) {} |
24 explicit PickleIterator(const Pickle& pickle); | 25 explicit PickleIterator(const Pickle& pickle); |
25 | 26 |
26 // Methods for reading the payload of the Pickle. To read from the start of | 27 // Methods for reading the payload of the Pickle. To read from the start of |
27 // the Pickle, create a PickleIterator from a Pickle. If successful, these | 28 // the Pickle, create a PickleIterator from a Pickle. If successful, these |
28 // methods return true. Otherwise, false is returned to indicate that the | 29 // methods return true. Otherwise, false is returned to indicate that the |
29 // result could not be extracted. It is not possible to read from the iterator | 30 // result could not be extracted. It is not possible to read from the iterator |
30 // after that. | 31 // after that. |
31 bool ReadBool(bool* result) WARN_UNUSED_RESULT; | 32 bool ReadBool(bool* result) WARN_UNUSED_RESULT; |
32 bool ReadInt(int* result) WARN_UNUSED_RESULT; | 33 bool ReadInt(int* result) WARN_UNUSED_RESULT; |
33 bool ReadLong(long* result) WARN_UNUSED_RESULT; | 34 bool ReadLong(long* result) WARN_UNUSED_RESULT; |
34 bool ReadUInt16(uint16* result) WARN_UNUSED_RESULT; | 35 bool ReadUInt16(uint16* result) WARN_UNUSED_RESULT; |
35 bool ReadUInt32(uint32* result) WARN_UNUSED_RESULT; | 36 bool ReadUInt32(uint32* result) WARN_UNUSED_RESULT; |
36 bool ReadInt64(int64* result) WARN_UNUSED_RESULT; | 37 bool ReadInt64(int64* result) WARN_UNUSED_RESULT; |
37 bool ReadUInt64(uint64* result) WARN_UNUSED_RESULT; | 38 bool ReadUInt64(uint64* result) WARN_UNUSED_RESULT; |
38 bool ReadSizeT(size_t* result) WARN_UNUSED_RESULT; | 39 bool ReadSizeT(size_t* result) WARN_UNUSED_RESULT; |
39 bool ReadFloat(float* result) WARN_UNUSED_RESULT; | 40 bool ReadFloat(float* result) WARN_UNUSED_RESULT; |
40 bool ReadDouble(double* result) WARN_UNUSED_RESULT; | 41 bool ReadDouble(double* result) WARN_UNUSED_RESULT; |
41 bool ReadString(std::string* result) WARN_UNUSED_RESULT; | 42 bool ReadString(std::string* result) WARN_UNUSED_RESULT; |
| 43 // The StringPiece data will only be valid for the lifetime of the message. |
| 44 bool ReadStringPiece(base::StringPiece* result) WARN_UNUSED_RESULT; |
42 bool ReadWString(std::wstring* result) WARN_UNUSED_RESULT; | 45 bool ReadWString(std::wstring* result) WARN_UNUSED_RESULT; |
43 bool ReadString16(base::string16* result) WARN_UNUSED_RESULT; | 46 bool ReadString16(base::string16* result) WARN_UNUSED_RESULT; |
| 47 // The StringPiece16 data will only be valid for the lifetime of the message. |
| 48 bool ReadStringPiece16(base::StringPiece16* result) WARN_UNUSED_RESULT; |
44 | 49 |
45 // A pointer to the data will be placed in |*data|, and the length will be | 50 // A pointer to the data will be placed in |*data|, and the length will be |
46 // placed in |*length|. The pointer placed into |*data| points into the | 51 // placed in |*length|. The pointer placed into |*data| points into the |
47 // message's buffer so it will be scoped to the lifetime of the message (or | 52 // message's buffer so it will be scoped to the lifetime of the message (or |
48 // until the message data is mutated). Do not keep the pointer around! | 53 // until the message data is mutated). Do not keep the pointer around! |
49 bool ReadData(const char** data, int* length) WARN_UNUSED_RESULT; | 54 bool ReadData(const char** data, int* length) WARN_UNUSED_RESULT; |
50 | 55 |
51 // A pointer to the data will be placed in |*data|. The caller specifies the | 56 // A pointer to the data will be placed in |*data|. The caller specifies the |
52 // number of bytes to read, and ReadBytes will validate this length. The | 57 // number of bytes to read, and ReadBytes will validate this length. The |
53 // pointer placed into |*data| points into the message's buffer so it will be | 58 // pointer placed into |*data| points into the message's buffer so it will be |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
188 // Always write size_t as a 64-bit value to ensure compatibility between | 193 // Always write size_t as a 64-bit value to ensure compatibility between |
189 // 32-bit and 64-bit processes. | 194 // 32-bit and 64-bit processes. |
190 return WritePOD(static_cast<uint64>(value)); | 195 return WritePOD(static_cast<uint64>(value)); |
191 } | 196 } |
192 bool WriteFloat(float value) { | 197 bool WriteFloat(float value) { |
193 return WritePOD(value); | 198 return WritePOD(value); |
194 } | 199 } |
195 bool WriteDouble(double value) { | 200 bool WriteDouble(double value) { |
196 return WritePOD(value); | 201 return WritePOD(value); |
197 } | 202 } |
198 bool WriteString(const std::string& value); | 203 bool WriteString(const base::StringPiece& value); |
199 bool WriteWString(const std::wstring& value); | 204 bool WriteWString(const std::wstring& value); |
200 bool WriteString16(const base::string16& value); | 205 bool WriteString16(const base::StringPiece16& value); |
201 // "Data" is a blob with a length. When you read it out you will be given the | 206 // "Data" is a blob with a length. When you read it out you will be given the |
202 // length. See also WriteBytes. | 207 // length. See also WriteBytes. |
203 bool WriteData(const char* data, int length); | 208 bool WriteData(const char* data, int length); |
204 // "Bytes" is a blob with no length. The caller must specify the length both | 209 // "Bytes" is a blob with no length. The caller must specify the length both |
205 // when reading and writing. It is normally used to serialize PoD types of a | 210 // when reading and writing. It is normally used to serialize PoD types of a |
206 // known size. See also WriteData. | 211 // known size. See also WriteData. |
207 bool WriteBytes(const void* data, int length); | 212 bool WriteBytes(const void* data, int length); |
208 | 213 |
209 // Reserves space for upcoming writes when multiple writes will be made and | 214 // Reserves space for upcoming writes when multiple writes will be made and |
210 // their sizes are computed in advance. It can be significantly faster to call | 215 // their sizes are computed in advance. It can be significantly faster to call |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
295 } | 300 } |
296 inline void WriteBytesCommon(const void* data, size_t length); | 301 inline void WriteBytesCommon(const void* data, size_t length); |
297 | 302 |
298 FRIEND_TEST_ALL_PREFIXES(PickleTest, Resize); | 303 FRIEND_TEST_ALL_PREFIXES(PickleTest, Resize); |
299 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNext); | 304 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNext); |
300 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNextWithIncompleteHeader); | 305 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNextWithIncompleteHeader); |
301 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNextOverflow); | 306 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNextOverflow); |
302 }; | 307 }; |
303 | 308 |
304 #endif // BASE_PICKLE_H__ | 309 #endif // BASE_PICKLE_H__ |
OLD | NEW |