summaryrefslogtreecommitdiffstats
path: root/ipc/chromium/src/mojo/core/ports/name.h
blob: 0e668ebc40196ed3ce574ed0f65b798833d7ca72 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef MOJO_CORE_PORTS_NAME_H_
#define MOJO_CORE_PORTS_NAME_H_

#include <stdint.h>

#include <ostream>
#include <tuple>

#include "base/logging.h"
#include "mozilla/HashFunctions.h"
#include "nsHashKeys.h"

namespace mojo {
namespace core {
namespace ports {

struct Name {
  constexpr Name(uint64_t v1, uint64_t v2) : v1(v1), v2(v2) {}
  uint64_t v1, v2;
};

inline bool operator==(const Name& a, const Name& b) {
  return a.v1 == b.v1 && a.v2 == b.v2;
}

inline bool operator!=(const Name& a, const Name& b) { return !(a == b); }

inline bool operator<(const Name& a, const Name& b) {
  return std::tie(a.v1, a.v2) < std::tie(b.v1, b.v2);
}

std::ostream& operator<<(std::ostream& stream, const Name& name);
mozilla::Logger& operator<<(mozilla::Logger& log, const Name& name);

struct PortName : Name {
  constexpr PortName() : Name(0, 0) {}
  constexpr PortName(uint64_t v1, uint64_t v2) : Name(v1, v2) {}
};

constexpr PortName kInvalidPortName{0, 0};

struct NodeName : Name {
  constexpr NodeName() : Name(0, 0) {}
  constexpr NodeName(uint64_t v1, uint64_t v2) : Name(v1, v2) {}
};

constexpr NodeName kInvalidNodeName{0, 0};

}  // namespace ports
}  // namespace core
}  // namespace mojo

namespace mozilla {

template <>
inline PLDHashNumber Hash<mojo::core::ports::PortName>(
    const mojo::core::ports::PortName& aValue) {
  return mozilla::HashGeneric(aValue.v1, aValue.v2);
}

template <>
inline PLDHashNumber Hash<mojo::core::ports::NodeName>(
    const mojo::core::ports::NodeName& aValue) {
  return mozilla::HashGeneric(aValue.v1, aValue.v2);
}

using PortNameHashKey = nsGenericHashKey<mojo::core::ports::PortName>;
using NodeNameHashKey = nsGenericHashKey<mojo::core::ports::NodeName>;

}  // namespace mozilla

namespace std {

template <>
struct hash<mojo::core::ports::PortName> {
  std::size_t operator()(const mojo::core::ports::PortName& name) const {
    // FIXME: PLDHashNumber is only 32-bits
    return mozilla::Hash(name);
  }
};

template <>
struct hash<mojo::core::ports::NodeName> {
  std::size_t operator()(const mojo::core::ports::NodeName& name) const {
    // FIXME: PLDHashNumber is only 32-bits
    return mozilla::Hash(name);
  }
};

}  // namespace std

class PickleIterator;

namespace IPC {

template <typename T>
struct ParamTraits;
class Message;
class MessageReader;
class MessageWriter;

template <>
struct ParamTraits<mojo::core::ports::PortName> {
  using paramType = mojo::core::ports::PortName;
  static void Write(MessageWriter* aWriter, const paramType& aParam);
  static bool Read(MessageReader* aReader, paramType* aResult);
};

template <>
struct ParamTraits<mojo::core::ports::NodeName> {
  using paramType = mojo::core::ports::NodeName;
  static void Write(MessageWriter* aWriter, const paramType& aParam);
  static bool Read(MessageReader* aReader, paramType* aResult);
};

}  // namespace IPC

#endif  // MOJO_CORE_PORTS_NAME_H_