Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,41 @@ int main()
}
```

### `cmd::CommandHistory` example

```cpp
import std;
import cmd;

imt main()
{
cmd::CommandHistory history;
cmd::Context<int> ctx;

try
{
history.Execute(std::make_unique<cmd::Insert<int>>(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
Expand Down
2 changes: 1 addition & 1 deletion command/src/CommandInsert.cppm
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export namespace cmd
public:
Insert(Context<ContextT> &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");
}
Expand Down
39 changes: 21 additions & 18 deletions command/src/ICommand.cppm
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
module cmd:ICommand;
export module cmd:ICommand;

import std;

template <typename ContextT>
struct Context
export namespace cmd
{
std::vector<ContextT> data;
};
template <typename ContextT>
struct Context
{
std::vector<ContextT> 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;
};
};
3 changes: 1 addition & 2 deletions command/src/cmd.cppm
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
export module cmd;

import :ICommand;

export import :ICommand;
export import :CommandClear;
export import :CommandErase;
export import :CommandHistory;
Expand Down
Loading