From 0a5bbd016190674d234c755c55fb8eb0872432f6 Mon Sep 17 00:00:00 2001 From: whaffman Date: Mon, 18 Aug 2025 16:44:33 +0200 Subject: [PATCH] Implement Intern class with form creation logic and enhance AForm destructor --- ex03/compile_commands.json | 121 +++++++++++++++++++++++++++++++++++++ ex03/inc/AForm.hpp | 2 +- ex03/inc/Intern.hpp | 21 +++++++ ex03/inc/colors.h | 2 + ex03/src/Intern.cpp | 68 +++++++++++++++++++++ ex03/src/main.cpp | 80 ++++++------------------ 6 files changed, 232 insertions(+), 62 deletions(-) create mode 100644 ex03/compile_commands.json create mode 100644 ex03/inc/Intern.hpp create mode 100644 ex03/src/Intern.cpp diff --git a/ex03/compile_commands.json b/ex03/compile_commands.json new file mode 100644 index 0000000..3869a4a --- /dev/null +++ b/ex03/compile_commands.json @@ -0,0 +1,121 @@ +[ + { + "arguments": [ + "/usr/bin/c++", + "-Wall", + "-Wextra", + "-Werror", + "-std=c++11", + "-I./inc", + "-c", + "-o", + "AForm.o", + "src/AForm.cpp" + ], + "directory": "/home/willem/projects/CPP05/ex03", + "file": "/home/willem/projects/CPP05/ex03/src/AForm.cpp", + "output": "/home/willem/projects/CPP05/ex03/AForm.o" + }, + { + "arguments": [ + "/usr/bin/c++", + "-Wall", + "-Wextra", + "-Werror", + "-std=c++11", + "-I./inc", + "-c", + "-o", + "Bureaucrat.o", + "src/Bureaucrat.cpp" + ], + "directory": "/home/willem/projects/CPP05/ex03", + "file": "/home/willem/projects/CPP05/ex03/src/Bureaucrat.cpp", + "output": "/home/willem/projects/CPP05/ex03/Bureaucrat.o" + }, + { + "arguments": [ + "/usr/bin/c++", + "-Wall", + "-Wextra", + "-Werror", + "-std=c++11", + "-I./inc", + "-c", + "-o", + "Intern.o", + "src/Intern.cpp" + ], + "directory": "/home/willem/projects/CPP05/ex03", + "file": "/home/willem/projects/CPP05/ex03/src/Intern.cpp", + "output": "/home/willem/projects/CPP05/ex03/Intern.o" + }, + { + "arguments": [ + "/usr/bin/c++", + "-Wall", + "-Wextra", + "-Werror", + "-std=c++11", + "-I./inc", + "-c", + "-o", + "main.o", + "src/main.cpp" + ], + "directory": "/home/willem/projects/CPP05/ex03", + "file": "/home/willem/projects/CPP05/ex03/src/main.cpp", + "output": "/home/willem/projects/CPP05/ex03/main.o" + }, + { + "arguments": [ + "/usr/bin/c++", + "-Wall", + "-Wextra", + "-Werror", + "-std=c++11", + "-I./inc", + "-c", + "-o", + "PresidentialPardonForm.o", + "src/PresidentialPardonForm.cpp" + ], + "directory": "/home/willem/projects/CPP05/ex03", + "file": "/home/willem/projects/CPP05/ex03/src/PresidentialPardonForm.cpp", + "output": "/home/willem/projects/CPP05/ex03/PresidentialPardonForm.o" + }, + { + "arguments": [ + "/usr/bin/c++", + "-Wall", + "-Wextra", + "-Werror", + "-std=c++11", + "-I./inc", + "-c", + "-o", + "RobotomyRequestForm.o", + "src/RobotomyRequestForm.cpp" + ], + "directory": "/home/willem/projects/CPP05/ex03", + "file": "/home/willem/projects/CPP05/ex03/src/RobotomyRequestForm.cpp", + "output": "/home/willem/projects/CPP05/ex03/RobotomyRequestForm.o" + }, + { + "arguments": [ + "/usr/bin/c++", + "-Wall", + "-Wextra", + "-Werror", + "-std=c++11", + "-I./inc", + "-c", + "-o", + "ShrubberyCreationForm.o", + "src/ShrubberyCreationForm.cpp" + ], + "directory": "/home/willem/projects/CPP05/ex03", + "file": "/home/willem/projects/CPP05/ex03/src/ShrubberyCreationForm.cpp", + "output": "/home/willem/projects/CPP05/ex03/ShrubberyCreationForm.o" + } +] diff --git a/ex03/inc/AForm.hpp b/ex03/inc/AForm.hpp index 0cee4f7..811c77c 100644 --- a/ex03/inc/AForm.hpp +++ b/ex03/inc/AForm.hpp @@ -18,7 +18,7 @@ class AForm AForm() = delete; AForm(std::string name, int signGrade, int executeGrade); AForm(const AForm &other); - ~AForm(); + virtual ~AForm(); AForm &operator=(const AForm &other) = delete; const std::string &getName() const; bool getIsSigned() const; diff --git a/ex03/inc/Intern.hpp b/ex03/inc/Intern.hpp new file mode 100644 index 0000000..986babd --- /dev/null +++ b/ex03/inc/Intern.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include + +#include "AForm.hpp" + +class Intern +{ + public: + Intern(); + Intern(const Intern &other); + Intern &operator=(const Intern &other); + ~Intern(); + + AForm *makeForm(const std::string &formName, const std::string &target); + + private: + static AForm *createShrubberyForm(const std::string &target); + static AForm *createRobotomyForm(const std::string &target); + static AForm *createPresidentialForm(const std::string &target); +}; diff --git a/ex03/inc/colors.h b/ex03/inc/colors.h index ba43697..37189b0 100644 --- a/ex03/inc/colors.h +++ b/ex03/inc/colors.h @@ -2,6 +2,7 @@ #define COLORS_H #define BUREAUCRAT BOLD BACKGROUND4 " Bureaucrat: " RESET " " +#define INTERN BOLD BACKGROUND6 " Intern: " RESET " " #define AFORM BOLD BACKGROUND5 " AForm: " RESET " " #define SHRUBBERY_CREATION_FORM BOLD BACKGROUND10 " ShrubberyCreationForm: " RESET " " #define ROBOTOMY_REQUEST_FORM BOLD BACKGROUND11 " RobotomyRequestForm: " RESET " " @@ -9,6 +10,7 @@ #define CONSTRUCTOR "Constructor called" #define PARAMETERIZED_CONSTRUCTOR "Parameterized constructor called" +#define ASSIGNMENT_OPERATOR "Assignment operator called" #define DESTRUCTOR "Destructor called" #define COPY_CONSTRUCTOR "Copy constructor called" diff --git a/ex03/src/Intern.cpp b/ex03/src/Intern.cpp new file mode 100644 index 0000000..f2bb967 --- /dev/null +++ b/ex03/src/Intern.cpp @@ -0,0 +1,68 @@ +#include "Intern.hpp" + +#include + +#include "AForm.hpp" +#include "PresidentialPardonForm.hpp" +#include "RobotomyRequestForm.hpp" +#include "ShrubberyCreationForm.hpp" + +#include "colors.h" + +Intern::Intern() +{ + std::cout << INTERN CONSTRUCTOR << std::endl; +} + +Intern::Intern(const Intern &other) +{ + (void)other; + std::cout << INTERN COPY_CONSTRUCTOR << std::endl; +} + +Intern &Intern::operator=(const Intern &other) +{ + if (this != &other) + { + std::cout << INTERN ASSIGNMENT_OPERATOR << std::endl; + } + return *this; +} + +Intern::~Intern() +{ + std::cout << INTERN DESTRUCTOR << std::endl; +} + +AForm *Intern::createShrubberyForm(const std::string &target) +{ + return new ShrubberyCreationForm(target); +} + +AForm *Intern::createRobotomyForm(const std::string &target) +{ + return new RobotomyRequestForm(target); +} + +AForm *Intern::createPresidentialForm(const std::string &target) +{ + return new PresidentialPardonForm(target); +} + +AForm *Intern::makeForm(const std::string &formName, const std::string &target) +{ + std::string formTypeStrs[3] = {"shrubbery creation", "robotomy request", + "presidential pardon"}; + // Array of factory functions + AForm *(*formFactories[3])(const std::string &) = { + Intern::createShrubberyForm, Intern::createRobotomyForm, + Intern::createPresidentialForm}; + + for (int i = 0; i < 3; ++i) + { + if (formName == formTypeStrs[i]) + return formFactories[i](target); + } + std::cout << "Unknown form type: " << formName << std::endl; + return nullptr; +} diff --git a/ex03/src/main.cpp b/ex03/src/main.cpp index 7d92e70..074498a 100644 --- a/ex03/src/main.cpp +++ b/ex03/src/main.cpp @@ -2,77 +2,35 @@ #include #include "Bureaucrat.hpp" -#include "PresidentialPardonForm.hpp" -#include "RobotomyRequestForm.hpp" -#include "ShrubberyCreationForm.hpp" +#include "Intern.hpp" +#include "AForm.hpp" + #include "colors.h" -void test(Bureaucrat &bureaucrat) -{ - try - { - std::cout << INFO << "Testing Bureaucrat: " << bureaucrat.getName() - << ", Grade: " << bureaucrat.getGrade() << std::endl; - std::cout << PRINT << bureaucrat << "\n" << std::endl; - std::cout << INFO << "Creating ShrubberyCreationForm" << std::endl; - ShrubberyCreationForm shrubberyForm("Garden"); - std::cout << PRINT << shrubberyForm << "\n" << std::endl; - - std::cout << INFO << "Signing ShrubberyCreationForm" << std::endl; - bureaucrat.signForm(shrubberyForm); - std::cout << PRINT << shrubberyForm << "\n" << std::endl; - - std::cout << INFO << "Executing ShrubberyCreationForm" << std::endl; - shrubberyForm.execute(bureaucrat); - - std::cout << INFO << "Creating RobotomyRequestForm" << std::endl; - RobotomyRequestForm robotomyForm("Alice"); - std::cout << PRINT << robotomyForm << "\n" << std::endl; - - std::cout << INFO << "Signing RobotomyRequestForm" << std::endl; - bureaucrat.signForm(robotomyForm); - std::cout << PRINT << robotomyForm << "\n" << std::endl; - - std::cout << INFO << "Executing RobotomyRequestForm" << std::endl; - robotomyForm.execute(bureaucrat); - std::cout << "\n" << std::endl; - - std::cout << INFO << "Creating PresidentialPardonForm" << std::endl; - PresidentialPardonForm pardonForm("Bob"); - std::cout << PRINT << pardonForm << "\n" << std::endl; - - std::cout << INFO << "Signing PresidentialPardonForm" << std::endl; - bureaucrat.signForm(pardonForm); - std::cout << PRINT << pardonForm << "\n" << std::endl; - - std::cout << INFO << "Executing PresidentialPardonForm" << std::endl; - pardonForm.execute(bureaucrat); - std::cout << "\n" << END_SCOPE << std::endl; - } - catch (const std::exception &e) - { - std::cerr << EXCEPTION << e.what() << std::endl; - } -} int main(void) { - for (int i = 0; i < 250; i += 10) + Intern intern; + std::string forms[] = {"shrubbery creation", "robotomy request", "presidential pardon", "unknown"}; + Bureaucrat bureaucrat("Alice", 50); + for (const std::string &formName : forms) { - std::cout << INFO << "Testing Bureaucrat with grade: " << i << std::endl; - try + std::cout << INFO << "Creating form: " << formName << std::endl; + AForm *form = intern.makeForm(formName, "Target"); + if (form) { - Bureaucrat bureaucrat("John", i); - test(bureaucrat); - std::cout << END_SCOPE << std::endl; - } - catch (const std::exception &e) - { - std::cerr << EXCEPTION << e.what() << std::endl; + std::cout << "Created form: " << *form << std::endl; + try { + bureaucrat.signForm(*form); + form->execute(bureaucrat); + + } catch (const std::exception &e) { + std::cerr << EXCEPTION << e.what() << std::endl; + } + delete form; } std::cout << std::endl; - } return 0;