ajout ex03 et correction dump core

This commit is contained in:
Hugo LAMY
2022-03-17 11:12:31 +01:00
parent bac6161af8
commit 5f090531bc
20 changed files with 1053 additions and 4 deletions

103
d05/ex03/srcs/AForm.cpp Normal file
View File

@@ -0,0 +1,103 @@
#include "AForm.hpp"
#define COPLIEN_COLOR B_CYAN
/*********************************************
* CONSTRUCTORS
*********************************************/
AForm::AForm( std::string name, std::string target, int signedGrade, int executeGrade )
: _name(name)
, _target(target)
, _signed(false)
, _signedGrade(signedGrade)
, _executeGrade(executeGrade) {
if (signedGrade > 150 || executeGrade > 150)
throw AForm::GradeTooLowException();
if (signedGrade < 1 || executeGrade < 1)
throw AForm::GradeTooHighException();
std::cout << COPLIEN_COLOR "AForm constructor" RESET "\n";
return;
}
AForm::AForm( AForm const & src )
: _name(src.getName())
, _signedGrade(src.getSignedGrade())
, _executeGrade(src.getExecuteGrade()) {
std::cout << COPLIEN_COLOR "AForm copy constructor" RESET "\n";
*this = src;
return;
}
/*********************************************
* DESTRUCTORS
*********************************************/
AForm::~AForm() {
std::cout << COPLIEN_COLOR "AForm destructor" RESET "\n";
return;
}
/*********************************************
* OPERATORS
*********************************************/
AForm & AForm::operator=( AForm const & rhs ) {
if ( this != &rhs ) {
_signed = rhs.getSigned();
}
return *this;
}
std::ostream & operator<<(std::ostream & o, AForm const & rhs)
{
o << "[form name]"
<< rhs.getName() << ", [target]"
<< rhs.getTarget() << ", [signed]"
<< rhs.getSigned() << ", [sign grade]"
<< rhs.getSignedGrade() << ", [exec grade]"
<< rhs.getExecuteGrade();
return (o);
}
/*********************************************
* ACCESSORS
*********************************************/
std::string AForm::getName() const {return _name;}
std::string AForm::getTarget() const {return _target;}
bool AForm::getSigned() const {return _signed;}
int AForm::getSignedGrade() const {return _signedGrade;}
int AForm::getExecuteGrade() const {return _executeGrade;}
/*********************************************
* PUBLIC MEMBER FUNCTIONS
*********************************************/
void AForm::beSigned( Bureaucrat const & b) {
if (b.getGrade() < _signedGrade)
_signed = true;
else
throw AForm::GradeTooLowException();
}
void AForm::execute(Bureaucrat const & executor) const {
if (!_signed)
throw NotSignedException();
if (executor.getGrade() > _executeGrade)
throw GradeTooLowException();
formAction();
}
/*********************************************
* NESTED CLASS
*********************************************/
const char * AForm::GradeTooHighException::what() const throw() {
return (B_RED "grade too high" RESET);
}
const char * AForm::GradeTooLowException::what() const throw() {
return (B_RED "grade too low" RESET);
}
const char * AForm::NotSignedException::what() const throw() {
return (B_RED "form is not signed" RESET);
}

View File

@@ -0,0 +1,117 @@
#include "Bureaucrat.hpp"
#define COPLIEN_COLOR B_CYAN
/*********************************************
* CONSTRUCTORS
*********************************************/
Bureaucrat::Bureaucrat( std::string name, int grade )
: _name(name)
, _grade(grade) {
if (grade > 150)
throw GradeTooLowException();
if (grade < 1)
throw GradeTooHighException();
std::cout << COPLIEN_COLOR "Bureaucrat constructor" RESET "\n";
return;
}
Bureaucrat::Bureaucrat( Bureaucrat const & src )
: _name(src.getName()) {
std::cout << COPLIEN_COLOR "Bureaucrat copy constructor" RESET "\n";
*this = src;
return;
}
/*********************************************
* DESTRUCTORS
*********************************************/
Bureaucrat::~Bureaucrat() {
std::cout << COPLIEN_COLOR "Bureaucrat destructor" RESET "\n";
return;
}
/*********************************************
* OPERATORS
*********************************************/
Bureaucrat & Bureaucrat::operator=( Bureaucrat const & rhs ) {
if ( this != &rhs )
_grade = rhs.getGrade();
return *this;
}
std::ostream & operator<<(std::ostream & o, Bureaucrat const & rhs)
{
o
<< rhs.getName() << ", bureaucrat grade "
<< rhs.getGrade();
return (o);
}
/*********************************************
* ACCESSORS
*********************************************/
std::string Bureaucrat::getName() const {return _name;}
int Bureaucrat::getGrade() const {return _grade;}
/*********************************************
* PUBLIC MEMBER FUNCTIONS
*********************************************/
void Bureaucrat::gradeUp() {
if (_grade > 1)
_grade--;
else
throw GradeTooHighException();
}
void Bureaucrat::gradeDown() {
if (_grade < 150)
_grade++;
else
throw GradeTooLowException();
}
void Bureaucrat::signForm( AForm & f) {
try {
f.beSigned( *this );
std::cout << _name << " signed "
<< f.getName() << "("
<< f.getTarget() << ")\n";
}
catch (std::exception & e) {
std::cout << _name << " couldn't sign " << f.getName()
<< f.getName() << "("
<< f.getTarget() << ")"
<< " because [" << e.what() << "]\n";
}
}
void Bureaucrat::executeForm( AForm const & f ) {
try {
f.execute( *this );
std::cout << _name << " executed "
<< f.getName() << "("
<< f.getTarget() << ")\n";
}
catch (std::exception & e) {
std::cout << _name << " couldn't execute "
<< f.getName() << "("
<< f.getTarget() << ")"
<< " because [" << e.what() << "]\n";
}
}
/*********************************************
* NESTED CLASS
*********************************************/
const char * Bureaucrat::GradeTooHighException::what() const throw() {
return (B_RED "grade higher than 1" RESET);
}
const char * Bureaucrat::GradeTooLowException::what() const throw() {
return (B_RED "grade lower than 150" RESET);
}

93
d05/ex03/srcs/Intern.cpp Normal file
View File

@@ -0,0 +1,93 @@
#include "Intern.hpp"
#define COPLIEN_COLOR B_CYAN
/*********************************************
* STATICS
*********************************************/
const t_formModel Intern::_chooseForm[] = {
{"Shrubbery", Intern::makeShrubbery},
{"Presidential", Intern::makePresidential},
{"Robotomy", Intern::makeRobotomy}
};
/*********************************************
* CONSTRUCTORS
*********************************************/
Intern::Intern() {
std::cout << COPLIEN_COLOR "Intern constructor" RESET "\n";
return;
}
Intern::Intern( Intern const & src __attribute__((unused))) {
std::cout << COPLIEN_COLOR "Intern copy constructor" RESET "\n";
*this = src;
return;
}
/*********************************************
* DESTRUCTORS
*********************************************/
Intern::~Intern() {
std::cout << COPLIEN_COLOR "Intern destructor" RESET "\n";
return;
}
/*********************************************
* OPERATORS
*********************************************/
Intern & Intern::operator=( Intern const & rhs __attribute__((unused))) {
return *this;
}
//std::ostream & operator<<(std::ostream & o, Intern const & rhs)
//{
// o << rhs.getFoo();
// return (o);
//}
/*********************************************
* ACCESSORS
*********************************************/
//std::string Intern::getFoo() const {return _foo;}
/*********************************************
* PRIVATE MEMBER FUNCTIONS
*********************************************/
AForm * Intern::makeForm(std::string formName, std::string formTarget) const {
unsigned int len = sizeof _chooseForm / sizeof _chooseForm[0];
for (unsigned int i = 0; i < len; i++)
if (!formName.compare(_chooseForm[i].name))
{
std::cout << "Intern creates " << formName << "\n";
return _chooseForm[i].create(formTarget);
}
std::cout << "Intern cannot create " << formName << "\n";
return NULL;
}
/*********************************************
* PUBLIC MEMBER FUNCTIONS
*********************************************/
AForm * Intern::makeShrubbery(std::string const &target) {
return new ShrubberyCreationForm(target);}
AForm * Intern::makePresidential(std::string const &target) {
return new PresidentialPardonForm(target);}
AForm * Intern::makeRobotomy(std::string const &target) {
return new RobotomyRequestForm(target);}
/*********************************************
* NESTED CLASS
*********************************************/
//void Intern::Class::function() {}

View File

@@ -0,0 +1,47 @@
#include "PresidentialPardonForm.hpp"
#define COPLIEN_COLOR B_CYAN
/*********************************************
* CONSTRUCTORS
*********************************************/
PresidentialPardonForm::PresidentialPardonForm( std::string target )
: AForm("presidential_creation", target, 25, 5){
std::cout << COPLIEN_COLOR "RobotomyRequestForm constructor" RESET "\n";
return;
}
PresidentialPardonForm::PresidentialPardonForm( PresidentialPardonForm const & src )
: AForm("presidential_creation", src.getTarget(), 25, 5) {
std::cout << COPLIEN_COLOR "PresidentialPardonForm copy constructor" RESET "\n";
*this = src;
return;
}
/*********************************************
* DESTRUCTORS
*********************************************/
PresidentialPardonForm::~PresidentialPardonForm() {
std::cout << COPLIEN_COLOR "RobotomyRequestForm destructor" RESET "\n";
return;
}
/*********************************************
* OPERATORS
*********************************************/
PresidentialPardonForm & PresidentialPardonForm::operator=( PresidentialPardonForm const & rhs ) {
AForm::operator=(rhs);
return *this;
}
/*********************************************
* PUBLIC MEMBER FUNCTIONS
*********************************************/
void PresidentialPardonForm::formAction() const {
std::cout << "Zaphod Beeblebrox pardon " << _target << "\n";
}

View File

@@ -0,0 +1,51 @@
#include "RobotomyRequestForm.hpp"
#define COPLIEN_COLOR B_CYAN
/*********************************************
* CONSTRUCTORS
*********************************************/
RobotomyRequestForm::RobotomyRequestForm( std::string target )
: AForm("robotomy_creation", target, 72, 45){
std::cout << COPLIEN_COLOR "RobotomyRequestForm constructor" RESET "\n";
return;
}
RobotomyRequestForm::RobotomyRequestForm( RobotomyRequestForm const & src )
: AForm("robotomy_creation", src.getTarget(), 72, 45) {
std::cout << COPLIEN_COLOR "RobotomyRequestForm copy constructor" RESET "\n";
*this = src;
return;
}
/*********************************************
* DESTRUCTORS
*********************************************/
RobotomyRequestForm::~RobotomyRequestForm() {
std::cout << COPLIEN_COLOR "RobotomyRequestForm destructor" RESET "\n";
return;
}
/*********************************************
* OPERATORS
*********************************************/
RobotomyRequestForm & RobotomyRequestForm::operator=( RobotomyRequestForm const & rhs ) {
AForm::operator=(rhs);
return *this;
}
/*********************************************
* PUBLIC MEMBER FUNCTIONS
*********************************************/
void RobotomyRequestForm::formAction() const {
std::cout << "*drill sounds*\n";
if (std::rand() % 2)
std::cout << _target << " robotomized with success\n";
else
std::cout << _target << " robotomization failed\n";
}

View File

@@ -0,0 +1,74 @@
#include "ShrubberyCreationForm.hpp"
#define COPLIEN_COLOR B_CYAN
/*********************************************
* CONSTRUCTORS
*********************************************/
ShrubberyCreationForm::ShrubberyCreationForm( std::string target )
: AForm("shrubbery_creation", target, 145, 137){
std::cout << COPLIEN_COLOR "ShrubberyCreationForm constructor" RESET "\n";
return;
}
ShrubberyCreationForm::ShrubberyCreationForm( ShrubberyCreationForm const & src )
: AForm("shrubbery_creation", src.getTarget(), 145, 137) {
std::cout << COPLIEN_COLOR "ShrubberyCreationForm copy constructor" RESET "\n";
*this = src;
return;
}
/*********************************************
* DESTRUCTORS
*********************************************/
ShrubberyCreationForm::~ShrubberyCreationForm() {
std::cout << COPLIEN_COLOR "ShrubberyCreationForm destructor" RESET "\n";
return;
}
/*********************************************
* OPERATORS
*********************************************/
ShrubberyCreationForm & ShrubberyCreationForm::operator=( ShrubberyCreationForm const & rhs ) {
AForm::operator=(rhs);
return *this;
}
/*********************************************
* PUBLIC MEMBER FUNCTIONS
*********************************************/
void ShrubberyCreationForm::formAction() const {
std::string name = _target + "_shrubbery";
std::ofstream ofs(name.c_str(), std::ofstream::out);
if (!ofs)
{
std::cout << "opening Shrubbery.txt file failed\n";
return ;
}
ofs << " * *\n";
ofs << " * * *\n";
ofs << " * * * * *\n";
ofs << " * * * * *\n";
ofs << " * * * * * * *\n";
ofs << " * * * * * .# * *\n";
ofs << " * * * #. .# * *\n";
ofs << " * \"#. #: #\" * * *\n";
ofs << " * * * \"#. ##\" *\n";
ofs << " * \"###\n";
ofs << " \"##\n";
ofs << " ##.\n";
ofs << " .##:\n";
ofs << " :###\n";
ofs << " ;###\n";
ofs << " ,####.\n";
ofs << " /\\/\\/\\/\\/\\/.######.\\/\\/\\/\\/\\\n";
ofs.close();
}