diff --git a/README.md b/README.md index 1fdea57..df335c6 100644 --- a/README.md +++ b/README.md @@ -103,6 +103,41 @@ int main() } ``` +### `cmd::CommandHistory` example + +```cpp +import std; +import cmd; + +imt main() +{ + cmd::CommandHistory history; + cmd::Context ctx; + + try + { + history.Execute(std::make_unique>(ctx, 0, 10)); + } + catch(const std::out_of_range &e) + { + std::println("Error: {}", e.what()); + return 0; + } + + if (history.CanUndo()) + { + history.Undo(); + } + + if (history.CanRedo()) + { + history.Redo(); + } + + return 0; +} +``` + ## TODO ### Input diff --git a/command/src/CommandInsert.cppm b/command/src/CommandInsert.cppm index 629abdd..a770a72 100644 --- a/command/src/CommandInsert.cppm +++ b/command/src/CommandInsert.cppm @@ -17,7 +17,7 @@ export namespace cmd public: Insert(Context &ctx, std::size_t index, ContextT value) : _ctx(ctx), _index(index), _value(std::move(value)) { - if (index >= ctx.data.size()) + if ((index >= ctx.data.size()) && (_index != 0)) { throw std::out_of_range("cmd::Insert: index out of range"); } diff --git a/command/src/ICommand.cppm b/command/src/ICommand.cppm index bae0842..6b1223e 100644 --- a/command/src/ICommand.cppm +++ b/command/src/ICommand.cppm @@ -1,27 +1,30 @@ -module cmd:ICommand; +export module cmd:ICommand; import std; -template -struct Context +export namespace cmd { - std::vector data; -}; + template + struct Context + { + std::vector data; + }; -class ICommand -{ -protected: - ICommand() = default; + class ICommand + { + protected: + ICommand() = default; -public: - virtual ~ICommand() = default; + public: + virtual ~ICommand() = default; - ICommand(const ICommand &) = delete; - ICommand(ICommand &&) = delete; - ICommand &operator=(const ICommand &) = delete; - ICommand &operator=(ICommand &&) = delete; + ICommand(const ICommand &) = delete; + ICommand(ICommand &&) = delete; + ICommand &operator=(const ICommand &) = delete; + ICommand &operator=(ICommand &&) = delete; - virtual void Execute() = 0; - virtual void Undo() = 0; - [[nodiscard]] virtual std::string Description() const = 0; + virtual void Execute() = 0; + virtual void Undo() = 0; + [[nodiscard]] virtual std::string Description() const = 0; + }; }; \ No newline at end of file diff --git a/command/src/cmd.cppm b/command/src/cmd.cppm index 79f0f0a..6f1055f 100644 --- a/command/src/cmd.cppm +++ b/command/src/cmd.cppm @@ -1,7 +1,6 @@ export module cmd; -import :ICommand; - +export import :ICommand; export import :CommandClear; export import :CommandErase; export import :CommandHistory;