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 THIRD_PARTY_MOJO_SRC_MOJO_EDK_SYSTEM_CHANNEL_ENDPOINT_ID_H_ | |
6 #define THIRD_PARTY_MOJO_SRC_MOJO_EDK_SYSTEM_CHANNEL_ENDPOINT_ID_H_ | |
7 | |
8 #include <stddef.h> | |
9 #include <stdint.h> | |
10 | |
11 #include <ostream> | |
12 | |
13 #include "base/containers/hash_tables.h" | |
14 #include "base/gtest_prod_util.h" | |
15 #include "mojo/public/cpp/system/macros.h" | |
16 #include "third_party/mojo/src/mojo/edk/system/system_impl_export.h" | |
17 | |
18 namespace mojo { | |
19 namespace system { | |
20 | |
21 // ChannelEndpointId ----------------------------------------------------------- | |
22 | |
23 class LocalChannelEndpointIdGenerator; | |
24 FORWARD_DECLARE_TEST(LocalChannelEndpointIdGeneratorTest, WrapAround); | |
25 FORWARD_DECLARE_TEST(RemoteChannelEndpointIdGeneratorTest, WrapAround); | |
26 | |
27 // Represents an ID for an endpoint (i.e., one side of a message pipe) on a | |
28 // |Channel|. This class must be POD. | |
29 // | |
30 // Note: The terminology "remote" for a |ChannelEndpointId| means a destination | |
31 // ID that was actually allocated by the sender, or similarly a source ID that | |
32 // was allocated by the receiver. | |
33 // | |
34 // From the standpoint of the |Channel| with such a remote ID in its endpoint | |
35 // table, such an ID is a "remotely-allocated local ID". From the standpoint of | |
36 // the |Channel| allocating such a remote ID (for its peer |Channel|), it's a | |
37 // "locally-allocated remote ID". | |
38 class MOJO_SYSTEM_IMPL_EXPORT ChannelEndpointId { | |
39 public: | |
40 ChannelEndpointId() : value_(0) {} | |
41 ChannelEndpointId(const ChannelEndpointId& other) : value_(other.value_) {} | |
42 | |
43 // Returns the local ID to use for the first message pipe endpoint on a | |
44 // channel. | |
45 static ChannelEndpointId GetBootstrap() { return ChannelEndpointId(1); } | |
46 | |
47 bool operator==(const ChannelEndpointId& other) const { | |
48 return value_ == other.value_; | |
49 } | |
50 bool operator!=(const ChannelEndpointId& other) const { | |
51 return !operator==(other); | |
52 } | |
53 // So that we can be used in |std::map|, etc. | |
54 bool operator<(const ChannelEndpointId& other) const { | |
55 return value_ < other.value_; | |
56 } | |
57 | |
58 bool is_valid() const { return !!value_; } | |
59 bool is_remote() const { return !!(value_ & kRemoteFlag); } | |
60 const uint32_t& value() const { return value_; } | |
61 | |
62 // Flag set in |value()| if this is a remote ID. | |
63 static const uint32_t kRemoteFlag = 0x80000000u; | |
64 | |
65 private: | |
66 friend class LocalChannelEndpointIdGenerator; | |
67 FRIEND_TEST_ALL_PREFIXES(LocalChannelEndpointIdGeneratorTest, WrapAround); | |
68 friend class RemoteChannelEndpointIdGenerator; | |
69 FRIEND_TEST_ALL_PREFIXES(RemoteChannelEndpointIdGeneratorTest, WrapAround); | |
70 | |
71 explicit ChannelEndpointId(uint32_t value) : value_(value) {} | |
72 | |
73 uint32_t value_; | |
74 | |
75 // Copying and assignment allowed. | |
76 }; | |
77 // This wrapper should add no overhead. | |
78 // TODO(vtl): Rewrite |sizeof(uint32_t)| as |sizeof(ChannelEndpointId::value)| | |
79 // once we have sufficient C++11 support. | |
80 static_assert(sizeof(ChannelEndpointId) == sizeof(uint32_t), | |
81 "ChannelEndpointId has incorrect size"); | |
82 | |
83 // So logging macros and |DCHECK_EQ()|, etc. work. | |
84 MOJO_SYSTEM_IMPL_EXPORT inline std::ostream& operator<<( | |
85 std::ostream& out, | |
86 const ChannelEndpointId& channel_endpoint_id) { | |
87 return out << channel_endpoint_id.value(); | |
88 } | |
89 | |
90 // LocalChannelEndpointIdGenerator --------------------------------------------- | |
91 | |
92 // A generator for "new" local |ChannelEndpointId|s. It does not track | |
93 // used/existing IDs; that must be done separately. (This class is not | |
94 // thread-safe.) | |
95 class MOJO_SYSTEM_IMPL_EXPORT LocalChannelEndpointIdGenerator { | |
96 public: | |
97 LocalChannelEndpointIdGenerator() | |
98 : next_(ChannelEndpointId::GetBootstrap()) {} | |
99 | |
100 ChannelEndpointId GetNext(); | |
101 | |
102 private: | |
103 FRIEND_TEST_ALL_PREFIXES(LocalChannelEndpointIdGeneratorTest, WrapAround); | |
104 | |
105 ChannelEndpointId next_; | |
106 | |
107 MOJO_DISALLOW_COPY_AND_ASSIGN(LocalChannelEndpointIdGenerator); | |
108 }; | |
109 | |
110 // RemoteChannelEndpointIdGenerator -------------------------------------------- | |
111 | |
112 // A generator for "new" remote |ChannelEndpointId|s, for |Channel|s to | |
113 // locally allocate remote IDs. (See the comment above |ChannelEndpointId| for | |
114 // an explanatory note.) It does not track used/existing IDs; that must be done | |
115 // separately. (This class is not thread-safe.) | |
116 class MOJO_SYSTEM_IMPL_EXPORT RemoteChannelEndpointIdGenerator { | |
117 public: | |
118 RemoteChannelEndpointIdGenerator() : next_(ChannelEndpointId::kRemoteFlag) {} | |
119 | |
120 ChannelEndpointId GetNext(); | |
121 | |
122 private: | |
123 FRIEND_TEST_ALL_PREFIXES(RemoteChannelEndpointIdGeneratorTest, WrapAround); | |
124 | |
125 ChannelEndpointId next_; | |
126 | |
127 MOJO_DISALLOW_COPY_AND_ASSIGN(RemoteChannelEndpointIdGenerator); | |
128 }; | |
129 | |
130 } // namespace system | |
131 } // namespace mojo | |
132 | |
133 // Define "hash" functions for |ChannelEndpointId|s, so they can be used in hash | |
134 // tables. | |
135 // TODO(vtl): Once we can use |std::unordered_{map,set}|, update this (and | |
136 // remove the base/containers/hash_tables.h include). | |
137 namespace BASE_HASH_NAMESPACE { | |
138 | |
139 template <> | |
140 struct hash<mojo::system::ChannelEndpointId> { | |
141 size_t operator()(mojo::system::ChannelEndpointId channel_endpoint_id) const { | |
142 return static_cast<size_t>(channel_endpoint_id.value()); | |
143 } | |
144 }; | |
145 | |
146 } // namespace BASE_HASH_NAMESPACE | |
147 | |
148 #endif // THIRD_PARTY_MOJO_SRC_MOJO_EDK_SYSTEM_CHANNEL_ENDPOINT_ID_H_ | |
OLD | NEW |