Skip to content
Open
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
49 changes: 49 additions & 0 deletions CommandEngine/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
cmake_minimum_required(VERSION 3.10)
project(CommandEngine)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

if(WIN32)
add_definitions(-D_WIN32_WINNT=0x0601)
add_definitions(-DNOMINMAX)
endif()

# Убираем -Werror и уменьшаем строгость предупреждений
if(MSVC)
add_compile_options(/W4 /EHsc) # Убрали /WX (трактовать предупреждения как ошибки)
else()
add_compile_options(-Wall -Wextra -pedantic) # Убрали -Werror
endif()

add_executable(CommandEngine
CommandEngine.cpp
TestFunctions.cpp
)

set_source_files_properties(
CommandEngine.cpp
TestFunctions.cpp
PROPERTIES LANGUAGE CXX
)

target_include_directories(CommandEngine PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})

if(WIN32)
target_compile_definitions(CommandEngine PRIVATE _CRT_SECURE_NO_WARNINGS)
endif()

# Настройка для отладки
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
if(MSVC)
target_compile_options(CommandEngine PRIVATE /Zi /Od)
else()
target_compile_options(CommandEngine PRIVATE -g -O0)
endif()
else()
if(MSVC)
target_compile_options(CommandEngine PRIVATE /O2)
else()
target_compile_options(CommandEngine PRIVATE -O3)
endif()
endif()
17 changes: 17 additions & 0 deletions CommandEngine/CommandEngine.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "TestFunctions.hpp"
#include "locale.h"

int main() {
setlocale(LC_ALL, "Rus");
try {
runAssignmentTest();
runAllTypeTests();
testDefaultArguments();

return 0;
}
catch (const std::exception& e) {
std::cerr << "\nОшибка: " << e.what() << std::endl;
return 1;
}
}
146 changes: 146 additions & 0 deletions CommandEngine/CommandEngine.vcxproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>17.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{ca454cd1-3686-4938-91ba-6330fcaf4ce2}</ProjectGuid>
<RootNamespace>CommandEngine</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="CommandEngine.cpp" />
<ClCompile Include="Engine.hpp" />
<ClCompile Include="TestFunctions.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="InterfaceCommand.hpp" />
<ClInclude Include="TestFunctions.hpp" />
<ClInclude Include="Wrapper.hpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
39 changes: 39 additions & 0 deletions CommandEngine/CommandEngine.vcxproj.filters
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Исходные файлы">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Файлы заголовков">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
<Filter Include="Файлы ресурсов">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="CommandEngine.cpp">
<Filter>Исходные файлы</Filter>
</ClCompile>
<ClCompile Include="Engine.hpp">
<Filter>Файлы заголовков</Filter>
</ClCompile>
<ClCompile Include="TestFunctions.cpp">
<Filter>Исходные файлы</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="InterfaceCommand.hpp">
<Filter>Файлы заголовков</Filter>
</ClInclude>
<ClInclude Include="Wrapper.hpp">
<Filter>Файлы заголовков</Filter>
</ClInclude>
<ClInclude Include="TestFunctions.hpp">
<Filter>Файлы заголовков</Filter>
</ClInclude>
</ItemGroup>
</Project>
54 changes: 54 additions & 0 deletions CommandEngine/Engine.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#pragma once
#ifndef ENGINE_HPP
#define ENGINE_HPP

#include "InterfaceCommand.hpp"
#include <unordered_map>
#include <mutex>
#include <stdexcept>
#include <string>

class Engine {
public:
Engine() = default;
void register_command(InterfaceCommand* cmd, const std::string& name) {

if (name.empty()) {
throw std::invalid_argument("Command name cannot be empty");
}

if (commands.find(name) != commands.end()) {
throw std::runtime_error("Command already registered: " + name);
}

commands[name] = cmd;
}

CommandResult execute(const std::string& name, const ArgList& args = {}) {

auto it = commands.find(name);
if (it == commands.end()) {
throw std::runtime_error("Command not found: " + name);
}

try {
return it->second->execute(args);
}
catch (const std::exception& e) {
throw std::runtime_error("Error executing command '" + name + "': " + e.what());
}
}

bool has_command(const std::string& name) const {
return commands.find(name) != commands.end();
}

void clear() {
commands.clear();
}

private:
std::unordered_map<std::string, InterfaceCommand*> commands;
};

#endif
35 changes: 35 additions & 0 deletions CommandEngine/InterfaceCommand.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#pragma once
#ifndef INTERFACECOMMAND_HPP
#define INTERFACECOMMAND_HPP

#include <string>
#include <vector>
#include <variant>
#include <iostream>
#include <map>

struct VoidResult {};
using ArgValue = std::variant<int, double, std::string, bool>;
using ArgList = std::vector<std::pair<std::string, std::string>>;
using CommandResult = std::variant<VoidResult, ArgValue>;

class InterfaceCommand {
public:
virtual ~InterfaceCommand() = default;
virtual CommandResult execute(const ArgList& args = {}) = 0;
};

inline std::ostream& operator<<(std::ostream& os, const CommandResult& result) {
if (std::holds_alternative<VoidResult>(result)) {
os << "void";
}
else {
const auto& value = std::get<ArgValue>(result);
std::visit([&os](auto&& arg) {
os << arg;
}, value);
}
return os;
}

#endif
Loading