mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-08-09 11:25:55 +00:00
Simplify this class by removing the result cache.
This change removes the DefaultConstructible and CopyAssignable constraints on the template parameter T (the first one). The second template parameter (R) is defaulted to be identical to the first and controls the result type. By specifying it to be (const T&) additionally the CopyConstructible constraint on T can be removed. This allows to use StringSwitch e.g. for llvm::Constant instances. Regarding the other review feedback regarding performance because of taking pointers, this class should be completely optimizable like before, since all methods are inline and the pointer dereferencing and result value caching should be possible behind the scenes by the "as-if" rule. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@91123 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
@@ -38,28 +38,24 @@ namespace llvm {
|
|||||||
/// .Cases("violet", "purple", Violet)
|
/// .Cases("violet", "purple", Violet)
|
||||||
/// .Default(UnknownColor);
|
/// .Default(UnknownColor);
|
||||||
/// \endcode
|
/// \endcode
|
||||||
template<typename T>
|
template<typename T, typename R = T>
|
||||||
class StringSwitch {
|
class StringSwitch {
|
||||||
/// \brief The string we are matching.
|
/// \brief The string we are matching.
|
||||||
StringRef Str;
|
StringRef Str;
|
||||||
|
|
||||||
/// \brief The result of this switch statement, once known.
|
/// \brief The pointer to the result of this switch statement, once known,
|
||||||
T Result;
|
/// null before that.
|
||||||
|
const T *Result;
|
||||||
/// \brief Set true when the result of this switch is already known; in this
|
|
||||||
/// case, Result is valid.
|
|
||||||
bool ResultKnown;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit StringSwitch(StringRef Str)
|
explicit StringSwitch(StringRef Str)
|
||||||
: Str(Str), ResultKnown(false) { }
|
: Str(Str), Result(0) { }
|
||||||
|
|
||||||
template<unsigned N>
|
template<unsigned N>
|
||||||
StringSwitch& Case(const char (&S)[N], const T& Value) {
|
StringSwitch& Case(const char (&S)[N], const T& Value) {
|
||||||
if (!ResultKnown && N-1 == Str.size() &&
|
if (!Result && N-1 == Str.size() &&
|
||||||
(std::memcmp(S, Str.data(), N-1) == 0)) {
|
(std::memcmp(S, Str.data(), N-1) == 0)) {
|
||||||
Result = Value;
|
Result = &Value;
|
||||||
ResultKnown = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return *this;
|
return *this;
|
||||||
@@ -92,16 +88,16 @@ public:
|
|||||||
.Case(S4, Value);
|
.Case(S4, Value);
|
||||||
}
|
}
|
||||||
|
|
||||||
T Default(const T& Value) {
|
R Default(const T& Value) const {
|
||||||
if (ResultKnown)
|
if (Result)
|
||||||
return Result;
|
return *Result;
|
||||||
|
|
||||||
return Value;
|
return Value;
|
||||||
}
|
}
|
||||||
|
|
||||||
operator T() {
|
operator R() const {
|
||||||
assert(ResultKnown && "Fell off the end of a string-switch");
|
assert(Result && "Fell off the end of a string-switch");
|
||||||
return Result;
|
return *Result;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user