1 /* 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 22 enum class TransferDirection : uint64_t { 23 READ = 0, 24 WRITE = 1, 25 }; 26 27 struct TransferId { 28 TransferDirection direction : 1; 29 uint64_t id : 63; 30 TransferIdTransferId31 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: TransferIdTransferId39 constexpr TransferId(TransferDirection direction, uint64_t id) : direction(direction), id(id) {} 40 41 public: 42 bool operator==(const TransferId& rhs) const { 43 return static_cast<uint64_t>(*this) == static_cast<uint64_t>(rhs); 44 } 45 uint64_tTransferId46 constexpr explicit operator uint64_t() const { 47 return static_cast<uint64_t>(direction) << 63 | id; 48 } 49 readTransferId50 static constexpr TransferId read(uint64_t id) { 51 return TransferId(TransferDirection::READ, id); 52 } 53 writeTransferId54 static constexpr TransferId write(uint64_t id) { 55 return TransferId(TransferDirection::WRITE, id); 56 } 57 from_valueTransferId58 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 68 namespace std { 69 template <> 70 struct hash<TransferId> { 71 size_t operator()(TransferId id) const { return hash<uint64_t>()(static_cast<uint64_t>(id)); } 72 }; 73 } // namespace std 74