Implement Intern class with form creation logic and enhance AForm destructor
This commit is contained in:
parent
9ec113d630
commit
0a5bbd0161
121
ex03/compile_commands.json
Normal file
121
ex03/compile_commands.json
Normal file
@ -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"
|
||||||
|
}
|
||||||
|
]
|
||||||
@ -18,7 +18,7 @@ class AForm
|
|||||||
AForm() = delete;
|
AForm() = delete;
|
||||||
AForm(std::string name, int signGrade, int executeGrade);
|
AForm(std::string name, int signGrade, int executeGrade);
|
||||||
AForm(const AForm &other);
|
AForm(const AForm &other);
|
||||||
~AForm();
|
virtual ~AForm();
|
||||||
AForm &operator=(const AForm &other) = delete;
|
AForm &operator=(const AForm &other) = delete;
|
||||||
const std::string &getName() const;
|
const std::string &getName() const;
|
||||||
bool getIsSigned() const;
|
bool getIsSigned() const;
|
||||||
|
|||||||
21
ex03/inc/Intern.hpp
Normal file
21
ex03/inc/Intern.hpp
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#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);
|
||||||
|
};
|
||||||
@ -2,6 +2,7 @@
|
|||||||
#define COLORS_H
|
#define COLORS_H
|
||||||
|
|
||||||
#define BUREAUCRAT BOLD BACKGROUND4 " Bureaucrat: " RESET " "
|
#define BUREAUCRAT BOLD BACKGROUND4 " Bureaucrat: " RESET " "
|
||||||
|
#define INTERN BOLD BACKGROUND6 " Intern: " RESET " "
|
||||||
#define AFORM BOLD BACKGROUND5 " AForm: " RESET " "
|
#define AFORM BOLD BACKGROUND5 " AForm: " RESET " "
|
||||||
#define SHRUBBERY_CREATION_FORM BOLD BACKGROUND10 " ShrubberyCreationForm: " RESET " "
|
#define SHRUBBERY_CREATION_FORM BOLD BACKGROUND10 " ShrubberyCreationForm: " RESET " "
|
||||||
#define ROBOTOMY_REQUEST_FORM BOLD BACKGROUND11 " RobotomyRequestForm: " RESET " "
|
#define ROBOTOMY_REQUEST_FORM BOLD BACKGROUND11 " RobotomyRequestForm: " RESET " "
|
||||||
@ -9,6 +10,7 @@
|
|||||||
|
|
||||||
#define CONSTRUCTOR "Constructor called"
|
#define CONSTRUCTOR "Constructor called"
|
||||||
#define PARAMETERIZED_CONSTRUCTOR "Parameterized constructor called"
|
#define PARAMETERIZED_CONSTRUCTOR "Parameterized constructor called"
|
||||||
|
#define ASSIGNMENT_OPERATOR "Assignment operator called"
|
||||||
#define DESTRUCTOR "Destructor called"
|
#define DESTRUCTOR "Destructor called"
|
||||||
#define COPY_CONSTRUCTOR "Copy constructor called"
|
#define COPY_CONSTRUCTOR "Copy constructor called"
|
||||||
|
|
||||||
|
|||||||
68
ex03/src/Intern.cpp
Normal file
68
ex03/src/Intern.cpp
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
#include "Intern.hpp"
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#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;
|
||||||
|
}
|
||||||
@ -2,77 +2,35 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include "Bureaucrat.hpp"
|
#include "Bureaucrat.hpp"
|
||||||
#include "PresidentialPardonForm.hpp"
|
#include "Intern.hpp"
|
||||||
#include "RobotomyRequestForm.hpp"
|
#include "AForm.hpp"
|
||||||
#include "ShrubberyCreationForm.hpp"
|
|
||||||
|
|
||||||
#include "colors.h"
|
#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)
|
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;
|
std::cout << INFO << "Creating form: " << formName << std::endl;
|
||||||
try
|
AForm *form = intern.makeForm(formName, "Target");
|
||||||
|
if (form)
|
||||||
{
|
{
|
||||||
Bureaucrat bureaucrat("John", i);
|
std::cout << "Created form: " << *form << std::endl;
|
||||||
test(bureaucrat);
|
try {
|
||||||
std::cout << END_SCOPE << std::endl;
|
bureaucrat.signForm(*form);
|
||||||
}
|
form->execute(bureaucrat);
|
||||||
catch (const std::exception &e)
|
|
||||||
{
|
} catch (const std::exception &e) {
|
||||||
std::cerr << EXCEPTION << e.what() << std::endl;
|
std::cerr << EXCEPTION << e.what() << std::endl;
|
||||||
|
}
|
||||||
|
delete form;
|
||||||
}
|
}
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user