C/C++ API Reference
Loading...
Searching...
No Matches
method_id.h
1// Copyright 2022 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14#pragma once
15
16#include <cstdint>
17#include <functional>
18
19// NOTE: These wrappers exist in order to provide future compatibility for
20// different internal representations of method identifiers.
21
22namespace pw::rpc {
23
24class MethodId;
25
26namespace internal {
27constexpr MethodId WrapMethodId(uint32_t id);
28constexpr uint32_t UnwrapMethodId(MethodId id);
29} // namespace internal
30
31// An identifier for a method.
32class MethodId {
33 private:
34 constexpr explicit MethodId(uint32_t id) : id_(id) {}
35 friend constexpr MethodId internal::WrapMethodId(uint32_t id);
36 friend constexpr uint32_t internal::UnwrapMethodId(MethodId id);
37 uint32_t id_;
38};
39
40constexpr bool operator==(MethodId lhs, MethodId rhs) {
41 return internal::UnwrapMethodId(lhs) == internal::UnwrapMethodId(rhs);
42}
43
44constexpr bool operator!=(MethodId lhs, MethodId rhs) { return !(lhs == rhs); }
45
46// Comparisons are provided to enable sorting by `MethodId`.
47
48constexpr bool operator<(MethodId lhs, MethodId rhs) {
49 return internal::UnwrapMethodId(lhs) < internal::UnwrapMethodId(rhs);
50}
51
52constexpr bool operator>(MethodId lhs, MethodId rhs) { return rhs < lhs; }
53
54constexpr bool operator<=(MethodId lhs, MethodId rhs) { return !(lhs > rhs); }
55
56constexpr bool operator>=(MethodId lhs, MethodId rhs) { return !(lhs < rhs); }
57
58namespace internal {
59
60constexpr MethodId WrapMethodId(uint32_t id) { return MethodId(id); }
61constexpr uint32_t UnwrapMethodId(MethodId id) { return id.id_; }
62
63} // namespace internal
64} // namespace pw::rpc
65
66namespace std {
67
68template <>
69struct hash<pw::rpc::MethodId> {
70 size_t operator()(const pw::rpc::MethodId& id) const {
71 return hash<uint32_t>{}(::pw::rpc::internal::UnwrapMethodId(id));
72 }
73};
74
75} // namespace std
Definition: method_id.h:32
The Pigweed namespace.
Definition: alignment.h:27