name.h (3060B)
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/logging.h" 14 #include "mozilla/HashFunctions.h" 15 #include "nsHashKeys.h" 16 17 namespace mojo { 18 namespace core { 19 namespace ports { 20 21 struct Name { 22 constexpr Name(uint64_t v1, uint64_t v2) : v1(v1), v2(v2) {} 23 uint64_t v1, v2; 24 }; 25 26 inline bool operator==(const Name& a, const Name& b) { 27 return a.v1 == b.v1 && a.v2 == b.v2; 28 } 29 30 inline bool operator!=(const Name& a, const Name& b) { return !(a == b); } 31 32 inline bool operator<(const Name& a, const Name& b) { 33 return std::tie(a.v1, a.v2) < std::tie(b.v1, b.v2); 34 } 35 36 std::ostream& operator<<(std::ostream& stream, const Name& name); 37 mozilla::Logger& operator<<(mozilla::Logger& log, const Name& name); 38 39 struct PortName : Name { 40 constexpr PortName() : Name(0, 0) {} 41 constexpr PortName(uint64_t v1, uint64_t v2) : Name(v1, v2) {} 42 }; 43 44 constexpr PortName kInvalidPortName{0, 0}; 45 46 struct NodeName : Name { 47 constexpr NodeName() : Name(0, 0) {} 48 constexpr NodeName(uint64_t v1, uint64_t v2) : Name(v1, v2) {} 49 }; 50 51 constexpr NodeName kInvalidNodeName{0, 0}; 52 53 } // namespace ports 54 } // namespace core 55 } // namespace mojo 56 57 namespace mozilla { 58 59 template <> 60 inline PLDHashNumber Hash<mojo::core::ports::PortName>( 61 const mojo::core::ports::PortName& aValue) { 62 return mozilla::HashGeneric(aValue.v1, aValue.v2); 63 } 64 65 template <> 66 inline PLDHashNumber Hash<mojo::core::ports::NodeName>( 67 const mojo::core::ports::NodeName& aValue) { 68 return mozilla::HashGeneric(aValue.v1, aValue.v2); 69 } 70 71 using PortNameHashKey = nsGenericHashKey<mojo::core::ports::PortName>; 72 using NodeNameHashKey = nsGenericHashKey<mojo::core::ports::NodeName>; 73 74 } // namespace mozilla 75 76 namespace std { 77 78 template <> 79 struct hash<mojo::core::ports::PortName> { 80 std::size_t operator()(const mojo::core::ports::PortName& name) const { 81 // FIXME: PLDHashNumber is only 32-bits 82 return mozilla::Hash(name); 83 } 84 }; 85 86 template <> 87 struct hash<mojo::core::ports::NodeName> { 88 std::size_t operator()(const mojo::core::ports::NodeName& name) const { 89 // FIXME: PLDHashNumber is only 32-bits 90 return mozilla::Hash(name); 91 } 92 }; 93 94 } // namespace std 95 96 class PickleIterator; 97 98 namespace IPC { 99 100 template <typename T> 101 struct ParamTraits; 102 class Message; 103 class MessageReader; 104 class MessageWriter; 105 106 template <> 107 struct ParamTraits<mojo::core::ports::PortName> { 108 using paramType = mojo::core::ports::PortName; 109 static void Write(MessageWriter* aWriter, const paramType& aParam); 110 static bool Read(MessageReader* aReader, paramType* aResult); 111 }; 112 113 template <> 114 struct ParamTraits<mojo::core::ports::NodeName> { 115 using paramType = mojo::core::ports::NodeName; 116 static void Write(MessageWriter* aWriter, const paramType& aParam); 117 static bool Read(MessageReader* aReader, paramType* aResult); 118 }; 119 120 } // namespace IPC 121 122 #endif // MOJO_CORE_PORTS_NAME_H_