|
|
@@ -0,0 +1,140 @@
|
|
|
+/* ************************************************************************** */
|
|
|
+/* */
|
|
|
+/* ::: :::::::: */
|
|
|
+/* Bureaucrat.cpp :+: :+: :+: */
|
|
|
+/* +:+ +:+ +:+ */
|
|
|
+/* By: bchanot <bchanot@42.fr> +#+ +:+ +#+ */
|
|
|
+/* +#+#+#+#+#+ +#+ */
|
|
|
+/* Created: 2025/12/26 17:51:13 by bchanot #+# #+# */
|
|
|
+/* Updated: 2025/12/30 18:27:44 by bchanot ### ########.fr */
|
|
|
+/* */
|
|
|
+/* ************************************************************************** */
|
|
|
+
|
|
|
+#include "Bureaucrat.hpp"
|
|
|
+#include "AForm.hpp"
|
|
|
+
|
|
|
+/*
|
|
|
+** ------------------------------- CONSTRUCTOR --------------------------------
|
|
|
+*/
|
|
|
+
|
|
|
+Bureaucrat::Bureaucrat() : _name("DefaultName"), _grade(150)
|
|
|
+{
|
|
|
+}
|
|
|
+
|
|
|
+Bureaucrat::Bureaucrat( std::string const name, u_int8_t grade ) : _name(name), _grade(grade)
|
|
|
+{
|
|
|
+ if (this->_grade > 150)
|
|
|
+ throw Bureaucrat::GradeTooHighException();
|
|
|
+ else if (this->_grade < 1)
|
|
|
+ throw Bureaucrat::GradeTooLowException();
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+Bureaucrat::Bureaucrat( const Bureaucrat & src ) : _name(src._name), _grade(src._grade)
|
|
|
+{
|
|
|
+ if (this->_grade > 150)
|
|
|
+ throw Bureaucrat::GradeTooHighException();
|
|
|
+ else if (this->_grade < 1)
|
|
|
+ throw Bureaucrat::GradeTooLowException();
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+/*
|
|
|
+** -------------------------------- DESTRUCTOR --------------------------------
|
|
|
+*/
|
|
|
+
|
|
|
+Bureaucrat::~Bureaucrat()
|
|
|
+{
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+/*
|
|
|
+** --------------------------------- OVERLOAD ---------------------------------
|
|
|
+*/
|
|
|
+
|
|
|
+Bureaucrat & Bureaucrat::operator=( Bureaucrat const & rhs )
|
|
|
+{
|
|
|
+ (void)rhs;
|
|
|
+ //if ( this != &rhs )
|
|
|
+ //{
|
|
|
+ //this->_value = rhs.getValue();
|
|
|
+ //}
|
|
|
+ return *this;
|
|
|
+}
|
|
|
+
|
|
|
+std::ostream & operator<<( std::ostream & o, Bureaucrat const & i )
|
|
|
+{
|
|
|
+ o << i.getName() << ", bureaucrat grade " << (int)i.getGrade();
|
|
|
+ return o;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+Bureaucrat &Bureaucrat::operator++(void) {
|
|
|
+ if (this->_grade > 1)
|
|
|
+ this->_grade--;
|
|
|
+ else
|
|
|
+ throw Bureaucrat::GradeTooHighException();
|
|
|
+ return *this;
|
|
|
+}
|
|
|
+
|
|
|
+Bureaucrat &Bureaucrat::operator--(void) {
|
|
|
+ if (this->_grade < 150)
|
|
|
+ this->_grade++;
|
|
|
+ else
|
|
|
+ throw Bureaucrat::GradeTooLowException();
|
|
|
+ return *this;
|
|
|
+}
|
|
|
+
|
|
|
+/*
|
|
|
+** --------------------------------- METHODS ----------------------------------
|
|
|
+*/
|
|
|
+
|
|
|
+void Bureaucrat::decrementGrade(void)
|
|
|
+{
|
|
|
+ if (this->_grade < 150)
|
|
|
+ this->_grade++;
|
|
|
+ else
|
|
|
+ throw Bureaucrat::GradeTooHighException();
|
|
|
+}
|
|
|
+
|
|
|
+void Bureaucrat::incrementGrade(void) {
|
|
|
+ if (this->_grade > 1)
|
|
|
+ this->_grade--;
|
|
|
+ else
|
|
|
+ throw Bureaucrat::GradeTooLowException();
|
|
|
+}
|
|
|
+
|
|
|
+void Bureaucrat::signForm(AForm *form)
|
|
|
+{
|
|
|
+ try {
|
|
|
+ form->beSigned(*this);
|
|
|
+ std::cout << this->_name << " signed " << form->getName() << std::endl;
|
|
|
+ } catch (std::exception &e) {
|
|
|
+ std::cout << this->_name << " couldn't sign " << form->getName() << " because " << e.what() << std::endl;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+void Bureaucrat::executeForm(AForm const & form) const {
|
|
|
+ try {
|
|
|
+ form.execute(*this);
|
|
|
+ std::cout << this->_name << " executed " << form.getName() << std::endl;
|
|
|
+ } catch (std::exception & e) {
|
|
|
+ std::cout << this->_name << " failed to execute " << form.getName() << std::endl;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+/*
|
|
|
+** --------------------------------- ACCESSOR ---------------------------------
|
|
|
+*/
|
|
|
+
|
|
|
+
|
|
|
+uint8_t Bureaucrat::getGrade(void) const {
|
|
|
+ return this->_grade;
|
|
|
+}
|
|
|
+
|
|
|
+std::string Bureaucrat::getName(void) const {
|
|
|
+ return this->_name;
|
|
|
+}
|
|
|
+
|
|
|
+/* ************************************************************************** */
|