WPILibC++ 2024.3.2
DenseMapInfoVariant.h
Go to the documentation of this file.
1//===- DenseMapInfoVariant.h - Type traits for DenseMap<variant> *- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// This file defines DenseMapInfo traits for DenseMap<std::variant<Ts...>>.
11///
12//===----------------------------------------------------------------------===//
13
14#ifndef WPIUTIL_WPI_DENSEMAPINFOVARIANT_H
15#define WPIUTIL_WPI_DENSEMAPINFOVARIANT_H
16
17#include "wpi/DenseMapInfo.h"
18#include <utility>
19#include <variant>
20
21namespace wpi {
22
23// Provide DenseMapInfo for variants whose all alternatives have DenseMapInfo.
24template <typename... Ts> struct DenseMapInfo<std::variant<Ts...>> {
25 using Variant = std::variant<Ts...>;
26 using FirstT = std::variant_alternative_t<0, Variant>;
27
28 static inline Variant getEmptyKey() {
29 return Variant(std::in_place_index<0>, DenseMapInfo<FirstT>::getEmptyKey());
30 }
31
32 static inline Variant getTombstoneKey() {
33 return Variant(std::in_place_index<0>,
35 }
36
37 static unsigned getHashValue(const Variant &Val) {
38 return std::visit(
39 [&Val](auto &&Alternative) {
40 using T = std::decay_t<decltype(Alternative)>;
41 // Include index in hash to make sure same value as different
42 // alternatives don't collide.
43 return DenseMapInfo<std::pair<size_t, T>>::getHashValuePiecewise(
44 Val.index(), Alternative);
45 },
46 Val);
47 }
48
49 static bool isEqual(const Variant &LHS, const Variant &RHS) {
50 if (LHS.index() != RHS.index())
51 return false;
52 if (LHS.valueless_by_exception())
53 return true;
54 // We want to dispatch to DenseMapInfo<T>::isEqual(LHS.get(I), RHS.get(I))
55 // We know the types are the same, but std::visit(V, LHS, RHS) doesn't.
56 // We erase the type held in LHS to void*, and dispatch over RHS.
57 const void *ErasedLHS =
58 std::visit([](const auto &LHS) -> const void * { return &LHS; }, LHS);
59 return std::visit(
60 [&](const auto &RHS) -> bool {
61 using T = std::remove_cv_t<std::remove_reference_t<decltype(RHS)>>;
62 return DenseMapInfo<T>::isEqual(*static_cast<const T *>(ErasedLHS),
63 RHS);
64 },
65 RHS);
66 }
67};
68
69} // end namespace wpi
70
71#endif // WPIUTIL_WPI_DENSEMAPINFOVARIANT_H
This file defines DenseMapInfo traits for DenseMap.
typename std::remove_reference< T >::type remove_reference_t
Definition: core.h:261
Definition: array.h:89
typename std::decay< T >::type decay_t
Definition: expected:231
Definition: ntcore_cpp.h:26
std::variant< Ts... > Variant
Definition: DenseMapInfoVariant.h:25
static Variant getTombstoneKey()
Definition: DenseMapInfoVariant.h:32
std::variant_alternative_t< 0, Variant > FirstT
Definition: DenseMapInfoVariant.h:26
static Variant getEmptyKey()
Definition: DenseMapInfoVariant.h:28
static bool isEqual(const Variant &LHS, const Variant &RHS)
Definition: DenseMapInfoVariant.h:49
static unsigned getHashValue(const Variant &Val)
Definition: DenseMapInfoVariant.h:37
An information struct used to provide DenseMap with the various necessary components for a given valu...
Definition: DenseMapInfo.h:50