Command pattern started

Added virtual base class and first implementation.
This commit is contained in:
Luís Murta 2023-07-31 21:33:35 +01:00
parent 54895a1152
commit 65005e9ba7
Signed by: satprog
GPG Key ID: 169EF1BBD7049F94
4 changed files with 52 additions and 0 deletions

View File

@ -1,4 +1,5 @@
# other features
set(CMAKE_CXX_CLANG_TIDY clang-tidy)
add_subdirectory(common)
add_subdirectory(cli)

View File

@ -0,0 +1,8 @@
add_library(common INTERFACE)
target_include_directories(common INTERFACE include)
add_executable(common_test src/command.test.cpp)
target_link_libraries(common_test common gtest gmock gtest_main)
include(GoogleTest)
gtest_discover_tests(common_test)

View File

@ -0,0 +1,27 @@
/// \file
/// \copyright SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include <iostream>
#include <string_view>
namespace command {
class Command {
public:
virtual ~Command() = default;
virtual void execute() const = 0;
};
class PrintCommand : public Command {
public:
explicit PrintCommand(std::string_view sv) : message{std::move(sv)} {}
void execute() const override { std::cout << message << std::endl; }
private:
std::string_view message;
};
} // namespace command

View File

@ -0,0 +1,16 @@
/// \copyright SPDX-License-Identifier: GPL-3.0-or-later
#include "command.hpp"
#include <gtest/gtest.h>
namespace command {
TEST(CommandTest, Initialization) {
const PrintCommand command{"Hi!"};
auto execute = [](const command::Command& cmd) { cmd.execute(); };
execute(command);
}
} // namespace command