OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 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 #ifndef CONTENT_COMMON_ID_TYPE_H_ | |
6 #define CONTENT_COMMON_ID_TYPE_H_ | |
7 | |
8 #include <stdint.h> | |
9 #include <ostream> | |
10 #include <type_traits> | |
11 | |
12 #include "base/containers/hash_tables.h" | |
13 | |
14 // IdType32<>, IdType64<>, etc. wrap an integer id in a custom, type-safe type. | |
15 // | |
16 // IdType32<Foo> is an alternative to int, for a class Foo with methods like: | |
17 // | |
18 // int GetId() { return id_; }; | |
19 // static Foo* FromId(int id) { return g_all_foos_by_id[id]; } | |
20 // | |
21 // Such methods are a standard means of safely referring to objects across | |
22 // thread and process boundaries. But if a nearby class Bar also represents | |
23 // its IDs as a bare int, horrific mixups are possible -- one example, of many, | |
24 // is http://crrev.com/365437. IdType<> offers compile-time protection against | |
25 // such mishaps, since IdType32<Foo> is incompatible with IdType32<Bar>, even | |
26 // though both just compile down to an int32_t. | |
27 // | |
28 // Templates in this file: | |
29 // IdType32<T> / IdTypeU32<T>: Signed / unsigned 32-bit IDs | |
30 // IdType64<T> / IdTypeU64<T>: Signed / unsigned 64-bit IDs | |
31 // IdType<>: For when you need a different underlying type or | |
32 // a default/invalid value other than zero. | |
33 // | |
34 // IdType32<Foo> behaves just like an int32_t in the following aspects: | |
35 // - it can be used as a key in STL containers; | |
36 // - it can be used as an argument to DCHECK_EQ or streamed to LOG(ERROR); | |
37 // - it has the same memory footprint and runtime overhead as int32_t; | |
38 // - it can be copied by memcpy. | |
39 // | |
40 // IdType32<Foo> has the following differences from a bare int32_t: | |
41 // - it forces coercions to go through GetUnsafeValue and FromUnsafeValue; | |
42 // - it restricts the set of available operations (i.e. no multiplication); | |
43 // - it ensures initialization to zero and allows checking against | |
44 // default-initialized values via is_null and/or is_valid methods. | |
45 | |
46 namespace content { | |
47 | |
48 template <typename TypeMarker, typename WrappedType, WrappedType kInvalidValue> | |
49 class IdType { | |
50 public: | |
51 IdType() : value_(kInvalidValue) {} | |
52 bool is_valid() const { return value_ != kInvalidValue; } | |
53 bool is_null() const { return value_ == kInvalidValue; } | |
dcheng
2016/01/07 22:07:28
Bikeshedding... seems a bit odd that the first is
ncarter (slow)
2016/01/07 22:49:42
Continuing the bikeshed: If we go to only one gett
dcheng
2016/01/07 22:50:37
Either way is fine. Just that having two different
Łukasz Anforowicz
2016/01/07 23:12:43
Done. Thanks for chiming in. I don't actually re
Łukasz Anforowicz
2016/01/07 23:12:43
I'd prefer "is_null", because this is the name of
| |
54 | |
55 static IdType FromUnsafeValue(WrappedType value) { return IdType(value); } | |
56 WrappedType GetUnsafeValue() const { return value_; } | |
57 | |
58 IdType(const IdType& other) = default; | |
59 IdType& operator=(const IdType& other) = default; | |
60 | |
61 bool operator==(const IdType& other) const { return value_ == other.value_; } | |
62 bool operator!=(const IdType& other) const { return value_ != other.value_; } | |
63 bool operator<(const IdType& other) const { return value_ < other.value_; } | |
64 | |
65 protected: | |
66 explicit IdType(WrappedType val) : value_(val) {} | |
67 | |
68 private: | |
69 // In theory WrappedType could be any type that supports ==, <, <<, std::hash, | |
70 // etc., but to make things simpler (both for users and for maintainers) we | |
71 // explicitly restrict the design space to integers. This means the users | |
72 // can safely assume that IdType is relatively small and cheap to copy | |
73 // and the maintainers don't have to worry about WrappedType being a complex | |
74 // type (i.e. std::string or std::pair or a move-only type). | |
75 using IntegralWrappedType = | |
76 typename std::enable_if<std::is_integral<WrappedType>::value, | |
77 WrappedType>::type; | |
78 IntegralWrappedType value_; | |
79 }; | |
80 | |
81 // Type aliases for convenience: | |
82 template <typename TypeMarker> | |
83 using IdType32 = IdType<TypeMarker, int32_t, 0>; | |
84 template <typename TypeMarker> | |
85 using IdTypeU32 = IdType<TypeMarker, uint32_t, 0>; | |
86 template <typename TypeMarker> | |
87 using IdType64 = IdType<TypeMarker, int64_t, 0>; | |
88 template <typename TypeMarker> | |
89 using IdTypeU64 = IdType<TypeMarker, uint64_t, 0>; | |
90 | |
91 template <typename TypeMarker, typename WrappedType, WrappedType kInvalidValue> | |
92 std::ostream& operator<<( | |
93 std::ostream& stream, | |
94 const IdType<TypeMarker, WrappedType, kInvalidValue>& id) { | |
95 return stream << id.GetUnsafeValue(); | |
96 } | |
97 | |
98 } // namespace content | |
99 | |
100 namespace BASE_HASH_NAMESPACE { | |
101 | |
102 template <typename TypeMarker, typename WrappedType, WrappedType kInvalidValue> | |
103 struct hash<content::IdType<TypeMarker, WrappedType, kInvalidValue>> { | |
104 using argument_type = content::IdType<TypeMarker, WrappedType, kInvalidValue>; | |
105 using result_type = std::size_t; | |
106 result_type operator()(const argument_type& id) const { | |
107 return BASE_HASH_NAMESPACE::hash<WrappedType>()(id.GetUnsafeValue()); | |
108 } | |
109 }; | |
110 | |
111 } // namespace BASE_HASH_NAMESPACE | |
112 | |
113 // If defined(COMPILER_MSVC) then BASE_HASH_NAMESPACE == std. | |
114 // In this case we need to avoid defininig std::hash<...> second time | |
115 // (it has already been defined above). | |
116 #if !defined(COMPILER_MSVC) | |
117 | |
118 namespace std { | |
119 | |
120 template <typename TypeMarker, typename WrappedType, WrappedType kInvalidValue> | |
121 struct hash<content::IdType<TypeMarker, WrappedType, kInvalidValue>> { | |
122 using argument_type = content::IdType<TypeMarker, WrappedType, kInvalidValue>; | |
123 using result_type = std::size_t; | |
124 result_type operator()(const argument_type& id) const { | |
125 return std::hash<WrappedType>()(id.GetUnsafeValue()); | |
126 } | |
127 }; | |
128 | |
129 } // namespace std; | |
130 | |
131 #endif // !defined(COMPILER_MSVC) | |
132 | |
133 #endif // CONTENT_COMMON_ID_TYPE_H_ | |
OLD | NEW |