OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 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 SYNC_INTERNAL_API_PUBLIC_BASE_NODE_ORDINAL_H_ | 5 #ifndef SYNC_INTERNAL_API_PUBLIC_BASE_NODE_ORDINAL_H_ |
6 #define SYNC_INTERNAL_API_PUBLIC_BASE_NODE_ORDINAL_H_ | 6 #define SYNC_INTERNAL_API_PUBLIC_BASE_NODE_ORDINAL_H_ |
7 | 7 |
8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
9 #include "sync/base/sync_export.h" | 9 #include "sync/base/sync_export.h" |
10 #include "sync/internal_api/public/base/ordinal.h" | 10 #include "sync/internal_api/public/base/ordinal.h" |
11 | 11 |
12 namespace syncer { | 12 namespace syncer { |
13 | 13 |
14 // A NodeOrdinal is an Ordinal whose internal value comes from the | 14 // A NodeOrdinal is an Ordinal whose internal value comes from the |
15 // ordinal_in_parent field of SyncEntity (see sync.proto). It uses | 15 // ordinal_in_parent field of SyncEntity (see sync.proto). It uses |
16 // the entire uint8 range for backwards compatibility with the old | 16 // the entire uint8 range for backwards compatibility with the old |
17 // int64-based positioning. | 17 // int64-based positioning. |
18 | 18 |
19 struct NodeOrdinalTraits { | 19 struct NodeOrdinalTraits { |
20 static const uint8 kZeroDigit = 0; | 20 static const uint8 kZeroDigit = 0; |
21 static const uint8 kMaxDigit = kuint8max; | 21 static const uint8 kMaxDigit = kuint8max; |
22 static const size_t kMinLength = 8; | 22 static const size_t kMinLength = 8; |
23 }; | 23 }; |
24 | 24 |
25 typedef Ordinal<NodeOrdinalTraits> NodeOrdinal; | 25 typedef Ordinal<NodeOrdinalTraits> NodeOrdinal; |
26 | 26 |
27 COMPILE_ASSERT(static_cast<char>(NodeOrdinal::kZeroDigit) == '\x00', | 27 static_assert(static_cast<char>(NodeOrdinal::kZeroDigit) == '\x00', |
28 NodeOrdinalHasCorrectZeroDigit); | 28 "NodeOrdinal has incorrect zero digit"); |
29 COMPILE_ASSERT(static_cast<char>(NodeOrdinal::kOneDigit) == '\x01', | 29 static_assert(static_cast<char>(NodeOrdinal::kOneDigit) == '\x01', |
30 NodeOrdinalHasCorrectOneDigit); | 30 "NodeOrdinal has incorrect one digit"); |
31 COMPILE_ASSERT(static_cast<char>(NodeOrdinal::kMidDigit) == '\x80', | 31 static_assert(static_cast<char>(NodeOrdinal::kMidDigit) == '\x80', |
32 NodeOrdinalHasCorrectMidDigit); | 32 "NodeOrdinal has incorrect mid digit"); |
33 COMPILE_ASSERT(static_cast<char>(NodeOrdinal::kMaxDigit) == '\xff', | 33 static_assert(static_cast<char>(NodeOrdinal::kMaxDigit) == '\xff', |
34 NodeOrdinalHasCorrectMaxDigit); | 34 "NodeOrdinal has incorrect max digit"); |
35 COMPILE_ASSERT(NodeOrdinal::kMidDigitValue == 128, | 35 static_assert(NodeOrdinal::kMidDigitValue == 128, |
36 NodeOrdinalHasCorrectMidDigitValue); | 36 "NodeOrdinal has incorrect mid digit value"); |
37 COMPILE_ASSERT(NodeOrdinal::kMaxDigitValue == 255, | 37 static_assert(NodeOrdinal::kMaxDigitValue == 255, |
38 NodeOrdinalHasCorrectMaxDigitValue); | 38 "NodeOrdinal has incorrect max digit value"); |
39 COMPILE_ASSERT(NodeOrdinal::kRadix == 256, | 39 static_assert(NodeOrdinal::kRadix == 256, |
40 NodeOrdinalHasCorrectRadix); | 40 "NodeOrdinal has incorrect radix"); |
41 | 41 |
42 // Converts an int64 position (usually from the position_in_parent | 42 // Converts an int64 position (usually from the position_in_parent |
43 // field of SyncEntity) to a NodeOrdinal. This transformation | 43 // field of SyncEntity) to a NodeOrdinal. This transformation |
44 // preserves the ordering relation: a < b under integer ordering if | 44 // preserves the ordering relation: a < b under integer ordering if |
45 // and only if Int64ToNodeOrdinal(a) < Int64ToNodeOrdinal(b). | 45 // and only if Int64ToNodeOrdinal(a) < Int64ToNodeOrdinal(b). |
46 SYNC_EXPORT_PRIVATE NodeOrdinal Int64ToNodeOrdinal(int64 x); | 46 SYNC_EXPORT_PRIVATE NodeOrdinal Int64ToNodeOrdinal(int64 x); |
47 | 47 |
48 // The inverse of Int64ToNodeOrdinal. This conversion is, in general, | 48 // The inverse of Int64ToNodeOrdinal. This conversion is, in general, |
49 // lossy: NodeOrdinals can have arbitrary fidelity, while numeric | 49 // lossy: NodeOrdinals can have arbitrary fidelity, while numeric |
50 // positions contain only 64 bits of information (in fact, this is the | 50 // positions contain only 64 bits of information (in fact, this is the |
51 // reason we've moved away from them). | 51 // reason we've moved away from them). |
52 SYNC_EXPORT_PRIVATE int64 NodeOrdinalToInt64(const NodeOrdinal& ordinal); | 52 SYNC_EXPORT_PRIVATE int64 NodeOrdinalToInt64(const NodeOrdinal& ordinal); |
53 | 53 |
54 } // namespace syncer | 54 } // namespace syncer |
55 | 55 |
56 #endif // SYNC_INTERNAL_API_PUBLIC_BASE_NODE_ORDINAL_H_ | 56 #endif // SYNC_INTERNAL_API_PUBLIC_BASE_NODE_ORDINAL_H_ |
OLD | NEW |