From 21440daf27d3b0d58b4044aba83f39d96c0d138c Mon Sep 17 00:00:00 2001 From: Jan Date: Mon, 8 Feb 2021 18:02:27 +0100 Subject: [PATCH] Change ZCG cpp to be able to handle multiple tasks --- .../ZoneCodeGenerator.cpp | 25 +++++++------- .../ZoneCodeGeneratorArguments.cpp | 33 +++++++++++-------- .../ZoneCodeGeneratorArguments.h | 14 ++++---- 3 files changed, 39 insertions(+), 33 deletions(-) diff --git a/src/ZoneCodeGeneratorNew/ZoneCodeGenerator.cpp b/src/ZoneCodeGeneratorNew/ZoneCodeGenerator.cpp index 9a48ccf2..5d69e27d 100644 --- a/src/ZoneCodeGeneratorNew/ZoneCodeGenerator.cpp +++ b/src/ZoneCodeGeneratorNew/ZoneCodeGenerator.cpp @@ -45,8 +45,8 @@ class ZoneCodeGenerator::Impl void PrintData() const { - PrettyPrinter prettyPrinter; - prettyPrinter.Print(m_repository.get()); + const PrettyPrinter prettyPrinter(std::cout, m_repository.get()); + prettyPrinter.PrintAll(); } bool GenerateCode() @@ -73,20 +73,19 @@ public: if (!ReadHeaderData() || !ReadCommandsData()) return 1; - - switch(m_args.m_task) + + if(m_args.ShouldPrint()) { - case ZoneCodeGeneratorArguments::ProcessingTask::PRINT_DATA: PrintData(); - return 0; - - case ZoneCodeGeneratorArguments::ProcessingTask::GENERATE_CODE: - return GenerateCode() ? 0 : 1; - - default: - std::cout << "Unknown task: " << static_cast(m_args.m_task) << std::endl; - return 2; } + + if(m_args.ShouldGenerate()) + { + if (!GenerateCode()) + return 1; + } + + return 0; } }; diff --git a/src/ZoneCodeGeneratorNew/ZoneCodeGeneratorArguments.cpp b/src/ZoneCodeGeneratorNew/ZoneCodeGeneratorArguments.cpp index 1b1e3f16..bc76abe9 100644 --- a/src/ZoneCodeGeneratorNew/ZoneCodeGeneratorArguments.cpp +++ b/src/ZoneCodeGeneratorNew/ZoneCodeGeneratorArguments.cpp @@ -94,10 +94,10 @@ ZoneCodeGeneratorArguments::GenerationTask::GenerationTask(std::string assetName } ZoneCodeGeneratorArguments::ZoneCodeGeneratorArguments() - : m_argument_parser(COMMAND_LINE_OPTIONS, _countof(COMMAND_LINE_OPTIONS)) + : m_argument_parser(COMMAND_LINE_OPTIONS, _countof(COMMAND_LINE_OPTIONS)), + m_task_flags(0) { m_verbose = false; - m_task = ProcessingTask::GENERATE_CODE; } void ZoneCodeGeneratorArguments::PrintUsage() @@ -132,7 +132,7 @@ bool ZoneCodeGeneratorArguments::Parse(const int argc, const char** argv) // -p; --print if (m_argument_parser.IsOptionSpecified(OPTION_PRINT)) - m_task = ProcessingTask::PRINT_DATA; + m_task_flags |= FLAG_TASK_PRINT; // -o; --output if (m_argument_parser.IsOptionSpecified(OPTION_OUTPUT_FOLDER)) @@ -164,25 +164,30 @@ bool ZoneCodeGeneratorArguments::Parse(const int argc, const char** argv) return false; } - if (m_task == ProcessingTask::GENERATE_CODE) + if (m_argument_parser.IsOptionSpecified(OPTION_GENERATE)) { - if (!m_argument_parser.IsOptionSpecified(OPTION_GENERATE)) - { - std::cout << "A generate parameter needs to be specified when generating code" << std::endl; - PrintUsage(); - return false; - } - + m_task_flags |= FLAG_TASK_GENERATE; const auto generateParameterValues = m_argument_parser.GetParametersForOption(OPTION_GENERATE); - for (auto i = 0u; i < generateParameterValues.size(); i+=2) + for (auto i = 0u; i < generateParameterValues.size(); i += 2) m_generation_tasks.emplace_back(generateParameterValues[i], generateParameterValues[i + 1]); } - else if (m_argument_parser.IsOptionSpecified(OPTION_GENERATE)) + + if (m_task_flags == 0) { - std::cout << "Cannot specify generate parameter when not generating code" << std::endl; + std::cout << "There was no output task specified." << std::endl; PrintUsage(); return false; } return true; } + +bool ZoneCodeGeneratorArguments::ShouldGenerate() const +{ + return m_task_flags & FLAG_TASK_GENERATE; +} + +bool ZoneCodeGeneratorArguments::ShouldPrint() const +{ + return m_task_flags & FLAG_TASK_PRINT; +} diff --git a/src/ZoneCodeGeneratorNew/ZoneCodeGeneratorArguments.h b/src/ZoneCodeGeneratorNew/ZoneCodeGeneratorArguments.h index fd0e7dbf..302879de 100644 --- a/src/ZoneCodeGeneratorNew/ZoneCodeGeneratorArguments.h +++ b/src/ZoneCodeGeneratorNew/ZoneCodeGeneratorArguments.h @@ -1,4 +1,6 @@ #pragma once + +#include "Utils/ClassUtils.h" #include "Utils/Arguments/ArgumentParser.h" #include @@ -13,11 +15,8 @@ class ZoneCodeGeneratorArguments static void PrintUsage(); public: - enum class ProcessingTask - { - GENERATE_CODE, - PRINT_DATA - }; + static constexpr unsigned FLAG_TASK_GENERATE = 1 << 0; + static constexpr unsigned FLAG_TASK_PRINT = 1 << 1; class GenerationTask { @@ -35,10 +34,13 @@ public: std::vector m_command_paths; std::string m_output_directory; - ProcessingTask m_task; + unsigned m_task_flags; std::vector m_generation_tasks; ZoneCodeGeneratorArguments(); bool Parse(int argc, const char** argv); + + _NODISCARD bool ShouldGenerate() const; + _NODISCARD bool ShouldPrint() const; };