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.
5 #ifndef MOJO_EDK_SYSTEM_CHANNEL_ENDPOINT_ID_H_
6 #define MOJO_EDK_SYSTEM_CHANNEL_ENDPOINT_ID_H_
13 #include "base/containers/hash_tables.h"
14 #include "base/gtest_prod_util.h"
15 #include "base/macros.h"
16 #include "mojo/edk/system/system_impl_export.h"
21 // ChannelEndpointId -----------------------------------------------------------
23 class LocalChannelEndpointIdGenerator;
24 FORWARD_DECLARE_TEST(LocalChannelEndpointIdGeneratorTest, WrapAround);
25 FORWARD_DECLARE_TEST(RemoteChannelEndpointIdGeneratorTest, WrapAround);
27 // Represents an ID for an endpoint (i.e., one side of a message pipe) on a
28 // |Channel|. This class must be POD.
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.
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 {
40 ChannelEndpointId() : value_(0) {}
41 ChannelEndpointId(const ChannelEndpointId& other) : value_(other.value_) {}
43 // Returns the local ID to use for the first message pipe endpoint on a
45 static ChannelEndpointId GetBootstrap() { return ChannelEndpointId(1); }
47 bool operator==(const ChannelEndpointId& other) const {
48 return value_ == other.value_;
50 bool operator!=(const ChannelEndpointId& other) const {
51 return !operator==(other);
53 // So that we can be used in |std::map|, etc.
54 bool operator<(const ChannelEndpointId& other) const {
55 return value_ < other.value_;
58 bool is_valid() const { return !!value_; }
59 bool is_remote() const { return !!(value_ & kRemoteFlag); }
60 const uint32_t& value() const { return value_; }
62 // Flag set in |value()| if this is a remote ID.
63 static const uint32_t kRemoteFlag = 0x80000000u;
66 friend class LocalChannelEndpointIdGenerator;
67 FRIEND_TEST_ALL_PREFIXES(LocalChannelEndpointIdGeneratorTest, WrapAround);
68 friend class RemoteChannelEndpointIdGenerator;
69 FRIEND_TEST_ALL_PREFIXES(RemoteChannelEndpointIdGeneratorTest, WrapAround);
71 explicit ChannelEndpointId(uint32_t value) : value_(value) {}
75 // Copying and assignment allowed.
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");
83 // So logging macros and |DCHECK_EQ()|, etc. work.
84 inline std::ostream& operator<<(std::ostream& out,
85 const ChannelEndpointId& channel_endpoint_id) {
86 return out << channel_endpoint_id.value();
89 // LocalChannelEndpointIdGenerator ---------------------------------------------
91 // A generator for "new" local |ChannelEndpointId|s. It does not track
92 // used/existing IDs; that must be done separately. (This class is not
94 class MOJO_SYSTEM_IMPL_EXPORT LocalChannelEndpointIdGenerator {
96 LocalChannelEndpointIdGenerator()
97 : next_(ChannelEndpointId::GetBootstrap()) {}
99 ChannelEndpointId GetNext();
102 FRIEND_TEST_ALL_PREFIXES(LocalChannelEndpointIdGeneratorTest, WrapAround);
104 ChannelEndpointId next_;
106 DISALLOW_COPY_AND_ASSIGN(LocalChannelEndpointIdGenerator);
109 // RemoteChannelEndpointIdGenerator --------------------------------------------
111 // A generator for "new" remote |ChannelEndpointId|s, for |Channel|s to
112 // locally allocate remote IDs. (See the comment above |ChannelEndpointId| for
113 // an explanatory note.) It does not track used/existing IDs; that must be done
114 // separately. (This class is not thread-safe.)
115 class MOJO_SYSTEM_IMPL_EXPORT RemoteChannelEndpointIdGenerator {
117 RemoteChannelEndpointIdGenerator() : next_(ChannelEndpointId::kRemoteFlag) {}
119 ChannelEndpointId GetNext();
122 FRIEND_TEST_ALL_PREFIXES(RemoteChannelEndpointIdGeneratorTest, WrapAround);
124 ChannelEndpointId next_;
126 DISALLOW_COPY_AND_ASSIGN(RemoteChannelEndpointIdGenerator);
129 } // namespace system
132 // Define "hash" functions for |ChannelEndpointId|s, so they can be used in hash
134 // TODO(vtl): Once we can use |std::unordered_{map,set}|, update this (and
135 // remove the base/containers/hash_tables.h include).
136 namespace BASE_HASH_NAMESPACE {
139 struct hash<mojo::system::ChannelEndpointId> {
140 size_t operator()(mojo::system::ChannelEndpointId channel_endpoint_id) const {
141 return static_cast<size_t>(channel_endpoint_id.value());
145 } // namespace BASE_HASH_NAMESPACE
147 #endif // MOJO_EDK_SYSTEM_CHANNEL_ENDPOINT_ID_H_