OLD | NEW |
| (Empty) |
1 // Copyright 2014 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 MOJO_EDK_SYSTEM_UNIQUE_IDENTIFIER_H_ | |
6 #define MOJO_EDK_SYSTEM_UNIQUE_IDENTIFIER_H_ | |
7 | |
8 #include <stdint.h> | |
9 #include <string.h> | |
10 | |
11 #include <iosfwd> | |
12 | |
13 #include "base/compiler_specific.h" | |
14 #include "base/containers/hash_tables.h" | |
15 #include "mojo/edk/system/system_impl_export.h" | |
16 | |
17 namespace mojo { | |
18 namespace system { | |
19 | |
20 class UniqueIdentifier; | |
21 | |
22 } // namespace system | |
23 } // namespace mojo | |
24 | |
25 namespace BASE_HASH_NAMESPACE { | |
26 | |
27 // Declare this before |UniqueIdentifier|, so that it can be friended. | |
28 template <> | |
29 struct hash<mojo::system::UniqueIdentifier>; | |
30 | |
31 } // BASE_HASH_NAMESPACE | |
32 | |
33 namespace mojo { | |
34 namespace system { | |
35 | |
36 // Declare this before |UniqueIdentifier|, so that it can be friended. | |
37 MOJO_SYSTEM_IMPL_EXPORT std::ostream& operator<<( | |
38 std::ostream& out, | |
39 const UniqueIdentifier& unique_identifier); | |
40 | |
41 // |UniqueIdentifier| is a POD class whose value is used to uniquely identify | |
42 // things. | |
43 class MOJO_SYSTEM_IMPL_EXPORT UniqueIdentifier { | |
44 public: | |
45 // This generates a new identifier. Uniqueness is "guaranteed" (i.e., | |
46 // probabilistically) for identifiers. | |
47 static UniqueIdentifier Generate(); | |
48 | |
49 bool operator==(const UniqueIdentifier& other) const { | |
50 return memcmp(data_, other.data_, sizeof(data_)) == 0; | |
51 } | |
52 bool operator!=(const UniqueIdentifier& other) const { | |
53 return !operator==(other); | |
54 } | |
55 bool operator<(const UniqueIdentifier& other) const { | |
56 return memcmp(data_, other.data_, sizeof(data_)) < 0; | |
57 } | |
58 | |
59 private: | |
60 friend BASE_HASH_NAMESPACE::hash<mojo::system::UniqueIdentifier>; | |
61 friend std::ostream& operator<<(std::ostream&, const UniqueIdentifier&); | |
62 | |
63 explicit UniqueIdentifier() {} | |
64 | |
65 char data_[16]; | |
66 | |
67 // Copying and assignment allowed. | |
68 }; | |
69 static_assert(sizeof(UniqueIdentifier) == 16, | |
70 "UniqueIdentifier has wrong size."); | |
71 // We want to be able to take any buffer and cast it to a |UniqueIdentifier|. | |
72 static_assert(ALIGNOF(UniqueIdentifier) == 1, | |
73 "UniqueIdentifier requires nontrivial alignment."); | |
74 | |
75 } // namespace system | |
76 } // namespace mojo | |
77 | |
78 namespace BASE_HASH_NAMESPACE { | |
79 | |
80 template <> | |
81 struct hash<mojo::system::UniqueIdentifier> { | |
82 size_t operator()(mojo::system::UniqueIdentifier unique_identifier) const { | |
83 return base::HashInts64( | |
84 *reinterpret_cast<uint64_t*>(unique_identifier.data_), | |
85 *reinterpret_cast<uint64_t*>(unique_identifier.data_ + | |
86 sizeof(uint64_t))); | |
87 } | |
88 }; | |
89 | |
90 } // BASE_HASH_NAMESPACE | |
91 | |
92 #endif // MOJO_EDK_SYSTEM_UNIQUE_IDENTIFIER_H_ | |
OLD | NEW |