OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 std::map and/or base::hash_map; |
| 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 method. |
| 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_null() const { return value_ == kInvalidValue; } |
| 53 |
| 54 static IdType FromUnsafeValue(WrappedType value) { return IdType(value); } |
| 55 WrappedType GetUnsafeValue() const { return value_; } |
| 56 |
| 57 IdType(const IdType& other) = default; |
| 58 IdType& operator=(const IdType& other) = default; |
| 59 |
| 60 bool operator==(const IdType& other) const { return value_ == other.value_; } |
| 61 bool operator!=(const IdType& other) const { return value_ != other.value_; } |
| 62 bool operator<(const IdType& other) const { return value_ < other.value_; } |
| 63 |
| 64 protected: |
| 65 explicit IdType(WrappedType val) : value_(val) {} |
| 66 |
| 67 private: |
| 68 // In theory WrappedType could be any type that supports ==, <, <<, std::hash, |
| 69 // etc., but to make things simpler (both for users and for maintainers) we |
| 70 // explicitly restrict the design space to integers. This means the users |
| 71 // can safely assume that IdType is relatively small and cheap to copy |
| 72 // and the maintainers don't have to worry about WrappedType being a complex |
| 73 // type (i.e. std::string or std::pair or a move-only type). |
| 74 using IntegralWrappedType = |
| 75 typename std::enable_if<std::is_integral<WrappedType>::value, |
| 76 WrappedType>::type; |
| 77 IntegralWrappedType value_; |
| 78 }; |
| 79 |
| 80 // Type aliases for convenience: |
| 81 template <typename TypeMarker> |
| 82 using IdType32 = IdType<TypeMarker, int32_t, 0>; |
| 83 template <typename TypeMarker> |
| 84 using IdTypeU32 = IdType<TypeMarker, uint32_t, 0>; |
| 85 template <typename TypeMarker> |
| 86 using IdType64 = IdType<TypeMarker, int64_t, 0>; |
| 87 template <typename TypeMarker> |
| 88 using IdTypeU64 = IdType<TypeMarker, uint64_t, 0>; |
| 89 |
| 90 template <typename TypeMarker, typename WrappedType, WrappedType kInvalidValue> |
| 91 std::ostream& operator<<( |
| 92 std::ostream& stream, |
| 93 const IdType<TypeMarker, WrappedType, kInvalidValue>& id) { |
| 94 return stream << id.GetUnsafeValue(); |
| 95 } |
| 96 |
| 97 } // namespace content |
| 98 |
| 99 namespace BASE_HASH_NAMESPACE { |
| 100 |
| 101 template <typename TypeMarker, typename WrappedType, WrappedType kInvalidValue> |
| 102 struct hash<content::IdType<TypeMarker, WrappedType, kInvalidValue>> { |
| 103 using argument_type = content::IdType<TypeMarker, WrappedType, kInvalidValue>; |
| 104 using result_type = std::size_t; |
| 105 result_type operator()(const argument_type& id) const { |
| 106 return BASE_HASH_NAMESPACE::hash<WrappedType>()(id.GetUnsafeValue()); |
| 107 } |
| 108 }; |
| 109 |
| 110 } // namespace BASE_HASH_NAMESPACE |
| 111 |
| 112 #endif // CONTENT_COMMON_ID_TYPE_H_ |
OLD | NEW |