Skip to content

Commit fa3d086

Browse files
committed
for #1, added newest_item() to return the top of undo/redo stack directly.
1 parent e5a9d69 commit fa3d086

File tree

2 files changed

+52
-28
lines changed

2 files changed

+52
-28
lines changed

include/undo_cxx/undo-zcore.hh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,9 +449,26 @@ namespace undo_cxx {
449449
}
450450
}
451451

452+
// @desc to return the focused item at undo/redo stack.
453+
// @note this api relies on the invocations of one of
454+
// these apis: undo/redo.
455+
// @note _position will be reset to end() once invoke()
456+
// invoked.
452457
MementoPtr &focused_item() { return *_position; }
453458
MementoPtr const &focused_item() const { return *_position; }
454459
std::ptrdiff_t position() { return std::distance(_saved_states.begin(), _position); }
460+
Iterator newest_iterator() { return _saved_states.end(); }
461+
Iterator oldest_iterator() { return _saved_states.begin(); }
462+
// @desc to return the newest item at stack.
463+
// @note this is NOT thread-safe, you must keep watching the
464+
// usages of iterators on multi-thread scene. Or you're working
465+
// for a ui app and it has single main ui-thread to operate
466+
// any of ui-commands.
467+
MementoPtr &newest_item() {
468+
auto it = _position;
469+
it--;
470+
return *it;
471+
}
455472

456473
auto size() const { return _saved_states.size(); }
457474
bool empty() const { return _saved_states.empty(); }

tests/undo.cc

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -120,37 +120,44 @@ namespace dp { namespace undo { namespace bugs {
120120
}}} // namespace dp::undo::bugs
121121

122122
void test_undo_sys() {
123-
using namespace dp::undo::test;
124-
using namespace dp::undo::bugs;
125-
126-
using State = std::string;
127-
using M = undo_cxx::undoable_cmd_system_t<State>;
128-
using UndoCmdT = UndoCmd<State>;
129-
using RedoCmdT = RedoCmd<State>;
130-
using FontStyleCmdT = FontStyleCmd<State>;
131-
132-
M undoable_cmd_system;
133-
134-
// do some stuffs
135-
136-
undoable_cmd_system.invoke<FontStyleCmdT>("italic state1");
137-
undoable_cmd_system.invoke<FontStyleCmdT>("italic-bold state2");
138-
undoable_cmd_system.invoke<FontStyleCmdT>("underline state3");
139-
undoable_cmd_system.invoke<FontStyleCmdT>("italic state4");
140-
141-
// and try to undo or redo
142-
143-
undoable_cmd_system.invoke<UndoCmdT>("undo 1");
144-
undoable_cmd_system.invoke<UndoCmdT>("undo 2");
145-
undoable_cmd_system.invoke<RedoCmdT>("redo 1");
146-
undoable_cmd_system.invoke<UndoCmdT>("undo 3");
147-
undoable_cmd_system.invoke<UndoCmdT>("undo 4");
123+
using namespace dp::undo::test;
124+
using namespace dp::undo::bugs;
125+
126+
using State = std::string;
127+
using M = undo_cxx::undoable_cmd_system_t<State>;
128+
using UndoCmdT = UndoCmd<State>;
129+
using RedoCmdT = RedoCmd<State>;
130+
using FontStyleCmdT = FontStyleCmd<State>;
131+
132+
M undoable_cmd_system;
133+
134+
// do some stuffs
135+
136+
undoable_cmd_system.invoke<FontStyleCmdT>("italic state1");
137+
std::cout << " > newest item: " << *undoable_cmd_system.newest_item() << '\n';
138+
undoable_cmd_system.invoke<FontStyleCmdT>("italic-bold state2");
139+
undoable_cmd_system.invoke<FontStyleCmdT>("underline state3");
140+
undoable_cmd_system.invoke<FontStyleCmdT>("italic state4");
141+
std::cout << " > newest item: " << *undoable_cmd_system.newest_item() << '\n';
142+
143+
// and try to undo or redo
144+
145+
undoable_cmd_system.invoke<UndoCmdT>("undo 1");
146+
std::cout << " > focused item: " << *undoable_cmd_system.focused_item() << ", newest item: " << *undoable_cmd_system.newest_item() << '\n';
147+
undoable_cmd_system.invoke<UndoCmdT>("undo 2");
148+
std::cout << " > focused item: " << *undoable_cmd_system.focused_item() << ", newest item: " << *undoable_cmd_system.newest_item() << '\n';
149+
undoable_cmd_system.invoke<RedoCmdT>("redo 1");
150+
std::cout << " > focused item: " << *undoable_cmd_system.focused_item() << ", newest item: " << *undoable_cmd_system.newest_item() << '\n';
151+
undoable_cmd_system.invoke<UndoCmdT>("undo 3");
152+
std::cout << " > focused item: " << *undoable_cmd_system.focused_item() << ", newest item: " << *undoable_cmd_system.newest_item() << '\n';
153+
undoable_cmd_system.invoke<UndoCmdT>("undo 4");
154+
std::cout << " > focused item: " << *undoable_cmd_system.focused_item() << ", newest item: " << *undoable_cmd_system.newest_item() << '\n';
148155
}
149156

150157
int main() {
151158

152-
// test_undo_basic();
153-
test_undo_sys();
159+
// test_undo_basic();
160+
test_undo_sys();
154161

155-
return 0;
162+
return 0;
156163
}

0 commit comments

Comments
 (0)