Command.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #ifndef _Command_h_
  2. #define _Command_h_
  3. /* Command.h
  4. *
  5. * Copyright (C) 1994-2017 David Weenink, 2015 Paul Boersma
  6. *
  7. * This code is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * This code is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this work. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include "Thing.h"
  21. #include "Collection.h"
  22. #pragma mark - class Command
  23. Thing_declare (Command);
  24. typedef MelderCallback<int, structCommand> Command_Callback;
  25. Thing_define (Command, Thing) {
  26. Thing boss;
  27. Command_Callback execute;
  28. Command_Callback undo;
  29. };
  30. void Command_init (Command me, conststring32 name, Thing boss, Command_Callback execute, Command_Callback undo);
  31. int Command_do (Command me);
  32. int Command_undo (Command me);
  33. #pragma mark - class CommandHistory
  34. Collection_define (CommandHistory, OrderedOf, Command) {
  35. integer current;
  36. };
  37. /* Active data structure. 'current' is position of the cursor in the list */
  38. /* Queries and insertions are at the current position */
  39. /* Invariants: */
  40. /* 0 <= current <= size + 1; */
  41. void CommandHistory_forth (CommandHistory me);
  42. /* Precondition: ! offright */
  43. /* my current++; */
  44. void CommandHistory_back (CommandHistory me);
  45. /* Precondition: ! offleft */
  46. /* my current--; */
  47. Command CommandHistory_getItem (CommandHistory me);
  48. /* return (pointer to) my item[my current]; */
  49. void CommandHistory_insertItem_move (CommandHistory me, autoCommand command);
  50. /* 1. forget about item[ current+1..size ] */
  51. /* 2. insert item after current. */
  52. /* 3. current = size */
  53. int CommandHistory_empty (CommandHistory me);
  54. /* return my size == 0; */
  55. int CommandHistory_offleft (CommandHistory me);
  56. /* return my current == 0; */
  57. int CommandHistory_offright (CommandHistory me);
  58. /* return my size == 0 || my current == my size + 1; */
  59. conststring32 CommandHistory_commandName (CommandHistory me, integer offsetFromCurrent);
  60. /* offsetFromCurrent may be zero, positive or negative. */
  61. /* References outside the list will return nullptr. */
  62. #endif /* _Command_h_ */