From b917e990d87ff52c7b514b3a44f55655b201d023 Mon Sep 17 00:00:00 2001 From: philippkuest <100705472+philippkuest@users.noreply.github.com> Date: Sun, 24 Jul 2022 09:21:09 +0000 Subject: [PATCH 01/11] Feature/i93 add example program (#112) * Added example code * changes * Added checks for exclusion, arguments and optional * Added doxygen comments * Changed attributes of ExampleCode to be more abstract * printVersion is overridden in ExtendedGeneratedCode.h * minor * minor * minor * doxygen * Added new line to version * Renamed getter for generatedClass * Fixed where code logic is places Co-authored-by: Tobias Goetz --- CMakeLists.txt | 6 ++ src/SourceCodeWriter.cpp | 2 +- src3/ExampleProgram.cpp | 85 +++++++++++++++++++++ src3/ExampleProgram.h | 20 +++++ src3/ExtendedGeneratedCode.cpp | 5 ++ src3/ExtendedGeneratedCode.h | 18 +++++ {exampleProgram => src3}/exampleProgram.xml | 10 +-- 7 files changed, 139 insertions(+), 7 deletions(-) create mode 100644 src3/ExampleProgram.cpp create mode 100644 src3/ExampleProgram.h create mode 100644 src3/ExtendedGeneratedCode.cpp create mode 100644 src3/ExtendedGeneratedCode.h rename {exampleProgram => src3}/exampleProgram.xml (68%) diff --git a/CMakeLists.txt b/CMakeLists.txt index fdfc5cc..4ca7f5c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,6 +17,12 @@ add_executable( ${Source_Files2} ) +file(GLOB Source_Files2 src3/*.cpp) +add_executable( + ExampleProgram + ${Source_Files2} +) + find_package (Boost REQUIRED) diff --git a/src/SourceCodeWriter.cpp b/src/SourceCodeWriter.cpp index f2ba625..0466f93 100644 --- a/src/SourceCodeWriter.cpp +++ b/src/SourceCodeWriter.cpp @@ -499,7 +499,7 @@ void SourceCodeWriter::createHeaderPrintVersion() { } void SourceCodeWriter::createSourcePrintVersion() { - fprintf(getSourceFile(), "void %s::printVersion(){\nprintf(\"version: 1.0.0\");\n}\n", + fprintf(getSourceFile(), "void %s::printVersion(){\nprintf(\"version: 1.0.0\\n\");\n}\n", getGetOptSetup()->getClassName().c_str()); } diff --git a/src3/ExampleProgram.cpp b/src3/ExampleProgram.cpp new file mode 100644 index 0000000..eb43d46 --- /dev/null +++ b/src3/ExampleProgram.cpp @@ -0,0 +1,85 @@ +#include "ExampleProgram.h" +#include "generatedCode.h" + +/** + * @brief Constructor, that generates a object of ExtendedGeneratedCode + */ +ExampleProgram::ExampleProgram(GC::GeneratedClass *generatedClass) { + this->generatedClass = generatedClass; +} + +/** + * @brief Getter for generatedClass + * @return object of class ExtendedGeneratedCode + */ +GC::GeneratedClass *ExampleProgram::getGeneratedClass() { + return generatedClass; +} + +/** + * @brief prints in command line if option 'version' was set + * @param ep object of class ExampleProgram + */ +void checkVersion(ExampleProgram ep){ + if(ep.getGeneratedClass()->isSetVersion()){ + //ep.getGeneratedClass()->printVersion(); + printf("The option 'version' was called --> isSetVersion=true\n"); + } else { + printf("The option 'version' was not called --> isSetVersion=false\n"); + } +} + +/** + * @brief prints in command line if option 'exclusion' was set + * @param ep object of class ExampleProgram + */ +void checkExclusion(ExampleProgram ep){ + if(ep.getGeneratedClass()->isSetExclusion()){ + printf("The option 'exclusion' was called --> isSetExclusion=true\n"); + } else { + printf("The option 'exclusion' was not called --> isSetExclusion=false\n"); + } +} + +/** + * @brief prints in command line if option 'arguments' was set and depending on it the argument + * @param ep object of class ExampleProgram + */ +void checkArguments(ExampleProgram ep){ + if(ep.getGeneratedClass()->isSetArguments()){ + printf("The option 'arguments' was called --> isSetExclusion=true\n"); + printf("The argument is: %d\n", ep.getGeneratedClass()->getValueOfArguments()); + } else { + printf("The option 'arguments' was not called --> isSetExclusion=false\n"); + } +} + +/** + * @brief prints in command line if option 'arguments' was set and depending on it the argument + * @param ep object of class ExampleProgram + */ +void checkOptional(ExampleProgram ep){ + if(ep.getGeneratedClass()->isSetOptional()){ + printf("The option 'optional' was called --> isSetOptional=true\n"); + printf("The argument is: %d\n", ep.getGeneratedClass()->getValueOfOptional()); + } else { + printf("The option 'optional' was not called --> isSetOptional=false\n"); + } +} + +/** + * @brief Generates object of class ExampleProgram, calls parseOptions and checks which options were set + * @param argc amount of arguments + * @param argv array of arguments + */ +int main(int argc, char* argv[] ) { + ExampleProgram exampleProgram = ExampleProgram(new ExtendedGeneratedCode()); + exampleProgram.getGeneratedClass()->parseOptions(argc, argv); + + checkVersion(exampleProgram); + checkExclusion(exampleProgram); + checkArguments(exampleProgram); + checkOptional(exampleProgram); + + return 0; +} \ No newline at end of file diff --git a/src3/ExampleProgram.h b/src3/ExampleProgram.h new file mode 100644 index 0000000..8013976 --- /dev/null +++ b/src3/ExampleProgram.h @@ -0,0 +1,20 @@ +#ifndef CODEGENERATOR_EXAMPLEPROGRAM_H +#define CODEGENERATOR_EXAMPLEPROGRAM_H + + +#include "ExtendedGeneratedCode.h" + +class ExampleProgram { +private: + GC::GeneratedClass *generatedClass = nullptr; +protected: +public: + //Constructor + explicit ExampleProgram(GC::GeneratedClass *generatedClass); + + //Getter + GC::GeneratedClass *getGeneratedClass(); +}; + + +#endif //CODEGENERATOR_EXAMPLEPROGRAM_H diff --git a/src3/ExtendedGeneratedCode.cpp b/src3/ExtendedGeneratedCode.cpp new file mode 100644 index 0000000..b99f03d --- /dev/null +++ b/src3/ExtendedGeneratedCode.cpp @@ -0,0 +1,5 @@ +#include "ExtendedGeneratedCode.h" + +void ExtendedGeneratedCode::printVersion() { + printf("Version: 2.0.0\n"); +} diff --git a/src3/ExtendedGeneratedCode.h b/src3/ExtendedGeneratedCode.h new file mode 100644 index 0000000..ee3231d --- /dev/null +++ b/src3/ExtendedGeneratedCode.h @@ -0,0 +1,18 @@ +#ifndef CODEGENERATOR_EXTENDEDGENERATEDCODE_H +#define CODEGENERATOR_EXTENDEDGENERATEDCODE_H + +#include "generatedCode.h" + +/** + * class with inheritance to generated class to use getopt of generated files + */ +class ExtendedGeneratedCode: public GC::GeneratedClass { + + /** + * @brief overrides printVersion function in generated Code + */ + void printVersion() override; +}; + + +#endif //CODEGENERATOR_EXTENDEDGENERATEDCODE_H diff --git a/exampleProgram/exampleProgram.xml b/src3/exampleProgram.xml similarity index 68% rename from exampleProgram/exampleProgram.xml rename to src3/exampleProgram.xml index a209984..7feafdf 100644 --- a/exampleProgram/exampleProgram.xml +++ b/src3/exampleProgram.xml @@ -4,12 +4,10 @@ generatedCode.h generatedCode.cpp GC - generatedClass + GeneratedClass - Erstellt einen Rumpf zum einlesen von Argumente aus der Kommandozeile. - Es kann sowohl mit innenliegenden Container wie externer Klassenanbindung eine Datenhaltung erfolgen. - Sobald ein Methodenaufruf abstrakt ist, wird die Basisklasse abstrakt. - Fuer die Formatierung der generierten Dateien wird astyle verwendet. + Gibt die verschiedenen Werte aus, welche von getOpt angenommen wurden. + Dies geschieht in der Kommandozeile. getoptgen -h @@ -18,7 +16,7 @@