Index: content/browser/download/id_type.h |
diff --git a/content/browser/download/id_type.h b/content/browser/download/id_type.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..abe709c505d027e4f14ae0f45c03cd320b8f57ce |
--- /dev/null |
+++ b/content/browser/download/id_type.h |
@@ -0,0 +1,137 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#ifndef CONTENT_BROWSER_DOWNLOAD_ID_TYPE_H_ |
+#define CONTENT_BROWSER_DOWNLOAD_ID_TYPE_H_ |
ncarter (slow)
2016/01/07 00:48:50
Let's put this in content/common for now.
Łukasz Anforowicz
2016/01/07 18:02:13
Done.
|
+ |
+#include <ostream> |
+ |
+#include "base/containers/hash_tables.h" |
+ |
+// Problem: |
+// |
+// void f(int foo_id, int bar_id); |
+// ... |
+// // Arguments passed in a wrong order: |
+// f(bar_id, foo_id); // Compiles - sigh... :-( |
+// |
+// Solution: |
+// |
+// using FooId = content::IdType<Foo, uint64_t, 0>; |
+// using BarId = content::IdType<Bar, uint64_t, 0>; |
ncarter (slow)
2016/01/07 00:48:50
Id prefer if we had a design that didn't require t
Łukasz Anforowicz
2016/01/07 18:02:12
I agree that this is desirable, although:
1) I wo
ncarter (slow)
2016/01/07 20:14:32
I like this idea, a lot.
It looks like, in existi
Łukasz Anforowicz
2016/01/07 21:43:01
Done. (changed IdType32/64 to wrap a signed integ
|
+// void f(FooId foo_id, BarId bar_id); |
+// ... |
+// // Arguments passed in a wrong order: |
+// f(bar_id, foo_id); // Doesn't compile anymore - yay! :-) |
+// |
+// Typically FooId would be declared like this: |
+// |
+// foo_id.h: |
+// |
+// #include "content/browser/download/id_type.h" |
+// |
+// namespace foo_namespace { |
+// |
+// class Foo; |
+// using FooId = content::IdType<Foo, uint64_t, 0>; |
+// |
+// } // namespace foo_namespace |
+// |
+// For most practical purposes, FooId declared like above behaves just like |
+// an uint64_t, except that: |
+// |
+// 1. FooId only supports a subset of uint64_t operations: |
+// - ==, !=, <, hashing |
+// - stream output |
+// - copy construction and assignment |
+// |
+// 2. FooId does not implicitly coerce to/from other uint64_t values and |
+// to/from content::IdType<SomeOtherType, ...>. Explicit coercion to/from |
+// uint64_t is possible through the following FooId methods: |
+// - static FooId FromUnsafeValue(uint64_t) |
+// - uint64_t GetUnsafeValue() |
+// |
+// 3. Default-constructed FooId contains a "null" / "invalid" value specified |
+// by the 3rd argument of the IdType<...> template. Testing against this |
+// value can be done by the following FooId methods: |
+// - bool is_valid() |
+// - bool is_null() |
+// |
+// 4. The presence of a custom default constructor means that FooId is not a |
+// "trivial" class and therefore is not a POD type (unlike an uint64_t). |
+// At the same time FooId has almost all of the properties of a POD type: |
+// - is "trivially copyable" (i.e. is memcpy-able), |
+// - has "standard layout" (i.e. interops with things expecting C layout). |
+// See http://stackoverflow.com/a/7189821 for more info about these |
+// concepts. |
+ |
+namespace content { |
+ |
+template <typename TypeMarker, typename WrappedType, WrappedType kInvalidValue> |
ncarter (slow)
2016/01/07 00:48:50
I don't really know C++ templates well, so I wonde
Łukasz Anforowicz
2016/01/07 18:02:12
I've tried this ans got an error:
../../content/co
ncarter (slow)
2016/01/07 20:14:32
I really like the solution of expose a ready-to-us
Łukasz Anforowicz
2016/01/07 21:43:01
Acknowledged.
|
+struct IdType { |
ncarter (slow)
2016/01/07 00:48:50
why struct? IdType has no public data members.
Łukasz Anforowicz
2016/01/07 18:02:13
I changed to a class.
RE: why
No particular reas
|
+ public: |
+ IdType() : value_(kInvalidValue) {} |
+ bool is_valid() const { return value_ != kInvalidValue; } |
+ bool is_null() const { return value_ == kInvalidValue; } |
+ |
+ static IdType FromUnsafeValue(WrappedType value) { return IdType(value); } |
+ WrappedType GetUnsafeValue() const { return value_; } |
+ |
+ IdType(const IdType& other) = default; |
+ IdType& operator=(const IdType& other) = default; |
+ |
+ bool operator==(const IdType& other) const { return value_ == other.value_; } |
+ bool operator!=(const IdType& other) const { return value_ != other.value_; } |
+ bool operator<(const IdType& other) const { return value_ < other.value_; } |
+ |
+ protected: |
+ explicit IdType(WrappedType val) : value_(val) {} |
+ |
+ private: |
+ WrappedType value_; |
+}; |
+ |
+template <typename TypeMarker, typename WrappedType, WrappedType kInvalidValue> |
+std::ostream& operator<<( |
+ std::ostream& stream, |
+ const IdType<TypeMarker, WrappedType, kInvalidValue>& id) { |
+ return stream << id.GetUnsafeValue(); |
+} |
+ |
+} // namespace content |
+ |
+namespace BASE_HASH_NAMESPACE { |
+ |
+template <typename TypeMarker, typename WrappedType, WrappedType kInvalidValue> |
+struct hash<content::IdType<TypeMarker, WrappedType, kInvalidValue>> { |
+ using argument_type = content::IdType<TypeMarker, WrappedType, kInvalidValue>; |
+ using result_type = std::size_t; |
+ result_type operator()(const argument_type& id) const { |
+ return BASE_HASH_NAMESPACE::hash<WrappedType>()(id.GetUnsafeValue()); |
+ } |
+}; |
+ |
+} // namespace BASE_HASH_NAMESPACE |
+ |
+// If defined(COMPILER_MSVC) then BASE_HASH_NAMESPACE == std. |
+// In this case we need to avoid defininig std::hash<...> second time |
+// (it has already been defined above). |
+#if !defined(COMPILER_MSVC) |
+ |
+namespace std { |
+ |
+template <typename TypeMarker, typename WrappedType, WrappedType kInvalidValue> |
+struct hash<content::IdType<TypeMarker, WrappedType, kInvalidValue>> { |
+ using argument_type = content::IdType<TypeMarker, WrappedType, kInvalidValue>; |
+ using result_type = std::size_t; |
+ result_type operator()(const argument_type& id) const { |
+ return std::hash<WrappedType>()(id.GetUnsafeValue()); |
+ } |
+}; |
+ |
+} // namespace std; |
+ |
+#endif // !defined(COMPILER_MSVC) |
+ |
+#endif // CONTENT_BROWSER_DOWNLOAD_ID_TYPE_H_ |