blob: c42471d6e41b6f1011c8ccd13e1a7fb341aca9a3 [file] [log] [blame]
Josh Gaod5417bc2021-04-27 20:32:01 -07001/*
2 * Copyright (C) 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19#include <stddef.h>
20#include <stdint.h>
21
22enum class TransferDirection : uint64_t {
23 READ = 0,
24 WRITE = 1,
25};
26
27struct TransferId {
28 TransferDirection direction : 1;
29 uint64_t id : 63;
30
31 constexpr TransferId() : TransferId(TransferDirection::READ, 0) {}
32 TransferId(const TransferId& copy) = default;
33 TransferId(TransferId&& move) = default;
34
35 TransferId& operator=(const TransferId& copy) = default;
36 TransferId& operator=(TransferId&& move) = default;
37
38 private:
39 constexpr TransferId(TransferDirection direction, uint64_t id) : direction(direction), id(id) {}
40
41 public:
Josh Gaoe7dd4d12021-04-28 14:07:30 -070042 bool operator==(const TransferId& rhs) const {
43 return static_cast<uint64_t>(*this) == static_cast<uint64_t>(rhs);
44 }
45
Josh Gaod5417bc2021-04-27 20:32:01 -070046 constexpr explicit operator uint64_t() const {
47 return static_cast<uint64_t>(direction) << 63 | id;
48 }
49
50 static constexpr TransferId read(uint64_t id) {
51 return TransferId(TransferDirection::READ, id);
52 }
53
54 static constexpr TransferId write(uint64_t id) {
55 return TransferId(TransferDirection::WRITE, id);
56 }
57
58 static constexpr TransferId from_value(uint64_t value) {
59 uint64_t mask = static_cast<uint64_t>(1) << 63;
60
61 TransferId result;
62 result.direction = static_cast<TransferDirection>(!!(value & mask));
63 result.id = value & (mask - 1);
64 return result;
65 }
66};
67
Josh Gaoe7dd4d12021-04-28 14:07:30 -070068namespace std {
69template <>
70struct hash<TransferId> {
71 size_t operator()(TransferId id) const { return hash<uint64_t>()(static_cast<uint64_t>(id)); }
72};
Josh Gaod5417bc2021-04-27 20:32:01 -070073} // namespace std