Advertisement
anechka_ne_plach

Untitled

Oct 29th, 2021
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.54 KB | None | 0 0
  1. #include <memory>
  2. #include <string>
  3.  
  4. class ICommand {
  5. public:
  6.     virtual void Do() = 0;
  7.     virtual void Undo() = 0;
  8.     virtual ~ICommand() = default;
  9. };
  10.  
  11. class Editor {
  12.     friend class TypeCommand;
  13.     friend class ShiftLeftCommand;
  14.     friend class ShiftRightCommand;
  15.     friend class BackspaceCommand;
  16. public:
  17.     const std::string &GetText() const;
  18.     void Type(char c);
  19.     void ShiftLeft();
  20.     void ShiftRight();
  21.     void Backspace();
  22.     void Undo();
  23.     void Redo();
  24.  
  25. private:
  26.     void AddCommand(std::unique_ptr<ICommand> command) {
  27.         commands_.push_back(command);
  28.     }
  29.     std::vector<std::unique_ptr<ICommand>> commands_;
  30.     std::vector<std::unique_ptr<ICommand>> undone_;
  31.     std::unique_ptr<ICommand> last_ = nullptr;
  32.     std::string text_;
  33.     size_t coursor_ = 0;
  34. };
  35.  
  36. class TypeCommand : public ICommand {
  37.     friend class Editor;
  38. public:
  39.     TypeCommand(Editor& editor, const char c) : editor_(editor), c_(c) {
  40.     }
  41.     void Do() {
  42.         editor_.text_.insert(editor_.coursor_++, 1, c_);
  43.     }
  44.     void Undo() {
  45.         editor_.text_.erase(editor_.coursor_ - 1, 1);
  46.         editor_.coursor_--;
  47.     }
  48. private:
  49.     Editor& editor_;
  50.     char c_;
  51. };
  52.  
  53. class ShiftLeftCommand : public ICommand {
  54.     friend class Editor;
  55. public:
  56.     ShiftLeftCommand(Editor& editor) : editor_(editor) {
  57.     }
  58.     void Do() {
  59.         editor_.coursor_--;
  60.     }
  61.     void Undo() {
  62.         editor_.coursor_++;
  63.     }
  64. private:
  65.     Editor& editor_;
  66. };
  67.  
  68. class ShiftRightCommand : public ICommand {
  69.     friend class Editor;
  70. public:
  71.     ShiftRightCommand(Editor& editor) : editor_(editor) {
  72.     }
  73.     void Do() {
  74.         editor_.coursor_++;
  75.     }
  76.     void Undo() {
  77.         editor_.coursor_--;
  78.     }
  79. private:
  80.     Editor& editor_;
  81.     std::string text_;
  82. };
  83.  
  84. class BackspaceCommand : public ICommand {
  85.     friend class Editor;
  86. public:
  87.     BackspaceCommand(Editor& editor) : editor_(editor) {
  88.     }
  89.     void Do() {
  90.         c_ = editor_.text_[editor_.coursor_ - 1];
  91.         editor_.text_.erase(editor_.coursor_ - 1, 1);
  92.         editor_.coursor_--;
  93.     }
  94.     void Undo() {
  95.         editor_.text_.insert(editor_.coursor_++, 1, c_);
  96.     }
  97. private:
  98.     Editor& editor_;
  99.     char c_;
  100. };
  101.  
  102.  
  103. const std::string& Editor::GetText() const {
  104.     return text_;
  105. }
  106.  
  107. void Editor::Type(char c) {
  108.     AddCommand(std::make_unique<TypeCommand>(*this, c));
  109.     commands_.back()->Do();
  110.     if (!undone_.empty()) {
  111.         undone_.clear();
  112.     }
  113. }
  114.  
  115. void Editor::ShiftLeft() {
  116.     if (coursor_ > 0) {
  117.         AddCommand(std::make_unique<ShiftLeftCommand>(*this));
  118.         commands_.back()->Do();
  119.         if (!undone_.empty()) {
  120.             undone_.clear();
  121.         }
  122.     }
  123. }
  124.  
  125. void Editor::ShiftRight() {
  126.     if (coursor_ < text_.size()) {
  127.         AddCommand(std::make_unique<ShiftRightCommand>(*this));
  128.         commands_.back()->Do();
  129.         if (!undone_.empty()) {
  130.             undone_.clear();
  131.         }
  132.     }
  133. }
  134.  
  135. void Editor::Backspace() {
  136.     if (coursor_ > 0) {
  137.         AddCommand(std::make_unique<BackspaceCommand>(*this));
  138.         commands_.back()->Do();
  139.         if (!undone_.empty()) {
  140.             undone_.clear();
  141.         }
  142.     }
  143. }
  144.  
  145. void Editor::Undo() {
  146.     if (commands_.empty()) {
  147.         return;
  148.     }
  149.     commands_.back()->Undo();
  150.     undone_.push_back(std::move(commands_.back()));
  151.     commands_.pop_back();
  152. }
  153.  
  154. void Editor::Redo() {
  155.     if (undone_.empty()) {
  156.         return;
  157.     }
  158.     undone_.back()->Do();
  159.     commands_.push_back(std::move(undone_.back()));
  160.     undone_.pop_back();
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement