add: ex03

This commit is contained in:
Camille Chauvet 2023-09-13 11:28:31 +00:00
parent ece0db3779
commit 05a614c92a
14 changed files with 989 additions and 0 deletions

26
ex03/Makefile Normal file
View File

@ -0,0 +1,26 @@
CXX := c++
CXXFLAGS := -std=c++98 -Wall -Wextra -Werror -g
SRCDIR := src
OBJDIR := obj
NAME := ex03
SRCS := $(wildcard $(SRCDIR)/*.cpp)
OBJS := $(patsubst $(SRCDIR)/%.cpp,$(OBJDIR)/%.o,$(SRCS))
all: $(NAME)
$(OBJDIR)/%.o: $(SRCDIR)/%.cpp
mkdir -p obj
$(CXX) $(CXXFLAGS) -c $< -o $@
$(NAME): $(OBJS)
$(CXX) $(CXXFLAGS) $^ -o $@
clean:
rm -rf $(OBJDIR)
fclean: clean
rm -fr $(NAME)
re: fclean
@make --no-print-directory all

109
ex03/src/AForm.cpp Normal file
View File

@ -0,0 +1,109 @@
#include "AForm.hpp"
#include <iostream>
#include <ostream>
#include <string>
AForm::AForm()
{
std::cout << "AForm()" << std::endl;
}
AForm::AForm(const AForm& src):
_name(src._name),
_target(src._target)
{
std::cout << "AForm(AForm)" << std::endl;
*this = src;
}
AForm::AForm(const std::string& name, int to_execute, int to_sign, const std::string& target):
_name(name),
_signed(0),
_to_sign(to_sign),
_to_execute(to_execute),
_target(target)
{
std::cout << "AForm(" << name << ", " << to_execute << ", " << to_sign << ")" << std::endl;
if (to_execute > 150 || to_sign < 1)
throw GradeTooLowException();
if (to_execute < 1 || to_sign < 1)
throw GradeTooHighException();
}
AForm::~AForm()
{
std::cout << "~AForm()" << std::endl;
}
AForm& AForm::operator=(const AForm& src)
{
this->_signed = src._signed;
this->_to_sign = src._to_sign;
this->_to_execute = src._to_execute;
return *this;
}
void AForm::beSigned(const Bureaucrat &bureaucrat)
{
if (this->_signed == true)
{
std::cout << bureaucrat.getName() << " couldn't sign " << this->_name << " because " << "the AForm is already sign" << std::endl;
return;
}
if (bureaucrat.getGrade() > this->_to_sign)
throw GradeTooLowException();
this->_signed = true;
std::cout << bureaucrat.getName() << " signed " << this->_name << std::endl;
}
void AForm::execute(const Bureaucrat &exec) const
{
if (!this->_signed)
throw NotSignedException();
if (exec.getGrade() > this->_to_execute)
throw GradeTooLowException();
}
const std::string& AForm::getName() const
{
return this->_name;
}
bool AForm::getSigned() const
{
return this->_signed;
}
int AForm::getGradeToBeExecute() const
{
return this->_to_execute;
}
int AForm::getGradeToBeSign() const
{
return this->_to_sign;
}
std::ostream& operator<<(std::ostream& os, const AForm& src)
{
os << src.getName() << " AForm signed: " << src.getSigned() << ", grade to be execute: " << src.getGradeToBeExecute() << ", grade to be sign: " << src.getGradeToBeSign() << "." << std::endl;
return os;
}
const char* AForm::GradeTooLowException::what() const throw()
{
return "grade is to low";
}
const char* AForm::GradeTooHighException::what() const throw()
{
return "grade is to high";
}
const char* AForm::NotSignedException::what() const throw()
{
return "form not signed";
}

57
ex03/src/AForm.hpp Normal file
View File

@ -0,0 +1,57 @@
#pragma once
#include <exception>
#include <ostream>
#include <string>
#include "Bureaucrat.hpp"
class Bureaucrat;
class AForm
{
private:
const std::string _name;
bool _signed;
int _to_sign;
int _to_execute;
protected:
const std::string _target;
public:
AForm();
AForm(const std::string& name, int to_execute, int to_sign, const std::string& target);
AForm(const AForm& src);
virtual ~AForm();
AForm& operator=(const AForm& src);
const std::string& getName() const;
bool getSigned() const;
int getGradeToBeExecute() const;
int getGradeToBeSign() const;
void beSigned(const Bureaucrat& bureaucrat);
virtual void execute(const Bureaucrat& exec) const = 0;
class GradeTooHighException: public std::exception
{
public:
const char * what() const throw();
};
class GradeTooLowException: public std::exception
{
public:
const char * what() const throw();
};
class NotSignedException: public std::exception
{
public:
const char * what() const throw();
};
};
std::ostream& operator<<(std::ostream& os, const AForm& src);

103
ex03/src/Bureaucrat.cpp Normal file
View File

@ -0,0 +1,103 @@
#include "Bureaucrat.hpp"
#include <iostream>
#include <exception>
#include <ostream>
#include <string>
Bureaucrat::Bureaucrat()
{
std::cout << "Bureaucrat()" << std::endl;
}
Bureaucrat::Bureaucrat(const Bureaucrat& src)
{
*this = src;
std::cout << "Bureaucrat(Bureaucrat)" << std::endl;
}
Bureaucrat::Bureaucrat(const std::string& name, int grade):
_name(name)
{
setGrade(grade);
std::cout << "Bureaucrat(" << name << ", " << grade << ")" << std::endl;
}
Bureaucrat::~Bureaucrat()
{
std::cout << "~Bureaucrat()" << std::endl;
}
Bureaucrat& Bureaucrat::operator=(const Bureaucrat &src)
{
this->_grade = src._grade;
return *this;
}
void Bureaucrat::setGrade(int new_grade)
{
if (new_grade > 150)
throw Bureaucrat::GradeTooLowException();
if (new_grade < 1)
throw Bureaucrat::GradeTooHighException();
this->_grade = new_grade;
}
const std::string& Bureaucrat::getName(void) const
{
return this->_name;
}
int Bureaucrat::getGrade(void) const
{
return this->_grade;
}
void Bureaucrat::increment()
{
if (this->_grade == 1)
throw GradeTooHighException();
this->_grade--;
}
void Bureaucrat::decrement()
{
if (this->_grade == 150)
throw GradeTooLowException();
this->_grade++;
}
void Bureaucrat::signForm(AForm& form) const
{
form.beSigned(*this);
}
void Bureaucrat::executeForm(AForm const & form) const
{
try
{
form.execute(*this);
std::cout << this->_name << " executed " << form.getName() << std::endl;
}
catch (std::exception& e)
{
std::cout << e.what() << std::endl;
}
}
const char* Bureaucrat::GradeTooLowException::what() const throw()
{
return "Too low grade";
}
const char* Bureaucrat::GradeTooHighException::what() const throw()
{
return "Too high grade";
}
std::ostream& operator<<(std::ostream &stream, Bureaucrat &src)
{
stream << src.getName() << ", bureaucrat grade " << src.getGrade() << "." << std::endl;
return stream;
}

46
ex03/src/Bureaucrat.hpp Normal file
View File

@ -0,0 +1,46 @@
#pragma once
#include <exception>
#include <string>
#include "AForm.hpp"
class AForm;
class Bureaucrat
{
private:
const std::string _name;
int _grade;
public:
Bureaucrat();
~Bureaucrat();
Bureaucrat(const Bureaucrat& src);
Bureaucrat(const std::string& name, int grade);
Bureaucrat& operator=(const Bureaucrat& src);
const std::string& getName() const;
int getGrade() const;
void setGrade(int new_grade);
void increment();
void decrement();
void signForm(AForm& form) const;
void executeForm(AForm const & form) const;
class GradeTooHighException: public std::exception
{
public:
const char* what() const throw();
};
class GradeTooLowException: public std::exception
{
public:
const char* what() const throw();
};
};
std::ostream& operator<<(std::ostream& stream, Bureaucrat &src);

56
ex03/src/Intern.cpp Normal file
View File

@ -0,0 +1,56 @@
#include "Intern.hpp"
#include "AForm.hpp"
#include "ShrubberyCreationForm.hpp"
#include "PresidentialPardonForm.hpp"
#include "RobotomyRequestForm.hpp"
#include <string>
#include <iostream>
Intern::Intern()
{
std::cout << "Intern()" << std::endl;
}
Intern::Intern(Intern const& src)
{
(void) src;
}
Intern::~Intern()
{
std::cout << "~Intern()" << std::endl;
}
Intern& Intern::operator=( Intern const& src )
{
(void) src;
return *this;
}
AForm* Intern::makeForm( std::string const& formName, std::string const& formTarget) const
{
static const std::string names[3] = {
"robotomy request",
"presidential pardon",
"shrubbery creation"
};
int name_idx;
for (name_idx = 0; name_idx < 3 && names[name_idx] != formName; ++name_idx) ;
switch (name_idx) {
case 0:
std::cout << "Intern creates " << formName << std::endl;
return new RobotomyRequestForm(formTarget);
case 1:
std::cout << "Intern creates " << formName << std::endl;
return new PresidentialPardonForm(formTarget);
case 2:
std::cout << "Intern creates " << formName << std::endl;
return new ShrubberyCreationForm(formTarget);
default:
std::cout << "Intern can't find form named " << formName << std::endl;
return NULL;
}
}

17
ex03/src/Intern.hpp Normal file
View File

@ -0,0 +1,17 @@
#pragma once
#include "AForm.hpp"
#include <string.h>
class Intern
{
public:
Intern();
Intern(const Intern& src);
~Intern();
Intern& operator=(const Intern& src);
AForm* makeForm( std::string const& formName, std::string const& formTarget ) const;
};

View File

@ -0,0 +1,41 @@
#include "PresidentialPardonForm.hpp"
#include "AForm.hpp"
#include <string>
#include <iostream>
PresidentialPardonForm::PresidentialPardonForm():
AForm("PresidentialPardonForm", 5, 25, "")
{
std::cout << "PresidentialPardonForm()" << std::endl;
}
PresidentialPardonForm::PresidentialPardonForm(const std::string& target):
AForm("PresidentialPardonForm", 5, 25, target)
{
std::cout << "PresidentialPardonForm(" << target << ")" << std::endl;
}
PresidentialPardonForm::PresidentialPardonForm(const PresidentialPardonForm& src):
AForm(src)
{
std::cout << "PresidentialPardonForm(PresidentialPardonForm)" << std::endl;
}
PresidentialPardonForm::~PresidentialPardonForm()
{
std::cout << "PresidentialPardonForm()" << std::endl;
}
PresidentialPardonForm& PresidentialPardonForm::operator=(const PresidentialPardonForm& src)
{
if (this != & src)
*this = src;
return *this;
}
void PresidentialPardonForm::execute(const Bureaucrat &exec) const
{
AForm::execute(exec);
std::cout << this->_target << " has been pardoned by Zaphod Beeblebrox" << std::endl;
}

View File

@ -0,0 +1,18 @@
#pragma once
#include "AForm.hpp"
#include "Bureaucrat.hpp"
#include <string>
class PresidentialPardonForm: public AForm
{
public:
PresidentialPardonForm();
PresidentialPardonForm(const std::string& target);
PresidentialPardonForm(const PresidentialPardonForm& src);
~PresidentialPardonForm();
PresidentialPardonForm& operator=(const PresidentialPardonForm& src);
void execute(const Bureaucrat& exec) const;
};

View File

@ -0,0 +1,49 @@
#include "RobotomyRequestForm.hpp"
#include "AForm.hpp"
#include <string>
#include <iostream>
RobotomyRequestForm::RobotomyRequestForm():
AForm("RobotomyRequestForm", 45, 72, "")
{
std::cout << "RobotomyRequestForm()" << std::endl;
}
RobotomyRequestForm::RobotomyRequestForm(const std::string& target):
AForm("RobotomyRequestForm", 45, 72, target)
{
std::cout << "RobotomyRequestForm(" << target << ")" << std::endl;
}
RobotomyRequestForm::RobotomyRequestForm(const RobotomyRequestForm& src):
AForm(src)
{
std::cout << "RobotomyRequestForm(RobotomyRequestForm)" << std::endl;
}
RobotomyRequestForm::~RobotomyRequestForm()
{
std::cout << "~RobotomyRequestForm()" << std::endl;
}
RobotomyRequestForm& RobotomyRequestForm::operator=(const RobotomyRequestForm& src)
{
if (this != & src)
*this = src;
return *this;
}
void RobotomyRequestForm::execute(const Bureaucrat &exec) const
{
static bool ok(false);
AForm::execute(exec);
std::cout << "Bzit bzit" << std::endl;
if (ok)
std::cout << this->_target << " be robotomized" << std::endl;
else
std::cout << this->_target << " robotomized failed" << std::endl;
ok = !ok;
}

View File

@ -0,0 +1,18 @@
#pragma once
#include "AForm.hpp"
#include "Bureaucrat.hpp"
#include <string>
class RobotomyRequestForm: public AForm
{
public:
RobotomyRequestForm();
RobotomyRequestForm(const std::string& target);
RobotomyRequestForm(const RobotomyRequestForm& src);
~RobotomyRequestForm();
RobotomyRequestForm& operator=(const RobotomyRequestForm& src);
void execute(const Bureaucrat& exec) const;
};

View File

@ -0,0 +1,71 @@
#include "ShrubberyCreationForm.hpp"
#include "AForm.hpp"
#include <iostream>
#include <fstream>
#include <string>
ShrubberyCreationForm::ShrubberyCreationForm():
AForm("shrubbery creation", 137, 145, "")
{
std::cout << "ShrubberyCreationForm()" << std::endl;
}
ShrubberyCreationForm::ShrubberyCreationForm(const std::string& target):
AForm("ShrubberyCreationForm", 137, 145, target)
{
std::cout << "ShrubberyCreationForm(" << target << ")" << std::endl;
}
ShrubberyCreationForm::ShrubberyCreationForm(const ShrubberyCreationForm& src):
AForm(src)
{
std::cout << "ShrubberyCreationForm(ShrubberyCreationForm)" << std::endl;
}
ShrubberyCreationForm::~ShrubberyCreationForm()
{
std::cout << "~ShrubberyCreationForm()" << std::endl;
}
ShrubberyCreationForm& ShrubberyCreationForm::operator=(const ShrubberyCreationForm &src)
{
if (this != &src)
*this = src;
return *this;
}
void ShrubberyCreationForm::execute(const Bureaucrat &exec) const
{
static const std::string asciiTrees =
" _-_\n"
" /~~ ~~\\\n"
" /~~ ~~\\\n"
"{ }\n"
" \\ _- -_ /\n"
" ~ \\\\ // ~\n"
"_- - | | _- _\n"
" _ - | | -_\n"
" // \\\\\n"
" _-_\n"
" /~~ ~~\\\n"
" /~~ ~~\\\n"
"{ }\n"
" \\ _- -_ /\n"
" ~ \\\\ // ~\n"
"_- - | | _- _\n"
" _ - | | -_\n"
" // \\\\\n"
;
AForm::execute(exec);
std::ofstream file((this->_target + "_shruberry").c_str());
if (!file.good())
{
std::cout << "error: " << this->_target << "_shruberry" << "file incorrect" << std::endl;
return;
}
file << asciiTrees;
}

View File

@ -0,0 +1,18 @@
#pragma once
#include "AForm.hpp"
#include "Bureaucrat.hpp"
#include <string>
class ShrubberyCreationForm: public AForm
{
public:
ShrubberyCreationForm();
ShrubberyCreationForm(const ShrubberyCreationForm& src);
ShrubberyCreationForm(const std::string& target);
~ShrubberyCreationForm();
ShrubberyCreationForm& operator=(const ShrubberyCreationForm& src);
void execute(const Bureaucrat& exec) const;
};

360
ex03/src/main.cpp Normal file
View File

@ -0,0 +1,360 @@
#include "AForm.hpp"
#include "Intern.hpp"
#include "Bureaucrat.hpp"
#include "RobotomyRequestForm.hpp"
#include "ShrubberyCreationForm.hpp"
#include "PresidentialPardonForm.hpp"
#include <iostream>
#include <string>
int main() {
std::cout << "-------------Bureaucrat TEST-----------" << std::endl;
std::cout << "Constructor too high grade" << std::endl;
{
try {
Bureaucrat crat("crat", 0);
} catch (std::exception &e) {
std::cout << e.what() << std::endl;
}
}
std::cout << std::endl;
std::cout << "Constructor too low grade" << std::endl;
{
try {
Bureaucrat crat("crat", 151);
} catch (std::exception &e) {
std::cout << e.what() << std::endl;
}
}
std::cout << std::endl;
std::cout << "increment too high" << std::endl;
{
Bureaucrat crat("crat", 1);
try {
crat.increment();
} catch (std::exception &e) {
std::cout << e.what() << std::endl;
}
}
std::cout << std::endl;
std::cout << "decrement too low" << std::endl;
{
Bureaucrat crat("crat", 150);
try {
crat.decrement();
} catch (std::exception &e) {
std::cout << e.what() << std::endl;
}
}
std::cout << std::endl;
std::cout << "setgrade too low" << std::endl;
{
Bureaucrat crat("crat", 42);
try {
crat.setGrade(151);
} catch (std::exception &e) {
std::cout << e.what() << std::endl;
}
}
std::cout << std::endl;
std::cout << "setgrade too high" << std::endl;
{
Bureaucrat crat("crat", 42);
try {
crat.setGrade(0);
} catch (std::exception &e) {
std::cout << e.what() << std::endl;
}
}
std::cout << std::endl;
std::cout << "increment normal" << std::endl;
{
Bureaucrat crat("crat", 42);
std::cout << "default grade: " << crat.getGrade() << std::endl;
crat.increment();
std::cout << "after increment grade: " << crat.getGrade() << std::endl;
}
std::cout << std::endl;
std::cout << "decrement normal" << std::endl;
{
Bureaucrat crat("crat", 42);
std::cout << "default grade: " << crat.getGrade() << std::endl;
crat.decrement();
std::cout << "after decrement grade: " << crat.getGrade() << std::endl;
}
std::cout << std::endl;
std::cout << "print" << std::endl;
{
Bureaucrat crat("crat", 42);
std::cout << crat;
}
std::cout << std::endl;
std::cout << "------------- Form TEST-----------" << std::endl;
std::cout << "print" << std::endl;
{
RobotomyRequestForm form("moi");
std::cout << form;
}
std::cout << std::endl;
std::cout << "------------- RobotomyRequestForm Sign TEST-----------" << std::endl;
std::cout << "Bureaucrat sign a RobotomyRequestForm" << std::endl;
{
RobotomyRequestForm form("form");
Bureaucrat crat("crat", 42);
crat.signForm(form);
}
std::cout << std::endl;
std::cout << "Too low Bureaucrat sign a RobotomyRequestForm" << std::endl;
{
RobotomyRequestForm form("form");
Bureaucrat crat("crat", 150);
try {
crat.signForm(form);
} catch (std::exception &e) {
std::cout << e.what() << std::endl;
}
}
std::cout << std::endl;
std::cout << "High Bureaucrat sign a low RobotomyRequestForm" << std::endl;
{
RobotomyRequestForm form("form");
Bureaucrat crat("crat", 1);
crat.signForm(form);
}
std::cout << std::endl;
std::cout << "RobotomyRequestForm already sign" << std::endl;
{
RobotomyRequestForm form("form");
Bureaucrat crat("crat", 1);
crat.signForm(form);
crat.signForm(form);
}
std::cout << std::endl;
std::cout << "------------- RobotomyRequestForm TEST-----------" << std::endl;
std::cout << "Bureaucrat sign a RobotomyRequestForm" << std::endl;
{
RobotomyRequestForm form("form");
Bureaucrat crat("crat", 42);
crat.signForm(form);
}
std::cout << std::endl;
std::cout << "Too low Bureaucrat sign a RobotomyRequestForm" << std::endl;
{
RobotomyRequestForm form("form");
Bureaucrat crat("crat", 150);
try {
crat.signForm(form);
} catch (std::exception &e) {
std::cout << e.what() << std::endl;
}
}
std::cout << std::endl;
std::cout << "High Bureaucrat sign a low RobotomyRequestForm" << std::endl;
{
RobotomyRequestForm form("form");
Bureaucrat crat("crat", 1);
crat.signForm(form);
}
std::cout << std::endl;
std::cout << "RobotomyRequestForm already sign" << std::endl;
{
RobotomyRequestForm form("form");
Bureaucrat crat("crat", 1);
crat.signForm(form);
crat.signForm(form);
}
std::cout << std::endl;
std::cout << "RobotomyRequestForm execute not signed form" << std::endl;
{
RobotomyRequestForm form("form");
Bureaucrat crat("crat", 1);
crat.executeForm(form);
}
std::cout << std::endl;
std::cout << "RobotomyRequestForm execute" << std::endl;
{
RobotomyRequestForm form("bozo");
Bureaucrat crat("crat", 1);
crat.signForm(form);
crat.executeForm(form);
crat.executeForm(form);
crat.executeForm(form);
}
std::cout << std::endl;
std::cout << "------------- PresidentialPardonForm TEST-----------" << std::endl;
std::cout << "Bureaucrat sign a PresidentialPardonForm" << std::endl;
{
PresidentialPardonForm form("form");
Bureaucrat crat("crat", 1);
crat.signForm(form);
}
std::cout << std::endl;
std::cout << "Too low Bureaucrat sign a PresidentialPardonForm" << std::endl;
{
PresidentialPardonForm form("form");
Bureaucrat crat("crat", 150);
try {
crat.signForm(form);
} catch (std::exception &e) {
std::cout << e.what() << std::endl;
}
}
std::cout << std::endl;
std::cout << "High Bureaucrat sign a low PresidentialPardonForm" << std::endl;
{
PresidentialPardonForm form("form");
Bureaucrat crat("crat", 1);
crat.signForm(form);
}
std::cout << std::endl;
std::cout << "PresidentialPardonForm already sign" << std::endl;
{
PresidentialPardonForm form("form");
Bureaucrat crat("crat", 1);
crat.signForm(form);
crat.signForm(form);
}
std::cout << std::endl;
std::cout << "PresidentialPardonForm not signed execute" << std::endl;
{
PresidentialPardonForm form("form");
Bureaucrat crat("crat", 1);
crat.executeForm(form);
}
std::cout << std::endl;
std::cout << "PresidentialPardonForm execute" << std::endl;
{
PresidentialPardonForm form("form");
Bureaucrat crat("crat", 1);
crat.signForm(form);
crat.executeForm(form);
}
std::cout << std::endl;
std::cout << "------------- ShrubberyCreationForm TEST-----------" << std::endl;
std::cout << "Bureaucrat sign a ShrubberyCreationForm" << std::endl;
{
ShrubberyCreationForm form("form");
Bureaucrat crat("crat", 42);
crat.signForm(form);
}
std::cout << std::endl;
std::cout << "Too low Bureaucrat sign a ShrubberyCreationForm" << std::endl;
{
ShrubberyCreationForm form("form");
Bureaucrat crat("crat", 150);
try {
crat.signForm(form);
} catch (std::exception &e) {
std::cout << e.what() << std::endl;
}
}
std::cout << std::endl;
std::cout << "High Bureaucrat sign a low ShrubberyCreationForm" << std::endl;
{
ShrubberyCreationForm form("form");
Bureaucrat crat("crat", 1);
crat.signForm(form);
}
std::cout << std::endl;
std::cout << "ShrubberyCreationForm already sign" << std::endl;
{
ShrubberyCreationForm form("form");
Bureaucrat crat("crat", 1);
crat.signForm(form);
crat.signForm(form);
}
std::cout << std::endl;
std::cout << "ShrubberyCreationForm not sign execute" << std::endl;
{
ShrubberyCreationForm form("form");
Bureaucrat crat("crat", 1);
crat.executeForm(form);
}
std::cout << std::endl;
std::cout << "ShrubberyCreationForm execute" << std::endl;
{
ShrubberyCreationForm form("bozo");
Bureaucrat crat("crat", 1);
crat.signForm(form);
crat.executeForm(form);
}
std::cout << std::endl;
std::cout << "------------- Intern TEST-----------" << std::endl;
std::cout << "Intern" << std::endl;
{
Intern intern;
static const std::string forms[] = {
"robotomy request",
"presidential pardon",
"shrubbery creation",
"bozo"
};
for (int i = 0; i != 4; ++i)
{
AForm* form = intern.makeForm(forms[i], "bozoman");
if (form != NULL)
delete form;
}
}
std::cout << std::endl;
}