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

Side by Side Diff: chrome/browser/sync/util/row_iterator.h

Issue 194065: Initial commit of sync engine code to browser/sync.... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Fixes to gtest include path, reverted syncapi. Created 11 years, 3 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
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // A handy type for iterating through query results.
6 //
7 // Just define your Traits type with
8 //
9 // RowType
10 // Extract(statement*, RowType);
11 //
12 // and then pass an sqlite3_stmt into the constructor for begin,
13 // and use the no-arg constructor for an end iterator. Ex:
14 //
15 // for (RowIterator<SomeTraits> i(statement), end; i != end; ++i)
16 // ...
17
18 #ifndef CHROME_BROWSER_SYNC_UTIL_ROW_ITERATOR_H_
19 #define CHROME_BROWSER_SYNC_UTIL_ROW_ITERATOR_H_
20
21 #include "base/logging.h"
22 #include "third_party/sqlite/preprocessed/sqlite3.h"
23
24 template <typename ColumnType, int index = 0>
25 struct SingleColumnTraits {
26 typedef ColumnType RowType;
27 inline void Extract(sqlite3_stmt* statement, ColumnType* x) const {
28 GetColumn(statement, index, x);
29 }
30 };
31
32 template <typename RowTraits>
33 class RowIterator : public std::iterator<std::input_iterator_tag,
34 const typename RowTraits::RowType> {
35 public:
36 typedef typename RowTraits::RowType RowType;
37 // Statement must have been prepared, but not yet stepped:
38 RowIterator(sqlite3_stmt* statement, RowTraits traits = RowTraits()) {
39 kernel_ = new Kernel;
40 kernel_->done = false;
41 kernel_->refcount = 1;
42 kernel_->statement = statement;
43 kernel_->row_traits = traits;
44 ++(*this);
45 }
46 RowIterator() : kernel_(NULL) { } // creates end iterator
47
48 RowIterator(const RowIterator& i)
49 : kernel_(NULL) {
50 *this = i;
51 }
52
53 ~RowIterator() {
54 if (kernel_ && 0 == --(kernel_->refcount)) {
55 sqlite3_finalize(kernel_->statement);
56 delete kernel_;
57 }
58 }
59
60 RowIterator& operator = (const RowIterator& i) {
61 if (kernel_ && (0 == --(kernel_->refcount))) {
62 sqlite3_finalize(kernel_->statement);
63 delete kernel_;
64 }
65 kernel_ = i.kernel_;
66 if (kernel_)
67 kernel_->refcount += 1;
68 return *this;
69 }
70
71 RowIterator operator ++(int) {
72 RowIterator i(*this);
73 return ++i;
74 }
75
76 RowIterator& operator ++() {
77 DCHECK(NULL != kernel_);
78 if (SQLITE_ROW == sqlite3_step(kernel_->statement)) {
79 kernel_->row_traits.Extract(kernel_->statement, &kernel_->row);
80 } else {
81 kernel_->done = true;
82 }
83 return *this;
84 }
85
86 const RowType& operator *() const {
87 return *(operator -> ());
88 }
89
90 const RowType* operator ->() const {
91 DCHECK(NULL != kernel_);
92 DCHECK(!kernel_->done);
93 return &(kernel_->row);
94 }
95
96 bool operator == (const RowIterator& i) const {
97 if (kernel_ == i.kernel_)
98 return true;
99 if (NULL == kernel_ && i.kernel_->done)
100 return true;
101 if (NULL == i.kernel_ && kernel_->done)
102 return true;
103 return false;
104 }
105
106 bool operator != (const RowIterator& i) const {
107 return !(*this == i);
108 }
109
110 protected:
111 struct Kernel {
112 int refcount;
113 bool done;
114 RowType row;
115 sqlite3_stmt* statement;
116 RowTraits row_traits;
117 };
118
119 Kernel* kernel_;
120 };
121
122 #endif // CHROME_BROWSER_SYNC_UTIL_ROW_ITERATOR_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698