1
0

」 feat: finished ex03

This commit is contained in:
2025-04-03 16:07:05 +02:00
parent bb8d13b9ed
commit bd578ea11e
17 changed files with 811 additions and 5 deletions

115
ex03/Bureaucrat.cpp Normal file
View File

@ -0,0 +1,115 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Bureaucrat.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/23 13:59:45 by adjoly #+# #+# */
/* Updated: 2025/04/03 14:21:09 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "Bureaucrat.hpp"
#include "AForm.hpp"
#include <exception>
void _log(std::string emoji, std::string what, std::string who,
std::string str) {
#ifdef VERBOSE
if (who.empty())
std::cout << "" << emoji << "" << what << ": " << str << std::endl;
else
std::cout << "" << emoji << "" << what << "(" << who << "): " << str
<< std::endl;
#else
(void)emoji, (void)what, (void)who, (void)str;
#endif
}
Bureaucrat::Bureaucrat(void) {
_log("", "Bureaucrat", "", "default constructor called");
}
Bureaucrat::Bureaucrat(const Bureaucrat &cpy) {
_log("", "Bureaucrat", "", "copy constructor called");
if (&cpy != this)
*this = cpy;
}
Bureaucrat::Bureaucrat(std::string name, uint8_t grade)
: _name(name), _grade(grade) {
_log("", "Bureaucrat", "", "constructor called");
}
const std::string &Bureaucrat::getName(void) const { return _name; }
uint8_t Bureaucrat::getGrade(void) const { return _grade; }
Bureaucrat::~Bureaucrat(void) {
_log("", "Bureaucrat", "", "destructor called");
}
Bureaucrat &Bureaucrat::operator=(Bureaucrat const &cpy) {
if (&cpy == this)
return *this;
_grade = cpy._grade;
return *this;
}
void Bureaucrat::executeForm(const AForm &form) const {
form.execute(*this);
}
Bureaucrat &Bureaucrat::operator++(void) {
_grade--;
if (_grade < MAXGRADE)
throw GradeTooHighException();
return *this;
}
Bureaucrat &Bureaucrat::operator--(void) {
_grade++;
if (_grade > MINGRADE)
throw GradeTooLowException();
return *this;
}
Bureaucrat Bureaucrat::operator++(int) {
Bureaucrat old = *this;
_grade--;
if (_grade < MAXGRADE)
throw GradeTooHighException();
return (old);
}
Bureaucrat Bureaucrat::operator--(int) {
Bureaucrat old = *this;
_grade++;
if (_grade > MINGRADE)
throw GradeTooLowException();
return old;
}
std::ostream &operator<<(std::ostream &os, Bureaucrat &val) {
os << val.getName() << ", bureaucrat grade " << (int)val.getGrade();
return os;
}
const char *Bureaucrat::GradeTooHighException::what() const throw() {
return ("Grade is too high");
}
const char *Bureaucrat::GradeTooLowException::what() const throw() {
return ("Grade is too low");
}
void Bureaucrat::signForm(AForm &f) {
try {
f.beSigned(*this);
std::cout << _name << " signed " << f.getName() << std::endl;
} catch (std::exception &e) {
std::cout << _name << " counldn't sign " << f.getName() << " because " << e.what() << std::endl;
}
}