WPILibC++ 2023.4.3-108-ge5452e3
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 <> class StringMapEntryStorage<std::nullopt_t> : public StringMapEntryBase {
92public:
93 explicit StringMapEntryStorage(size_t keyLength, std::nullopt_t = std::nullopt)
94 : StringMapEntryBase(keyLength) {}
96
97 std::nullopt_t getValue() const { return std::nullopt; }
98};
99
100/// StringMapEntry - This is used to represent one value that is inserted into
101/// a StringMap. It contains the Value itself and the key: the string length
102/// and data.
103template <typename ValueTy>
104class StringMapEntry final : public StringMapEntryStorage<ValueTy> {
105public:
107
109 return std::string_view(getKeyData(), this->getKeyLength());
110 }
111
112 /// getKeyData - Return the start of the string data that is the key for this
113 /// value. The string data is always stored immediately after the
114 /// StringMapEntry object.
115 const char *getKeyData() const {
116 return reinterpret_cast<const char *>(this + 1);
117 }
118
120 return std::string_view(getKeyData(), this->getKeyLength());
121 }
122
123 /// Create a StringMapEntry for the specified key construct the value using
124 /// \p InitiVals.
125 template <typename AllocatorTy, typename... InitTy>
126 static StringMapEntry *Create(std::string_view key, AllocatorTy &allocator,
127 InitTy &&... initVals) {
129 sizeof(StringMapEntry), alignof(StringMapEntry), key, allocator))
130 StringMapEntry(key.size(), std::forward<InitTy>(initVals)...);
131 }
132
133 /// GetStringMapEntryFromKeyData - Given key data that is known to be embedded
134 /// into a StringMapEntry, return the StringMapEntry itself.
135 static StringMapEntry &GetStringMapEntryFromKeyData(const char *keyData) {
136 char *ptr = const_cast<char *>(keyData) - sizeof(StringMapEntry<ValueTy>);
137 return *reinterpret_cast<StringMapEntry *>(ptr);
138 }
139
140 /// Destroy - Destroy this StringMapEntry, releasing memory back to the
141 /// specified allocator.
142 template <typename AllocatorTy> void Destroy(AllocatorTy &allocator) {
143 // Free memory referenced by the item.
144 size_t AllocSize = sizeof(StringMapEntry) + this->getKeyLength() + 1;
145 this->~StringMapEntry();
146 allocator.Deallocate(static_cast<void *>(this), AllocSize,
147 alignof(StringMapEntry));
148 }
149};
150
151} // end namespace wpi
152
153#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:104
static StringMapEntry & GetStringMapEntryFromKeyData(const char *keyData)
GetStringMapEntryFromKeyData - Given key data that is known to be embedded into a StringMapEntry,...
Definition: StringMapEntry.h:135
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:126
std::string_view first() const
Definition: StringMapEntry.h:119
const char * getKeyData() const
getKeyData - Return the start of the string data that is the key for this value.
Definition: StringMapEntry.h:115
std::string_view getKey() const
Definition: StringMapEntry.h:108
void Destroy(AllocatorTy &allocator)
Destroy - Destroy this StringMapEntry, releasing memory back to the specified allocator.
Definition: StringMapEntry.h:142
std::nullopt_t getValue() const
Definition: StringMapEntry.h:97
StringMapEntryStorage(size_t keyLength, std::nullopt_t=std::nullopt)
Definition: StringMapEntry.h:93
StringMapEntryStorage(StringMapEntryStorage &entry)=delete
StringMapEntryStorage - Holds the value in a StringMapEntry.
Definition: StringMapEntry.h:73
StringMapEntryStorage(size_t keyLength, InitTy &&... initVals)
Definition: StringMapEntry.h:80
StringMapEntryStorage(size_t keyLength)
Definition: StringMapEntry.h:77
const ValueTy & getValue() const
Definition: StringMapEntry.h:85
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:520
auto ptr(T p) -> const void *
\rst Converts p to const void* for pointer formatting.
Definition: format.h:3823
Definition: BFloat16.h:88
static constexpr const charge::coulomb_t e(1.6021766208e-19)
elementary charge.
Definition: AprilTagFieldLayout.h:18