CPP05/ex03/src/Bureaucrat.cpp

103 lines
2.2 KiB
C++

#include "Bureaucrat.hpp"
#include <iostream>
#include "AForm.hpp"
#include "colors.h"
Bureaucrat::Bureaucrat(std::string name, int grade) : _name(name)
{
std::cout << BUREAUCRAT PARAMETERIZED_CONSTRUCTOR << std::endl;
if (grade < HIGHEST_GRADE)
throw(Bureaucrat::GradeTooHighException());
if (grade > LOWEST_GRADE)
throw(Bureaucrat::GradeTooLowException());
_grade = grade;
std::cout << BUREAUCRAT << *this << std::endl;
}
Bureaucrat::Bureaucrat(Bureaucrat &other)
: _name(other._name), _grade(other._grade)
{
std::cout << BUREAUCRAT COPY_CONSTRUCTOR << std::endl;
std::cout << BUREAUCRAT << *this << std::endl;
}
Bureaucrat::~Bureaucrat()
{
std::cout << BUREAUCRAT DESTRUCTOR << std::endl;
}
Bureaucrat &Bureaucrat::operator++() // Pre-increment
{
if (_grade <= HIGHEST_GRADE)
throw(Bureaucrat::GradeTooHighException());
_grade--;
return *this;
}
Bureaucrat Bureaucrat::operator++(int) // Post-increment
{
Bureaucrat temp(*this);
++(*this);
return temp;
}
Bureaucrat &Bureaucrat::operator--() // Pre-decrement
{
if (_grade >= LOWEST_GRADE)
throw(Bureaucrat::GradeTooLowException());
_grade++;
return *this;
}
Bureaucrat Bureaucrat::operator--(int) // Post-decrement
{
Bureaucrat temp(*this);
--(*this);
return temp;
}
int Bureaucrat::getGrade() const
{
return _grade;
}
const std::string Bureaucrat::getName() const
{
return _name;
}
void Bureaucrat::signForm(AForm &form)
{
try
{
form.beSigned(*this);
std::cout << BUREAUCRAT << _name << " signs " << form.getName()
<< std::endl;
}
catch (std::exception &e)
{
std::cout << EXCEPTION << _name << " cannot sign " << form.getName()
<< " because: " << e.what() << std::endl;
}
}
const char *Bureaucrat::GradeTooHighException::what() const throw()
{
return "Grade too high!";
}
const char *Bureaucrat::GradeTooLowException::what() const throw()
{
return "Grade too low!";
}
std::ostream &operator<<(std::ostream &os, const Bureaucrat &bureaucrat)
{
std::cout << bureaucrat.getName() << ", bureaucrat grade "
<< bureaucrat.getGrade();
return os;
}