Просмотр исходного кода

LibWeb: Add StateTransaction RAII to CSS TokenStream

This is modeled after the one in ISO8601Parser. It rolls back the
TokenStream state automatically at the end of scope unless told to
commit the changes. This should be less error-prone than remembering to
manually call `rewind_to_position()` at the correct time.

For convenience, a StateTransaction can have "child" transactions. When
a transaction is committed, it automatically commits its parents too.
This is useful in situations where you have several nested and don't
want to have to remember to manually `commit()` them all.
Sam Atkins 3 лет назад
Родитель
Сommit
a490f24a2d
1 измененных файлов с 38 добавлено и 0 удалено
  1. 38 0
      Userland/Libraries/LibWeb/CSS/Parser/Parser.h

+ 38 - 0
Userland/Libraries/LibWeb/CSS/Parser/Parser.h

@@ -55,6 +55,43 @@ private:
 template<typename T>
 class TokenStream {
 public:
+    class StateTransaction {
+    public:
+        explicit StateTransaction(TokenStream<T>& token_stream)
+            : m_token_stream(token_stream)
+            , m_saved_iterator_offset(token_stream.m_iterator_offset)
+        {
+        }
+
+        ~StateTransaction()
+        {
+            if (!m_commit)
+                m_token_stream.m_iterator_offset = m_saved_iterator_offset;
+        }
+
+        StateTransaction create_child() { return StateTransaction(*this); }
+
+        void commit()
+        {
+            m_commit = true;
+            if (m_parent)
+                m_parent->commit();
+        }
+
+    private:
+        explicit StateTransaction(StateTransaction& parent)
+            : m_parent(&parent)
+            , m_token_stream(parent.m_token_stream)
+            , m_saved_iterator_offset(parent.m_token_stream.m_iterator_offset)
+        {
+        }
+
+        StateTransaction* m_parent { nullptr };
+        TokenStream<T>& m_token_stream;
+        int m_saved_iterator_offset { 0 };
+        bool m_commit { false };
+    };
+
     explicit TokenStream(Vector<T> const&);
     ~TokenStream() = default;
 
@@ -66,6 +103,7 @@ public:
     T const& current_token();
     void reconsume_current_input_token();
 
+    StateTransaction begin_transaction() { return StateTransaction(*this); }
     int position() const { return m_iterator_offset; }
     void rewind_to_position(int);