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

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') | base/pickle.cc » ('J')
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.
30 bool ReadBool(bool* result) WARN_UNUSED_RESULT; 30 bool ReadBool(bool* result) WARN_UNUSED_RESULT;
31 bool ReadInt(int* result) WARN_UNUSED_RESULT; 31 bool ReadInt(int* result) WARN_UNUSED_RESULT;
32 bool ReadLong(long* result) WARN_UNUSED_RESULT; 32 bool ReadLong(long* result) WARN_UNUSED_RESULT;
33 bool ReadUInt16(uint16* result) WARN_UNUSED_RESULT; 33 bool ReadUInt16(uint16* result) WARN_UNUSED_RESULT;
(...skipping 22 matching lines...) Expand all
56 private: 56 private:
57 // Aligns 'i' by rounding it up to the next multiple of 'alignment' 57 // Aligns 'i' by rounding it up to the next multiple of 'alignment'
58 static size_t AlignInt(size_t i, int alignment) { 58 static size_t AlignInt(size_t i, int alignment) {
59 return i + (alignment - (i % alignment)) % alignment; 59 return i + (alignment - (i % alignment)) % alignment;
60 } 60 }
61 61
62 // Read Type from Pickle. 62 // Read Type from Pickle.
63 template <typename Type> 63 template <typename Type>
64 inline bool ReadBuiltinType(Type* result); 64 inline bool ReadBuiltinType(Type* result);
65 65
66 // Advance read_index_ but do not allow it to exceed end_index_.
67 inline void Advance(size_t size);
68
66 // Get read pointer for Type and advance read pointer. 69 // Get read pointer for Type and advance read pointer.
67 template<typename Type> 70 template<typename Type>
68 inline const char* GetReadPointerAndAdvance(); 71 inline const char* GetReadPointerAndAdvance();
69 72
70 // Get read pointer for |num_bytes| and advance read pointer. This method 73 // Get read pointer for |num_bytes| and advance read pointer. This method
71 // checks num_bytes for negativity and wrapping. 74 // checks num_bytes for negativity and wrapping.
72 const char* GetReadPointerAndAdvance(int num_bytes); 75 const char* GetReadPointerAndAdvance(int num_bytes);
73 76
74 // Get read pointer for (num_elements * size_element) bytes and advance read 77 // Get read pointer for (num_elements * size_element) bytes and advance read
75 // pointer. This method checks for int overflow, negativity and wrapping. 78 // pointer. This method checks for int overflow, negativity and wrapping.
76 inline const char* GetReadPointerAndAdvance(int num_elements, 79 inline const char* GetReadPointerAndAdvance(int num_elements,
77 size_t size_element); 80 size_t size_element);
78 81
79 // Pointers to the Pickle data. 82 // Pointers to the Pickle data.
80 const char* read_ptr_; 83 const char* payload_ptr_;
81 const char* read_end_ptr_; 84 size_t read_index_;
85 size_t end_index_;
82 86
83 FRIEND_TEST_ALL_PREFIXES(PickleTest, GetReadPointerAndAdvance); 87 FRIEND_TEST_ALL_PREFIXES(PickleTest, GetReadPointerAndAdvance);
84 }; 88 };
85 89
86 // This class provides facilities for basic binary value packing and unpacking. 90 // This class provides facilities for basic binary value packing and unpacking.
87 // 91 //
88 // The Pickle class supports appending primitive values (ints, strings, etc.) 92 // The Pickle class supports appending primitive values (ints, strings, etc.)
89 // to a pickle instance. The Pickle instance grows its internal memory buffer 93 // to a pickle instance. The Pickle instance grows its internal memory buffer
90 // dynamically to hold the sequence of primitive values. The internal memory 94 // 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 95 // 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)); 274 DCHECK_EQ(header_size_, sizeof(T));
271 return static_cast<T*>(header_); 275 return static_cast<T*>(header_);
272 } 276 }
273 template <class T> 277 template <class T>
274 const T* headerT() const { 278 const T* headerT() const {
275 DCHECK_EQ(header_size_, sizeof(T)); 279 DCHECK_EQ(header_size_, sizeof(T));
276 return static_cast<const T*>(header_); 280 return static_cast<const T*>(header_);
277 } 281 }
278 282
279 // The payload is the pickle data immediately following the header. 283 // The payload is the pickle data immediately following the header.
280 size_t payload_size() const { return header_->payload_size; } 284 size_t payload_size() const {
285 return header_ ? header_->payload_size : 0;
286 }
281 287
282 const char* payload() const { 288 const char* payload() const {
283 return reinterpret_cast<const char*>(header_) + header_size_; 289 return reinterpret_cast<const char*>(header_) + header_size_;
284 } 290 }
285 291
286 // Returns the address of the byte immediately following the currently valid 292 // Returns the address of the byte immediately following the currently valid
287 // header + payload. 293 // header + payload.
288 const char* end_of_payload() const { 294 const char* end_of_payload() const {
289 // This object may be invalid. 295 // This object may be invalid.
290 return header_ ? payload() + payload_size() : NULL; 296 return header_ ? payload() + payload_size() : NULL;
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
339 } 345 }
340 inline void WriteBytesCommon(const void* data, size_t length); 346 inline void WriteBytesCommon(const void* data, size_t length);
341 347
342 FRIEND_TEST_ALL_PREFIXES(PickleTest, Resize); 348 FRIEND_TEST_ALL_PREFIXES(PickleTest, Resize);
343 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNext); 349 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNext);
344 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNextWithIncompleteHeader); 350 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNextWithIncompleteHeader);
345 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNextOverflow); 351 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNextOverflow);
346 }; 352 };
347 353
348 #endif // BASE_PICKLE_H__ 354 #endif // BASE_PICKLE_H__
OLDNEW
« no previous file with comments | « no previous file | base/pickle.cc » ('j') | base/pickle.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698