WPILibC++ 2024.3.2
StringMapEntry.h
Go to the documentation of this file.
1//===- StringMapEntry.h - String Hash table map interface -------*- 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 the StringMapEntry class - it is intended to be a low
11/// dependency implementation detail of StringMap that is more suitable for
12/// inclusion in public headers than StringMap.h itself is.
13///
14//===----------------------------------------------------------------------===//
15
16#ifndef WPIUTIL_WPI_STRINGMAPENTRY_H
17#define WPIUTIL_WPI_STRINGMAPENTRY_H
18
19#include "wpi/MemAlloc.h"
20
21#include <cassert>
22#include <cstring>
23#include <optional>
24#include <string_view>
25
26namespace wpi {
27
28/// StringMapEntryBase - Shared base class of StringMapEntry instances.
30 size_t keyLength;
31
32public:
33 explicit StringMapEntryBase(size_t keyLength) : keyLength(keyLength) {}
34
35 size_t getKeyLength() const { return keyLength; }
36
37protected:
38 /// Helper to tail-allocate \p Key. It'd be nice to generalize this so it
39 /// could be reused elsewhere, maybe even taking an wpi::function_ref to
40 /// type-erase the allocator and put it in a source file.
41 template <typename AllocatorTy>
42 static void *allocateWithKey(size_t EntrySize, size_t EntryAlign,
43 std::string_view Key, AllocatorTy &Allocator);
44};
45
46// Define out-of-line to dissuade inlining.
47template <typename AllocatorTy>
48void *StringMapEntryBase::allocateWithKey(size_t EntrySize, size_t EntryAlign,
50 AllocatorTy &Allocator) {
51 size_t KeyLength = Key.size();
52
53 // Allocate a new item with space for the string at the end and a null
54 // terminator.
55 size_t AllocSize = EntrySize + KeyLength + 1;
56 void *Allocation = Allocator.Allocate(AllocSize, EntryAlign);
57 assert(Allocation && "Unhandled out-of-memory");
58
59 // Copy the string information.
60 char *Buffer = reinterpret_cast<char *>(Allocation) + EntrySize;
61 if (KeyLength > 0)
62 ::memcpy(Buffer, Key.data(), KeyLength);
63 Buffer[KeyLength] = 0; // Null terminate for convenience of clients.
64 return Allocation;
65}
66
67/// StringMapEntryStorage - Holds the value in a StringMapEntry.
68///
69/// Factored out into a separate base class to make it easier to specialize.
70/// This is primarily intended to support StringSet, which doesn't need a value
71/// stored at all.
72template <typename ValueTy>
74public:
75 ValueTy second;
76
77 explicit StringMapEntryStorage(size_t keyLength)
78 : StringMapEntryBase(keyLength), second() {}
79 template <typename... InitTy>
80 StringMapEntryStorage(size_t keyLength, InitTy &&...initVals)
81 : StringMapEntryBase(keyLength),
82 second(std::forward<InitTy>(initVals)...) {}
84
85 const ValueTy &getValue() const { return second; }
86 ValueTy &getValue() { return second; }
87
88 void setValue(const ValueTy &V) { second = V; }
89};
90
91template <>
92class StringMapEntryStorage<std::nullopt_t> : public StringMapEntryBase {
93public:
94 explicit StringMapEntryStorage(size_t keyLength,
95 std::nullopt_t = std::nullopt)
96 : StringMapEntryBase(keyLength) {}
98
99 std::nullopt_t getValue() const { return std::nullopt; }
100};
101
102/// StringMapEntry - This is used to represent one value that is inserted into
103/// a StringMap. It contains the Value itself and the key: the string length
104/// and data.
105template <typename ValueTy>
106class StringMapEntry final : public StringMapEntryStorage<ValueTy> {
107public:
109
110 using ValueType = ValueTy;
111
113 return std::string_view(getKeyData(), this->getKeyLength());
114 }
115
116 /// getKeyData - Return the start of the string data that is the key for this
117 /// value. The string data is always stored immediately after the
118 /// StringMapEntry object.
119 const char *getKeyData() const {
120 return reinterpret_cast<const char *>(this + 1);
121 }
122
124 return std::string_view(getKeyData(), this->getKeyLength());
125 }
126
127 /// Create a StringMapEntry for the specified key construct the value using
128 /// \p InitiVals.
129 template <typename AllocatorTy, typename... InitTy>
130 static StringMapEntry *create(std::string_view key, AllocatorTy &allocator,
131 InitTy &&... initVals) {
133 sizeof(StringMapEntry), alignof(StringMapEntry), key, allocator))
134 StringMapEntry(key.size(), std::forward<InitTy>(initVals)...);
135 }
136
137 /// GetStringMapEntryFromKeyData - Given key data that is known to be embedded
138 /// into a StringMapEntry, return the StringMapEntry itself.
139 static StringMapEntry &GetStringMapEntryFromKeyData(const char *keyData) {
140 char *ptr = const_cast<char *>(keyData) - sizeof(StringMapEntry<ValueTy>);
141 return *reinterpret_cast<StringMapEntry *>(ptr);
142 }
143
144 /// Destroy - Destroy this StringMapEntry, releasing memory back to the
145 /// specified allocator.
146 template <typename AllocatorTy> void Destroy(AllocatorTy &allocator) {
147 // Free memory referenced by the item.
148 size_t AllocSize = sizeof(StringMapEntry) + this->getKeyLength() + 1;
149 this->~StringMapEntry();
150 allocator.Deallocate(static_cast<void *>(this), AllocSize,
151 alignof(StringMapEntry));
152 }
153};
154
155// Allow structured bindings on StringMapEntry.
156template <std::size_t Index, typename ValueTy>
157decltype(auto) get(const StringMapEntry<ValueTy> &E) {
158 static_assert(Index < 2);
159 if constexpr (Index == 0)
160 return E.first();
161 else
162 return E.second;
163}
164
165} // end namespace wpi
166
167namespace std {
168template <typename ValueTy>
169struct tuple_size<wpi::StringMapEntry<ValueTy>>
170 : std::integral_constant<std::size_t, 2> {};
171
172template <std::size_t I, typename ValueTy>
173struct tuple_element<I, wpi::StringMapEntry<ValueTy>>
174 : std::conditional<I == 0, std::string_view, ValueTy> {};
175} // namespace std
176
177#endif // WPIUTIL_WPI_STRINGMAPENTRY_H
This file defines counterparts of C library allocation functions defined in the namespace 'std'.
StringMapEntryBase - Shared base class of StringMapEntry instances.
Definition: StringMapEntry.h:29
static void * allocateWithKey(size_t EntrySize, size_t EntryAlign, std::string_view Key, AllocatorTy &Allocator)
Helper to tail-allocate Key.
Definition: StringMapEntry.h:48
StringMapEntryBase(size_t keyLength)
Definition: StringMapEntry.h:33
size_t getKeyLength() const
Definition: StringMapEntry.h:35
StringMapEntry - This is used to represent one value that is inserted into a StringMap.
Definition: StringMapEntry.h:106
static StringMapEntry & GetStringMapEntryFromKeyData(const char *keyData)
GetStringMapEntryFromKeyData - Given key data that is known to be embedded into a StringMapEntry,...
Definition: StringMapEntry.h:139
std::string_view first() const
Definition: StringMapEntry.h:123
ValueTy ValueType
Definition: StringMapEntry.h:110
static StringMapEntry * create(std::string_view key, AllocatorTy &allocator, InitTy &&... initVals)
Create a StringMapEntry for the specified key construct the value using InitiVals.
Definition: StringMapEntry.h:130
const char * getKeyData() const
getKeyData - Return the start of the string data that is the key for this value.
Definition: StringMapEntry.h:119
std::string_view getKey() const
Definition: StringMapEntry.h:112
void Destroy(AllocatorTy &allocator)
Destroy - Destroy this StringMapEntry, releasing memory back to the specified allocator.
Definition: StringMapEntry.h:146
std::nullopt_t getValue() const
Definition: StringMapEntry.h:99
StringMapEntryStorage(size_t keyLength, std::nullopt_t=std::nullopt)
Definition: StringMapEntry.h:94
StringMapEntryStorage(StringMapEntryStorage &entry)=delete
StringMapEntryStorage - Holds the value in a StringMapEntry.
Definition: StringMapEntry.h:73
StringMapEntryStorage(size_t keyLength)
Definition: StringMapEntry.h:77
const ValueTy & getValue() const
Definition: StringMapEntry.h:85
StringMapEntryStorage(size_t keyLength, InitTy &&...initVals)
Definition: StringMapEntry.h:80
void setValue(const ValueTy &V)
Definition: StringMapEntry.h:88
ValueTy & getValue()
Definition: StringMapEntry.h:86
StringMapEntryStorage(StringMapEntryStorage &e)=delete
ValueTy second
Definition: StringMapEntry.h:75
basic_string_view< char > string_view
Definition: core.h:501
auto ptr(T p) -> const void *
\rst Converts p to const void* for pointer formatting.
Definition: format.h:4100
Definition: array.h:89
static constexpr const charge::coulomb_t e(1.6021766208e-19)
elementary charge.
Definition: ntcore_cpp.h:26
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
Definition: PointerIntPair.h:270