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 MOJO_CORE_PORTS_NAME_H_
6 #define MOJO_CORE_PORTS_NAME_H_
7 
8 #include <stdint.h>
9 
10 #include <ostream>
11 #include <tuple>
12 
13 #include "base/component_export.h"
14 #include "base/hash.h"
15 
16 namespace mojo {
17 namespace core {
18 namespace ports {
19 
COMPONENT_EXPORT(MOJO_CORE_PORTS)20 struct COMPONENT_EXPORT(MOJO_CORE_PORTS) Name {
21   Name(uint64_t v1, uint64_t v2) : v1(v1), v2(v2) {}
22   uint64_t v1, v2;
23 };
24 
25 inline bool operator==(const Name& a, const Name& b) {
26   return a.v1 == b.v1 && a.v2 == b.v2;
27 }
28 
29 inline bool operator!=(const Name& a, const Name& b) {
30   return !(a == b);
31 }
32 
33 inline bool operator<(const Name& a, const Name& b) {
34   return std::tie(a.v1, a.v2) < std::tie(b.v1, b.v2);
35 }
36 
37 COMPONENT_EXPORT(MOJO_CORE_PORTS)
38 std::ostream& operator<<(std::ostream& stream, const Name& name);
39 
COMPONENT_EXPORT(MOJO_CORE_PORTS)40 struct COMPONENT_EXPORT(MOJO_CORE_PORTS) PortName : Name {
41   PortName() : Name(0, 0) {}
42   PortName(uint64_t v1, uint64_t v2) : Name(v1, v2) {}
43 };
44 
45 extern COMPONENT_EXPORT(MOJO_CORE_PORTS) const PortName kInvalidPortName;
46 
COMPONENT_EXPORT(MOJO_CORE_PORTS)47 struct COMPONENT_EXPORT(MOJO_CORE_PORTS) NodeName : Name {
48   NodeName() : Name(0, 0) {}
49   NodeName(uint64_t v1, uint64_t v2) : Name(v1, v2) {}
50 };
51 
52 extern COMPONENT_EXPORT(MOJO_CORE_PORTS) const NodeName kInvalidNodeName;
53 
54 }  // namespace ports
55 }  // namespace core
56 }  // namespace mojo
57 
58 namespace std {
59 
60 template <>
COMPONENT_EXPORT(MOJO_CORE_PORTS)61 struct COMPONENT_EXPORT(MOJO_CORE_PORTS) hash<mojo::core::ports::PortName> {
62   std::size_t operator()(const mojo::core::ports::PortName& name) const {
63     return base::HashInts64(name.v1, name.v2);
64   }
65 };
66 
67 template <>
COMPONENT_EXPORT(MOJO_CORE_PORTS)68 struct COMPONENT_EXPORT(MOJO_CORE_PORTS) hash<mojo::core::ports::NodeName> {
69   std::size_t operator()(const mojo::core::ports::NodeName& name) const {
70     return base::HashInts64(name.v1, name.v2);
71   }
72 };
73 
74 }  // namespace std
75 
76 #endif  // MOJO_CORE_PORTS_NAME_H_
77