Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <memory>
- #include <string>
- class ICommand {
- public:
- virtual void Do() = 0;
- virtual void Undo() = 0;
- virtual ~ICommand() = default;
- };
- class Editor {
- friend class TypeCommand;
- friend class ShiftLeftCommand;
- friend class ShiftRightCommand;
- friend class BackspaceCommand;
- public:
- const std::string &GetText() const;
- void Type(char c);
- void ShiftLeft();
- void ShiftRight();
- void Backspace();
- void Undo();
- void Redo();
- private:
- void AddCommand(std::unique_ptr<ICommand> command) {
- commands_.push_back(command);
- }
- std::vector<std::unique_ptr<ICommand>> commands_;
- std::vector<std::unique_ptr<ICommand>> undone_;
- std::unique_ptr<ICommand> last_ = nullptr;
- std::string text_;
- size_t coursor_ = 0;
- };
- class TypeCommand : public ICommand {
- friend class Editor;
- public:
- TypeCommand(Editor& editor, const char c) : editor_(editor), c_(c) {
- }
- void Do() {
- editor_.text_.insert(editor_.coursor_++, 1, c_);
- }
- void Undo() {
- editor_.text_.erase(editor_.coursor_ - 1, 1);
- editor_.coursor_--;
- }
- private:
- Editor& editor_;
- char c_;
- };
- class ShiftLeftCommand : public ICommand {
- friend class Editor;
- public:
- ShiftLeftCommand(Editor& editor) : editor_(editor) {
- }
- void Do() {
- editor_.coursor_--;
- }
- void Undo() {
- editor_.coursor_++;
- }
- private:
- Editor& editor_;
- };
- class ShiftRightCommand : public ICommand {
- friend class Editor;
- public:
- ShiftRightCommand(Editor& editor) : editor_(editor) {
- }
- void Do() {
- editor_.coursor_++;
- }
- void Undo() {
- editor_.coursor_--;
- }
- private:
- Editor& editor_;
- std::string text_;
- };
- class BackspaceCommand : public ICommand {
- friend class Editor;
- public:
- BackspaceCommand(Editor& editor) : editor_(editor) {
- }
- void Do() {
- c_ = editor_.text_[editor_.coursor_ - 1];
- editor_.text_.erase(editor_.coursor_ - 1, 1);
- editor_.coursor_--;
- }
- void Undo() {
- editor_.text_.insert(editor_.coursor_++, 1, c_);
- }
- private:
- Editor& editor_;
- char c_;
- };
- const std::string& Editor::GetText() const {
- return text_;
- }
- void Editor::Type(char c) {
- AddCommand(std::make_unique<TypeCommand>(*this, c));
- commands_.back()->Do();
- if (!undone_.empty()) {
- undone_.clear();
- }
- }
- void Editor::ShiftLeft() {
- if (coursor_ > 0) {
- AddCommand(std::make_unique<ShiftLeftCommand>(*this));
- commands_.back()->Do();
- if (!undone_.empty()) {
- undone_.clear();
- }
- }
- }
- void Editor::ShiftRight() {
- if (coursor_ < text_.size()) {
- AddCommand(std::make_unique<ShiftRightCommand>(*this));
- commands_.back()->Do();
- if (!undone_.empty()) {
- undone_.clear();
- }
- }
- }
- void Editor::Backspace() {
- if (coursor_ > 0) {
- AddCommand(std::make_unique<BackspaceCommand>(*this));
- commands_.back()->Do();
- if (!undone_.empty()) {
- undone_.clear();
- }
- }
- }
- void Editor::Undo() {
- if (commands_.empty()) {
- return;
- }
- commands_.back()->Undo();
- undone_.push_back(std::move(commands_.back()));
- commands_.pop_back();
- }
- void Editor::Redo() {
- if (undone_.empty()) {
- return;
- }
- undone_.back()->Do();
- commands_.push_back(std::move(undone_.back()));
- undone_.pop_back();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement