CPP05/ex00/src/Bureaucrat.cpp
whaffman f916d5cb2f Refactor Bureaucrat and Form classes to implement move semantics and improve exception handling
- Added move constructors and move assignment operators to Bureaucrat and Form classes.
- Updated constructors and destructors to include logging for move operations.
- Enhanced Bureaucrat class with an executeForm method to handle form execution.
- Refactored color definitions in colors.h for better organization and clarity.
- Removed redundant macro definitions and improved code readability.
- Updated main functions to utilize new executeForm method for better exception handling.
- Ensured proper handling of copy and move semantics across all relevant classes.
2026-03-27 19:54:54 +01:00

95 lines
2.1 KiB
C++

#include "Bureaucrat.hpp"
#include <iostream>
#include "colors.h"
#define BUREAUCRAT BOLD BACKGROUND4 " Bureaucrat: " RESET " "
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(const Bureaucrat &other)
: _name(other._name), _grade(other._grade)
{
std::cout << BUREAUCRAT COPY_CONSTRUCTOR << std::endl;
std::cout << BUREAUCRAT << *this << std::endl;
}
Bureaucrat::Bureaucrat(Bureaucrat &&other) noexcept
: _name(std::move(other._name)), _grade(other._grade)
{
std::cout << BUREAUCRAT MOVE_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;
}
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)
{
os << bureaucrat.getName() << ", bureaucrat grade "
<< bureaucrat.getGrade() << ".";
return os;
}