123 lines
3.2 KiB
C++
123 lines
3.2 KiB
C++
/* ************************************************************************** */
|
||
/* */
|
||
/* ::: :::::::: */
|
||
/* Bureaucrat.cpp :+: :+: :+: */
|
||
/* +:+ +:+ +:+ */
|
||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||
/* +#+#+#+#+#+ +#+ */
|
||
/* Created: 2025/06/18 20:11:39 by mmoussou #+# #+# */
|
||
/* Updated: 2025/06/27 14:10:53 by mmoussou ### ########.fr */
|
||
/* */
|
||
/* ************************************************************************** */
|
||
|
||
#include "Bureaucrat.hpp"
|
||
#include "Form.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)
|
||
: _name("kanel"), _grade(150)
|
||
{
|
||
_log("➕", "Bureaucrat", "", "default constructor called");
|
||
}
|
||
|
||
Bureaucrat::Bureaucrat(const Bureaucrat &cpy)
|
||
{
|
||
_log("➕", "Bureaucrat", "", "copy constructor called");
|
||
if (&cpy != this)
|
||
{
|
||
_name = cpy.getName();
|
||
_grade = cpy.getGrade();
|
||
}
|
||
}
|
||
|
||
Bureaucrat::Bureaucrat(uint8_t grade)
|
||
: _name("kanel"), _grade(grade)
|
||
{
|
||
_log("➕", "Bureaucrat", "", "constructor called");
|
||
}
|
||
|
||
Bureaucrat::Bureaucrat(std::string name)
|
||
: _name(name), _grade(150)
|
||
{
|
||
_log("➕", "Bureaucrat", "", "constructor called");
|
||
}
|
||
|
||
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");
|
||
}
|
||
|
||
void Bureaucrat::signForm(Form &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;
|
||
}
|
||
}
|
||
|
||
|
||
Bureaucrat &Bureaucrat::operator=(Bureaucrat const &cpy)
|
||
{
|
||
if (&cpy == this)
|
||
return *this;
|
||
_grade = cpy._grade;
|
||
return *this;
|
||
}
|
||
|
||
void Bureaucrat::IncrementGrade(void)
|
||
{
|
||
if (_grade - 1 < MAXGRADE)
|
||
throw GradeTooHighException();
|
||
_grade--;
|
||
}
|
||
|
||
void Bureaucrat::DecrementGrade(void)
|
||
{
|
||
if (_grade + 1 > MINGRADE)
|
||
throw GradeTooLowException();
|
||
_grade++;
|
||
}
|
||
|
||
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");
|
||
}
|