OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 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 | 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 CHROME_BROWSER_SYNC_SYNCABLE_SYNCABLE_H_ | 5 #ifndef CHROME_BROWSER_SYNC_SYNCABLE_SYNCABLE_H_ |
6 #define CHROME_BROWSER_SYNC_SYNCABLE_SYNCABLE_H_ | 6 #define CHROME_BROWSER_SYNC_SYNCABLE_SYNCABLE_H_ |
7 | 7 |
8 #include <algorithm> | 8 #include <algorithm> |
9 #include <bitset> | 9 #include <bitset> |
10 #include <iosfwd> | 10 #include <iosfwd> |
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
214 | 214 |
215 enum CreateNewUpdateItem { | 215 enum CreateNewUpdateItem { |
216 CREATE_NEW_UPDATE_ITEM | 216 CREATE_NEW_UPDATE_ITEM |
217 }; | 217 }; |
218 | 218 |
219 typedef std::set<std::string> AttributeKeySet; | 219 typedef std::set<std::string> AttributeKeySet; |
220 | 220 |
221 // Why the singular enums? So the code compile-time dispatches instead of | 221 // Why the singular enums? So the code compile-time dispatches instead of |
222 // runtime dispatches as it would with a single enum and an if() statement. | 222 // runtime dispatches as it would with a single enum and an if() statement. |
223 | 223 |
224 // The EntryKernel class contains the actual data for an entry. It | 224 // The EntryKernel class contains the actual data for an entry. |
225 // would be a private class, except the number of required friend | |
226 // declarations would bloat the code. | |
227 struct EntryKernel { | 225 struct EntryKernel { |
228 protected: | 226 private: |
229 std::string string_fields[STRING_FIELDS_COUNT]; | 227 std::string string_fields[STRING_FIELDS_COUNT]; |
230 Blob blob_fields[BLOB_FIELDS_COUNT]; | 228 Blob blob_fields[BLOB_FIELDS_COUNT]; |
231 int64 int64_fields[INT64_FIELDS_COUNT]; | 229 int64 int64_fields[INT64_FIELDS_COUNT]; |
232 Id id_fields[ID_FIELDS_COUNT]; | 230 Id id_fields[ID_FIELDS_COUNT]; |
233 std::bitset<BIT_FIELDS_COUNT> bit_fields; | 231 std::bitset<BIT_FIELDS_COUNT> bit_fields; |
234 std::bitset<BIT_TEMPS_COUNT> bit_temps; | 232 std::bitset<BIT_TEMPS_COUNT> bit_temps; |
235 | 233 |
236 public: | 234 public: |
237 inline void mark_dirty() { | 235 inline void mark_dirty() { |
238 dirty_ = true; | 236 dirty_ = true; |
239 } | 237 } |
240 | 238 |
241 inline void clear_dirty() { | 239 inline void clear_dirty() { |
242 dirty_ = false; | 240 dirty_ = false; |
243 } | 241 } |
244 | 242 |
245 inline bool is_dirty() const { | 243 inline bool is_dirty() const { |
246 return dirty_; | 244 return dirty_; |
247 } | 245 } |
248 | 246 |
249 // Contain all this error-prone arithmetic in one place. | 247 // Setters. |
250 inline int64& ref(MetahandleField field) { | 248 inline void put(MetahandleField field, int64 value) { |
251 return int64_fields[field - INT64_FIELDS_BEGIN]; | 249 int64_fields[field - INT64_FIELDS_BEGIN] = value; |
252 } | 250 } |
253 inline int64& ref(Int64Field field) { | 251 inline void put(Int64Field field, int64 value) { |
254 return int64_fields[field - INT64_FIELDS_BEGIN]; | 252 int64_fields[field - INT64_FIELDS_BEGIN] = value; |
255 } | 253 } |
256 inline Id& ref(IdField field) { | 254 inline void put(IdField field, const Id& value) { |
257 return id_fields[field - ID_FIELDS_BEGIN]; | 255 id_fields[field - ID_FIELDS_BEGIN] = value; |
258 } | 256 } |
259 inline int64& ref(BaseVersion field) { | 257 inline void put(BaseVersion field, int64 value) { |
260 return int64_fields[field - INT64_FIELDS_BEGIN]; | 258 int64_fields[field - INT64_FIELDS_BEGIN] = value; |
261 } | 259 } |
262 inline std::bitset<BIT_FIELDS_COUNT>::reference ref(IndexedBitField field) { | 260 inline void put(IndexedBitField field, bool value) { |
263 return bit_fields[field - BIT_FIELDS_BEGIN]; | 261 bit_fields[field - BIT_FIELDS_BEGIN] = value; |
264 } | 262 } |
265 inline std::bitset<BIT_FIELDS_COUNT>::reference ref(IsDelField field) { | 263 inline void put(IsDelField field, bool value) { |
266 return bit_fields[field - BIT_FIELDS_BEGIN]; | 264 bit_fields[field - BIT_FIELDS_BEGIN] = value; |
267 } | 265 } |
268 inline std::bitset<BIT_FIELDS_COUNT>::reference ref(BitField field) { | 266 inline void put(BitField field, bool value) { |
269 return bit_fields[field - BIT_FIELDS_BEGIN]; | 267 bit_fields[field - BIT_FIELDS_BEGIN] = value; |
270 } | 268 } |
271 inline std::string& ref(StringField field) { | 269 inline void put(StringField field, const std::string& value) { |
272 return string_fields[field - STRING_FIELDS_BEGIN]; | 270 string_fields[field - STRING_FIELDS_BEGIN] = value; |
273 } | 271 } |
274 inline Blob& ref(BlobField field) { | 272 inline void put(BlobField field, const Blob& value) { |
275 return blob_fields[field - BLOB_FIELDS_BEGIN]; | 273 blob_fields[field - BLOB_FIELDS_BEGIN] = value; |
276 } | 274 } |
277 inline std::bitset<BIT_TEMPS_COUNT>::reference ref(BitTemp field) { | 275 inline void put(BitTemp field, bool value) { |
278 return bit_temps[field - BIT_TEMPS_BEGIN]; | 276 bit_temps[field - BIT_TEMPS_BEGIN] = value; |
279 } | 277 } |
280 | 278 |
| 279 // Const ref getters. |
281 inline int64 ref(MetahandleField field) const { | 280 inline int64 ref(MetahandleField field) const { |
282 return int64_fields[field - INT64_FIELDS_BEGIN]; | 281 return int64_fields[field - INT64_FIELDS_BEGIN]; |
283 } | 282 } |
284 inline int64 ref(Int64Field field) const { | 283 inline int64 ref(Int64Field field) const { |
285 return int64_fields[field - INT64_FIELDS_BEGIN]; | 284 return int64_fields[field - INT64_FIELDS_BEGIN]; |
286 } | 285 } |
287 inline const Id& ref(IdField field) const { | 286 inline const Id& ref(IdField field) const { |
288 return id_fields[field - ID_FIELDS_BEGIN]; | 287 return id_fields[field - ID_FIELDS_BEGIN]; |
289 } | 288 } |
290 inline int64 ref(BaseVersion field) const { | 289 inline int64 ref(BaseVersion field) const { |
(...skipping 11 matching lines...) Expand all Loading... |
302 inline const std::string& ref(StringField field) const { | 301 inline const std::string& ref(StringField field) const { |
303 return string_fields[field - STRING_FIELDS_BEGIN]; | 302 return string_fields[field - STRING_FIELDS_BEGIN]; |
304 } | 303 } |
305 inline const Blob& ref(BlobField field) const { | 304 inline const Blob& ref(BlobField field) const { |
306 return blob_fields[field - BLOB_FIELDS_BEGIN]; | 305 return blob_fields[field - BLOB_FIELDS_BEGIN]; |
307 } | 306 } |
308 inline bool ref(BitTemp field) const { | 307 inline bool ref(BitTemp field) const { |
309 return bit_temps[field - BIT_TEMPS_BEGIN]; | 308 return bit_temps[field - BIT_TEMPS_BEGIN]; |
310 } | 309 } |
311 | 310 |
| 311 // Non-const, mutable ref getters for object types only. |
| 312 inline std::string& mutable_ref(StringField field) { |
| 313 return string_fields[field - STRING_FIELDS_BEGIN]; |
| 314 } |
| 315 inline Blob& mutable_ref(BlobField field) { |
| 316 return blob_fields[field - BLOB_FIELDS_BEGIN]; |
| 317 } |
| 318 inline Id& mutable_ref(IdField field) { |
| 319 return id_fields[field - ID_FIELDS_BEGIN]; |
| 320 } |
312 private: | 321 private: |
313 // Tracks whether this entry needs to be saved to the database. | 322 // Tracks whether this entry needs to be saved to the database. |
314 bool dirty_; | 323 bool dirty_; |
315 }; | 324 }; |
316 | 325 |
317 // A read-only meta entry. | 326 // A read-only meta entry. |
318 class Entry { | 327 class Entry { |
319 friend class Directory; | 328 friend class Directory; |
320 friend std::ostream& ::operator << (std::ostream& s, const Entry& e); | 329 friend std::ostream& ::operator << (std::ostream& s, const Entry& e); |
321 | 330 |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
455 inline bool Put(BitTemp field, bool value) { | 464 inline bool Put(BitTemp field, bool value) { |
456 return PutTemp(field, value); | 465 return PutTemp(field, value); |
457 } | 466 } |
458 | 467 |
459 protected: | 468 protected: |
460 | 469 |
461 template <typename FieldType, typename ValueType> | 470 template <typename FieldType, typename ValueType> |
462 inline bool PutField(FieldType field, const ValueType& value) { | 471 inline bool PutField(FieldType field, const ValueType& value) { |
463 DCHECK(kernel_); | 472 DCHECK(kernel_); |
464 if (kernel_->ref(field) != value) { | 473 if (kernel_->ref(field) != value) { |
465 kernel_->ref(field) = value; | 474 kernel_->put(field, value); |
466 kernel_->mark_dirty(); | 475 kernel_->mark_dirty(); |
467 } | 476 } |
468 return true; | 477 return true; |
469 } | 478 } |
470 | 479 |
471 template <typename TempType, typename ValueType> | 480 template <typename TempType, typename ValueType> |
472 inline bool PutTemp(TempType field, const ValueType& value) { | 481 inline bool PutTemp(TempType field, const ValueType& value) { |
473 DCHECK(kernel_); | 482 DCHECK(kernel_); |
474 kernel_->ref(field) = value; | 483 kernel_->put(field, value); |
475 return true; | 484 return true; |
476 } | 485 } |
477 | 486 |
478 bool PutIsDel(bool value); | 487 bool PutIsDel(bool value); |
479 | 488 |
480 private: // Don't allow creation on heap, except by sync API wrappers. | 489 private: // Don't allow creation on heap, except by sync API wrappers. |
481 friend class sync_api::WriteNode; | 490 friend class sync_api::WriteNode; |
482 void* operator new(size_t size) { return (::operator new)(size); } | 491 void* operator new(size_t size) { return (::operator new)(size); } |
483 | 492 |
484 bool PutImpl(StringField field, const std::string& value); | 493 bool PutImpl(StringField field, const std::string& value); |
(...skipping 587 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1072 void ZeroFields(EntryKernel* entry, int first_field); | 1081 void ZeroFields(EntryKernel* entry, int first_field); |
1073 | 1082 |
1074 } // namespace syncable | 1083 } // namespace syncable |
1075 | 1084 |
1076 std::ostream& operator <<(std::ostream&, const syncable::Blob&); | 1085 std::ostream& operator <<(std::ostream&, const syncable::Blob&); |
1077 | 1086 |
1078 browser_sync::FastDump& operator << | 1087 browser_sync::FastDump& operator << |
1079 (browser_sync::FastDump&, const syncable::Blob&); | 1088 (browser_sync::FastDump&, const syncable::Blob&); |
1080 | 1089 |
1081 #endif // CHROME_BROWSER_SYNC_SYNCABLE_SYNCABLE_H_ | 1090 #endif // CHROME_BROWSER_SYNC_SYNCABLE_SYNCABLE_H_ |
OLD | NEW |