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

Side by Side Diff: base/pickle.h

Issue 290173008: Refactor PickleIterator. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 6 years, 7 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | base/pickle.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 16
17 class Pickle; 17 class Pickle;
18 18
19 // PickleIterator reads data from a Pickle. The Pickle object must remain valid 19 // PickleIterator reads data from a Pickle. The Pickle object must remain valid
20 // while the PickleIterator object is in use. 20 // while the PickleIterator object is in use.
21 class BASE_EXPORT PickleIterator { 21 class BASE_EXPORT PickleIterator {
22 public: 22 public:
23 PickleIterator() : read_ptr_(NULL), read_end_ptr_(NULL) {} 23 PickleIterator() : payload_ptr_(NULL), read_index_(0), end_index_(0) {}
24 explicit PickleIterator(const Pickle& pickle); 24 explicit PickleIterator(const Pickle& pickle);
25 25
26 // Methods for reading the payload of the Pickle. To read from the start of 26 // 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 27 // the Pickle, create a PickleIterator from a Pickle. If successful, these
28 // methods return true. Otherwise, false is returned to indicate that the 28 // methods return true. Otherwise, false is returned to indicate that the
29 // result could not be extracted. 29 // result could not be extracted. It is not possible to read from iterator
30 // after that.
30 bool ReadBool(bool* result) WARN_UNUSED_RESULT; 31 bool ReadBool(bool* result) WARN_UNUSED_RESULT;
31 bool ReadInt(int* result) WARN_UNUSED_RESULT; 32 bool ReadInt(int* result) WARN_UNUSED_RESULT;
32 bool ReadLong(long* result) WARN_UNUSED_RESULT; 33 bool ReadLong(long* result) WARN_UNUSED_RESULT;
33 bool ReadUInt16(uint16* result) WARN_UNUSED_RESULT; 34 bool ReadUInt16(uint16* result) WARN_UNUSED_RESULT;
34 bool ReadUInt32(uint32* result) WARN_UNUSED_RESULT; 35 bool ReadUInt32(uint32* result) WARN_UNUSED_RESULT;
35 bool ReadInt64(int64* result) WARN_UNUSED_RESULT; 36 bool ReadInt64(int64* result) WARN_UNUSED_RESULT;
36 bool ReadUInt64(uint64* result) WARN_UNUSED_RESULT; 37 bool ReadUInt64(uint64* result) WARN_UNUSED_RESULT;
37 bool ReadFloat(float* result) WARN_UNUSED_RESULT; 38 bool ReadFloat(float* result) WARN_UNUSED_RESULT;
38 bool ReadString(std::string* result) WARN_UNUSED_RESULT; 39 bool ReadString(std::string* result) WARN_UNUSED_RESULT;
39 bool ReadWString(std::wstring* result) WARN_UNUSED_RESULT; 40 bool ReadWString(std::wstring* result) WARN_UNUSED_RESULT;
(...skipping 16 matching lines...) Expand all
56 private: 57 private:
57 // Aligns 'i' by rounding it up to the next multiple of 'alignment' 58 // Aligns 'i' by rounding it up to the next multiple of 'alignment'
58 static size_t AlignInt(size_t i, int alignment) { 59 static size_t AlignInt(size_t i, int alignment) {
59 return i + (alignment - (i % alignment)) % alignment; 60 return i + (alignment - (i % alignment)) % alignment;
60 } 61 }
61 62
62 // Read Type from Pickle. 63 // Read Type from Pickle.
63 template <typename Type> 64 template <typename Type>
64 inline bool ReadBuiltinType(Type* result); 65 inline bool ReadBuiltinType(Type* result);
65 66
67 // Advance read_index_ but do not allow it to exceed end_index_.
68 // Keeps read_index_ aligned.
69 inline void Advance(size_t size);
70
66 // Get read pointer for Type and advance read pointer. 71 // Get read pointer for Type and advance read pointer.
67 template<typename Type> 72 template<typename Type>
68 inline const char* GetReadPointerAndAdvance(); 73 inline const char* GetReadPointerAndAdvance();
69 74
70 // Get read pointer for |num_bytes| and advance read pointer. This method 75 // Get read pointer for |num_bytes| and advance read pointer. This method
71 // checks num_bytes for negativity and wrapping. 76 // checks num_bytes for negativity and wrapping.
72 const char* GetReadPointerAndAdvance(int num_bytes); 77 const char* GetReadPointerAndAdvance(int num_bytes);
73 78
74 // Get read pointer for (num_elements * size_element) bytes and advance read 79 // Get read pointer for (num_elements * size_element) bytes and advance read
75 // pointer. This method checks for int overflow, negativity and wrapping. 80 // pointer. This method checks for int overflow, negativity and wrapping.
76 inline const char* GetReadPointerAndAdvance(int num_elements, 81 inline const char* GetReadPointerAndAdvance(int num_elements,
77 size_t size_element); 82 size_t size_element);
78 83
79 // Pointers to the Pickle data. 84 // Pointers to the Pickle data.
80 const char* read_ptr_; 85 const char* payload_ptr_;
jar (doing other things) 2014/05/29 01:51:16 nit: Now that we're not advancing this ptr, perha
halyavin 2014/05/29 07:34:55 Done.
81 const char* read_end_ptr_; 86 size_t read_index_;
87 size_t end_index_;
82 88
83 FRIEND_TEST_ALL_PREFIXES(PickleTest, GetReadPointerAndAdvance); 89 FRIEND_TEST_ALL_PREFIXES(PickleTest, GetReadPointerAndAdvance);
84 }; 90 };
85 91
86 // This class provides facilities for basic binary value packing and unpacking. 92 // This class provides facilities for basic binary value packing and unpacking.
87 // 93 //
88 // The Pickle class supports appending primitive values (ints, strings, etc.) 94 // The Pickle class supports appending primitive values (ints, strings, etc.)
89 // to a pickle instance. The Pickle instance grows its internal memory buffer 95 // to a pickle instance. The Pickle instance grows its internal memory buffer
90 // dynamically to hold the sequence of primitive values. The internal memory 96 // dynamically to hold the sequence of primitive values. The internal memory
91 // buffer is exposed as the "data" of the Pickle. This "data" can be passed 97 // buffer is exposed as the "data" of the Pickle. This "data" can be passed
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after
270 DCHECK_EQ(header_size_, sizeof(T)); 276 DCHECK_EQ(header_size_, sizeof(T));
271 return static_cast<T*>(header_); 277 return static_cast<T*>(header_);
272 } 278 }
273 template <class T> 279 template <class T>
274 const T* headerT() const { 280 const T* headerT() const {
275 DCHECK_EQ(header_size_, sizeof(T)); 281 DCHECK_EQ(header_size_, sizeof(T));
276 return static_cast<const T*>(header_); 282 return static_cast<const T*>(header_);
277 } 283 }
278 284
279 // The payload is the pickle data immediately following the header. 285 // The payload is the pickle data immediately following the header.
280 size_t payload_size() const { return header_->payload_size; } 286 size_t payload_size() const {
287 return header_ ? header_->payload_size : 0;
288 }
281 289
282 const char* payload() const { 290 const char* payload() const {
283 return reinterpret_cast<const char*>(header_) + header_size_; 291 return reinterpret_cast<const char*>(header_) + header_size_;
284 } 292 }
285 293
286 // Returns the address of the byte immediately following the currently valid 294 // Returns the address of the byte immediately following the currently valid
287 // header + payload. 295 // header + payload.
288 const char* end_of_payload() const { 296 const char* end_of_payload() const {
289 // This object may be invalid. 297 // This object may be invalid.
290 return header_ ? payload() + payload_size() : NULL; 298 return header_ ? payload() + payload_size() : NULL;
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
339 } 347 }
340 inline void WriteBytesCommon(const void* data, size_t length); 348 inline void WriteBytesCommon(const void* data, size_t length);
341 349
342 FRIEND_TEST_ALL_PREFIXES(PickleTest, Resize); 350 FRIEND_TEST_ALL_PREFIXES(PickleTest, Resize);
343 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNext); 351 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNext);
344 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNextWithIncompleteHeader); 352 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNextWithIncompleteHeader);
345 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNextOverflow); 353 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNextOverflow);
346 }; 354 };
347 355
348 #endif // BASE_PICKLE_H__ 356 #endif // BASE_PICKLE_H__
OLDNEW
« 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