1*9e3b08aeSAndroid Build Coastguard Worker // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 2*9e3b08aeSAndroid Build Coastguard Worker // -*- mode: C++ -*- 3*9e3b08aeSAndroid Build Coastguard Worker // 4*9e3b08aeSAndroid Build Coastguard Worker // Copyright 2023-2024 Google LLC 5*9e3b08aeSAndroid Build Coastguard Worker // 6*9e3b08aeSAndroid Build Coastguard Worker // Licensed under the Apache License v2.0 with LLVM Exceptions (the 7*9e3b08aeSAndroid Build Coastguard Worker // "License"); you may not use this file except in compliance with the 8*9e3b08aeSAndroid Build Coastguard Worker // License. You may obtain a copy of the License at 9*9e3b08aeSAndroid Build Coastguard Worker // 10*9e3b08aeSAndroid Build Coastguard Worker // https://llvm.org/LICENSE.txt 11*9e3b08aeSAndroid Build Coastguard Worker // 12*9e3b08aeSAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software 13*9e3b08aeSAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS, 14*9e3b08aeSAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15*9e3b08aeSAndroid Build Coastguard Worker // See the License for the specific language governing permissions and 16*9e3b08aeSAndroid Build Coastguard Worker // limitations under the License. 17*9e3b08aeSAndroid Build Coastguard Worker // 18*9e3b08aeSAndroid Build Coastguard Worker // Author: Giuliano Procida 19*9e3b08aeSAndroid Build Coastguard Worker 20*9e3b08aeSAndroid Build Coastguard Worker #ifndef STG_HEX_H_ 21*9e3b08aeSAndroid Build Coastguard Worker #define STG_HEX_H_ 22*9e3b08aeSAndroid Build Coastguard Worker 23*9e3b08aeSAndroid Build Coastguard Worker #include <cstddef> // for std::size_t 24*9e3b08aeSAndroid Build Coastguard Worker #include <functional> // for std::hash 25*9e3b08aeSAndroid Build Coastguard Worker #include <ios> 26*9e3b08aeSAndroid Build Coastguard Worker #include <ostream> 27*9e3b08aeSAndroid Build Coastguard Worker 28*9e3b08aeSAndroid Build Coastguard Worker namespace stg { 29*9e3b08aeSAndroid Build Coastguard Worker 30*9e3b08aeSAndroid Build Coastguard Worker template <typename T> 31*9e3b08aeSAndroid Build Coastguard Worker struct Hex { HexHex32*9e3b08aeSAndroid Build Coastguard Worker explicit Hex(const T& value) : value(value) {} 33*9e3b08aeSAndroid Build Coastguard Worker auto operator<=>(const Hex<T>& other) const = default; 34*9e3b08aeSAndroid Build Coastguard Worker T value; 35*9e3b08aeSAndroid Build Coastguard Worker }; 36*9e3b08aeSAndroid Build Coastguard Worker 37*9e3b08aeSAndroid Build Coastguard Worker template <typename T> Hex(const T&) -> Hex<T>; 38*9e3b08aeSAndroid Build Coastguard Worker 39*9e3b08aeSAndroid Build Coastguard Worker template <typename T> 40*9e3b08aeSAndroid Build Coastguard Worker std::ostream& operator<<(std::ostream& os, const Hex<T>& hex_value) { 41*9e3b08aeSAndroid Build Coastguard Worker // not quite right if an exception is thrown 42*9e3b08aeSAndroid Build Coastguard Worker const auto flags = os.flags(); 43*9e3b08aeSAndroid Build Coastguard Worker os << "0x" << std::hex << hex_value.value; 44*9e3b08aeSAndroid Build Coastguard Worker os.flags(flags); 45*9e3b08aeSAndroid Build Coastguard Worker return os; 46*9e3b08aeSAndroid Build Coastguard Worker } 47*9e3b08aeSAndroid Build Coastguard Worker 48*9e3b08aeSAndroid Build Coastguard Worker } // namespace stg 49*9e3b08aeSAndroid Build Coastguard Worker 50*9e3b08aeSAndroid Build Coastguard Worker template <typename T> 51*9e3b08aeSAndroid Build Coastguard Worker struct std::hash<stg::Hex<T>> { 52*9e3b08aeSAndroid Build Coastguard Worker std::size_t operator()(const stg::Hex<T>& hex) const noexcept { 53*9e3b08aeSAndroid Build Coastguard Worker return std::hash<T>{}(hex.value); 54*9e3b08aeSAndroid Build Coastguard Worker } 55*9e3b08aeSAndroid Build Coastguard Worker }; 56*9e3b08aeSAndroid Build Coastguard Worker 57*9e3b08aeSAndroid Build Coastguard Worker #endif // STG_HEX_H_ 58