File indexing completed on 2024-05-12 04:02:20
0001 /* 0002 SPDX-FileCopyrightText: 2016 Volker Krause <vkrause@kde.org> 0003 SPDX-FileCopyrightText: 2018 Christoph Cullmann <cullmann@kde.org> 0004 0005 SPDX-License-Identifier: MIT 0006 */ 0007 0008 #ifndef KSYNTAXHIGHLIGHTING_STATE_P_H 0009 #define KSYNTAXHIGHLIGHTING_STATE_P_H 0010 0011 #include <vector> 0012 0013 #include <QSharedData> 0014 #include <QStringList> 0015 0016 #include "definitionref_p.h" 0017 0018 namespace KSyntaxHighlighting 0019 { 0020 class Context; 0021 0022 class StateData : public QSharedData 0023 { 0024 friend class State; 0025 friend class AbstractHighlighter; 0026 friend std::size_t qHash(const StateData &, std::size_t); 0027 0028 public: 0029 StateData() = default; 0030 0031 static StateData *reset(State &state); 0032 static StateData *detach(State &state); 0033 0034 static StateData *get(const State &state) 0035 { 0036 return state.d.data(); 0037 } 0038 0039 int size() const 0040 { 0041 return m_contextStack.size(); 0042 } 0043 0044 void push(Context *context, QStringList &&captures); 0045 0046 /** 0047 * Pop the number of elements given from the top of the current stack. 0048 * Will not pop the initial element. 0049 * @param popCount number of elements to pop 0050 * @return false if one has tried to pop the initial context, else true 0051 */ 0052 bool pop(int popCount); 0053 0054 Context *topContext() const 0055 { 0056 return m_contextStack.back().context; 0057 } 0058 0059 const QStringList &topCaptures() const 0060 { 0061 return m_contextStack.back().captures; 0062 } 0063 0064 struct StackValue { 0065 Context *context; 0066 QStringList captures; 0067 0068 bool operator==(const StackValue &other) const 0069 { 0070 return context == other.context && captures == other.captures; 0071 } 0072 }; 0073 0074 private: 0075 /** 0076 * definition id to filter out invalid states 0077 */ 0078 uint64_t m_defId = 0; 0079 0080 /** 0081 * the context stack combines the active context + valid captures 0082 */ 0083 std::vector<StackValue> m_contextStack; 0084 }; 0085 0086 inline std::size_t qHash(const StateData::StackValue &stackValue, std::size_t seed = 0) 0087 { 0088 return qHashMulti(seed, stackValue.context, stackValue.captures); 0089 } 0090 0091 inline std::size_t qHash(const StateData &k, std::size_t seed = 0) 0092 { 0093 return qHashMulti(seed, k.m_defId, qHashRange(k.m_contextStack.begin(), k.m_contextStack.end(), seed)); 0094 } 0095 } 0096 0097 #endif