From af05d382f26c828a45ba39044cf4e514c0fe9cc6 Mon Sep 17 00:00:00 2001 From: Robin Haberkorn Date: Tue, 22 Nov 2016 16:47:09 +0100 Subject: save some bytes per Q-Register creation on the undo stack * a table reference was stored in the UndoToken. * since there are only two tables at a given moment, this can be avoided by having two different undo tokens, one for globals and one for locals. * Practically, undo tokens for locals are only created for the top-level local Q-Reg table since macro calls with locals with set must_undo to false since the local table is destroyed with the macro return. --- src/qregisters.cpp | 33 +++++++++++++++++++++++++++++++++ src/qregisters.h | 35 ++++++++++++++--------------------- 2 files changed, 47 insertions(+), 21 deletions(-) (limited to 'src') diff --git a/src/qregisters.cpp b/src/qregisters.cpp index 19590c9..7d18910 100644 --- a/src/qregisters.cpp +++ b/src/qregisters.cpp @@ -696,6 +696,39 @@ QRegisterClipboard::undo_exchange_string(QRegisterData ®) reg.undo_set_string(); } +void +QRegisterTable::UndoTokenRemoveGlobal::run(void) +{ + delete (QRegister *)QRegisters::globals.remove(reg); +} + +void +QRegisterTable::UndoTokenRemoveLocal::run(void) +{ + /* + * NOTE: QRegisters::locals should point + * to the correct table when the token is + * executed. + */ + delete (QRegister *)QRegisters::locals->remove(reg); +} + +void +QRegisterTable::undo_remove(QRegister *reg) +{ + if (!must_undo) + return; + + /* + * NOTE: Could also be solved using a virtual + * method and subclasses... + */ + if (this == &QRegisters::globals) + undo.push(reg); + else + undo.push(reg); +} + void QRegisterTable::insert_defaults(void) { diff --git a/src/qregisters.h b/src/qregisters.h index fc7a624..d40a19b 100644 --- a/src/qregisters.h +++ b/src/qregisters.h @@ -285,25 +285,23 @@ public: }; class QRegisterTable : private RBTreeString, public Object { - class UndoTokenRemove : public UndoToken { - /* - * NOTE: Storing the table here is only necessary since - * we may have to remove from a global or local Q-Reg - * table. This could be avoided using a separate - * subclass for local registers. - */ - QRegisterTable *table; + class UndoTokenRemoveGlobal : public UndoToken { + protected: QRegister *reg; public: - UndoTokenRemove(QRegisterTable *_table, QRegister *_reg) - : table(_table), reg(_reg) {} + UndoTokenRemoveGlobal(QRegister *_reg) + : reg(_reg) {} - void - run(void) - { - delete (QRegister *)table->remove(reg); - } + void run(void); + }; + + class UndoTokenRemoveLocal : public UndoTokenRemoveGlobal { + public: + UndoTokenRemoveLocal(QRegister *reg) + : UndoTokenRemoveGlobal(reg) {} + + void run(void); }; bool must_undo; @@ -320,12 +318,7 @@ public: delete (QRegister *)remove(cur); } - inline void - undo_remove(QRegister *reg) - { - if (must_undo) - undo.push(this, reg); - } + void undo_remove(QRegister *reg); inline QRegister * insert(QRegister *reg) -- cgit v1.2.3