1//===--- StringSwitch.h - Switch-on-literal-string Construct --------------===/ 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//===----------------------------------------------------------------------===/ 9/// This file implements the StringSwitch template, which mimics a switch() 10/// statement whose cases are string literals. 12//===----------------------------------------------------------------------===/ 13#ifndef LLVM_ADT_STRINGSWITCH_H 14#define LLVM_ADT_STRINGSWITCH_H 24/// A switch()-like statement whose cases are string literals. 26/// The StringSwitch class is a simple form of a switch() statement that 27/// determines whether the given string matches one of the given string 28/// literals. The template type parameter \p T is the type of the value that 29/// will be returned from the string-switch expression. For example, 30/// the following code switches on the name of a color in \c argv[i]: 33/// Color color = StringSwitch<Color>(argv[i]) 35/// .Case("orange", Orange) 36/// .Case("yellow", Yellow) 37/// .Case("green", Green) 38/// .Case("blue", Blue) 39/// .Case("indigo", Indigo) 40/// .Cases("violet", "purple", Violet) 41/// .Default(UnknownColor); 43template<
typename T,
typename R = T>
45 /// The string we are matching. 48 /// The pointer to the result of this switch statement, once known, 50 std::optional<T> Result;
54 : Str(S), Result() { }
56// StringSwitch is not copyable. 59// StringSwitch is not assignable due to 'Str' being 'const'. 64 : Str(other.Str), Result(
std::
move(other.Result)) { }
68// Case-sensitive case matchers 70if (!Result && Str == S) {
71 Result = std::move(
Value);
77if (!Result && Str.ends_with(S)) {
78 Result = std::move(
Value);
84if (!Result && Str.starts_with(S)) {
85 Result = std::move(
Value);
138returnCase(S0,
Value).
Cases(
S1, S2, S3, S4, S5, S6, S7,
S8, S9,
Value);
141// Case-insensitive case matchers. 143if (!Result && Str.equals_insensitive(S))
144 Result = std::move(
Value);
150if (!Result && Str.ends_with_insensitive(S))
157if (!Result && Str.starts_with_insensitive(S))
158 Result = std::move(
Value);
184return std::move(*Result);
188 [[nodiscard]]
operator R() {
189assert(Result &&
"Fell off the end of a string-switch");
190return std::move(*Result);
194}
// end namespace llvm 196#endif// LLVM_ADT_STRINGSWITCH_H assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
A wrapper around a string literal that serves as a proxy for constructing global tables of StringRefs...
StringRef - Represent a constant reference to a string, i.e.
A switch()-like statement whose cases are string literals.
StringSwitch & CasesLower(StringLiteral S0, StringLiteral S1, StringLiteral S2, StringLiteral S3, StringLiteral S4, T Value)
StringSwitch & Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2, StringLiteral S3, StringLiteral S4, StringLiteral S5, T Value)
StringSwitch & EndsWithLower(StringLiteral S, T Value)
StringSwitch & StartsWithLower(StringLiteral S, T Value)
StringSwitch & CaseLower(StringLiteral S, T Value)
StringSwitch & Case(StringLiteral S, T Value)
StringSwitch & CasesLower(StringLiteral S0, StringLiteral S1, T Value)
StringSwitch & Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2, StringLiteral S3, StringLiteral S4, T Value)
void operator=(const StringSwitch &)=delete
StringSwitch & CasesLower(StringLiteral S0, StringLiteral S1, StringLiteral S2, StringLiteral S3, T Value)
StringSwitch & StartsWith(StringLiteral S, T Value)
StringSwitch & Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2, StringLiteral S3, StringLiteral S4, StringLiteral S5, StringLiteral S6, StringLiteral S7, StringLiteral S8, T Value)
StringSwitch & Cases(StringLiteral S0, StringLiteral S1, T Value)
StringSwitch(const StringSwitch &)=delete
StringSwitch & Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2, StringLiteral S3, T Value)
StringSwitch & EndsWith(StringLiteral S, T Value)
StringSwitch & Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2, StringLiteral S3, StringLiteral S4, StringLiteral S5, StringLiteral S6, StringLiteral S7, T Value)
StringSwitch(StringSwitch &&other)
void operator=(StringSwitch &&other)=delete
StringSwitch & Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2, StringLiteral S3, StringLiteral S4, StringLiteral S5, StringLiteral S6, T Value)
StringSwitch & Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2, T Value)
StringSwitch(StringRef S)
StringSwitch & Cases(StringLiteral S0, StringLiteral S1, StringLiteral S2, StringLiteral S3, StringLiteral S4, StringLiteral S5, StringLiteral S6, StringLiteral S7, StringLiteral S8, StringLiteral S9, T Value)
StringSwitch & CasesLower(StringLiteral S0, StringLiteral S1, StringLiteral S2, T Value)
LLVM Value Representation.
This is an optimization pass for GlobalISel generic memory operations.
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Implement std::hash so that hash_code can be used in STL containers.