Command pattern started
Added virtual base class and first implementation.
This commit is contained in:
parent
54895a1152
commit
65005e9ba7
@ -1,4 +1,5 @@
|
||||
# other features
|
||||
set(CMAKE_CXX_CLANG_TIDY clang-tidy)
|
||||
|
||||
add_subdirectory(common)
|
||||
add_subdirectory(cli)
|
||||
|
||||
8
libs/common/CMakeLists.txt
Normal file
8
libs/common/CMakeLists.txt
Normal 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)
|
||||
27
libs/common/include/command.hpp
Normal file
27
libs/common/include/command.hpp
Normal 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
|
||||
16
libs/common/src/command.test.cpp
Normal file
16
libs/common/src/command.test.cpp
Normal 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
|
||||
Loading…
x
Reference in New Issue
Block a user