Command.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* Command.cpp
  2. *
  3. * Copyright (C) 1994-2017 David Weenink
  4. *
  5. * This code is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This code is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this work. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "Command.h"
  19. #pragma mark - class Command
  20. Thing_implement (Command, Thing, 0);
  21. void Command_init (Command me, conststring32 name, Thing boss, Command_Callback execute, Command_Callback undo) {
  22. Melder_assert (execute && undo);
  23. Thing_setName (me, name);
  24. my boss = boss;
  25. my execute = execute;
  26. my undo = undo;
  27. }
  28. int Command_do (Command me) {
  29. return my execute (me);
  30. }
  31. int Command_undo (Command me) {
  32. return my undo (me);
  33. }
  34. #pragma mark - class CommandHistory
  35. Thing_implement (CommandHistory, Ordered, 0);
  36. void CommandHistory_forth (CommandHistory me) {
  37. my current ++;
  38. }
  39. void CommandHistory_back (CommandHistory me) {
  40. my current --;
  41. }
  42. Command CommandHistory_getItem (CommandHistory me) {
  43. Melder_assert (my current > 0 && my current <= my size);
  44. return my at [my current];
  45. }
  46. void CommandHistory_insertItem_move (CommandHistory me, autoCommand command) {
  47. for (integer i = my size; i >= my current + 1; i --) {
  48. my removeItem (i);
  49. }
  50. my addItem_move (command.move());
  51. while (my size > 20) {
  52. my removeItem (1);
  53. }
  54. my current = my size;
  55. }
  56. int CommandHistory_empty (CommandHistory me) {
  57. return my size == 0;
  58. }
  59. int CommandHistory_offleft (CommandHistory me) {
  60. return my current == 0;
  61. }
  62. int CommandHistory_offright (CommandHistory me) {
  63. return my size == 0 || my current == my size + 1;
  64. }
  65. conststring32 CommandHistory_commandName (CommandHistory me, integer offsetFromCurrent) {
  66. integer pos = my current + offsetFromCurrent;
  67. return pos >= 1 && pos <= my size ? Thing_getName (my at [pos]) : nullptr;
  68. }
  69. /* End of file Command.cpp */