/ CPP_Modules / CPP_Module_05 / ex03 / bonus / Bureaucrat.cpp
Bureaucrat.cpp
  1  /* ************************************************************************** */
  2  /*                                                                            */
  3  /*                                                        :::      ::::::::   */
  4  /*   Bureaucrat.cpp                                     :+:      :+:    :+:   */
  5  /*                                                    +:+ +:+         +:+     */
  6  /*   By: gychoi <gychoi@student.42seoul.kr>         +#+  +:+       +#+        */
  7  /*                                                +#+#+#+#+#+   +#+           */
  8  /*   Created: 2023/11/10 17:36:55 by gychoi            #+#    #+#             */
  9  /*   Updated: 2023/12/08 21:18:34 by gychoi           ###   ########.fr       */
 10  /*                                                                            */
 11  /* ************************************************************************** */
 12  
 13  #include "Bureaucrat.hpp"
 14  #include "AForm.hpp"
 15  
 16  static void	_copyConstWarning(std::string const& object);
 17  
 18  /* ************************************************************************** */
 19  /*                          Constructor & Destructor                          */
 20  /* ************************************************************************** */
 21  Bureaucrat::Bureaucrat()
 22  	: mName(std::string())
 23  	, mGrade(0)
 24  {
 25  	// nothing to do
 26  }
 27  
 28  Bureaucrat::Bureaucrat(std::string const& name, int grade)
 29  	: mName(name),
 30  	  mGrade(grade)
 31  {
 32  	if (grade < 1)
 33  	{
 34  		throw GradeTooHighException();
 35  	}
 36  	else if (grade > 150)
 37  	{
 38  		throw GradeTooLowException();
 39  	}
 40  	else
 41  	{
 42  		// nothing to do
 43  	}
 44  }
 45  
 46  Bureaucrat::Bureaucrat(Bureaucrat const& target)
 47  	: mName(target.getName()),
 48  	  mGrade(target.getGrade())
 49  {
 50  	// nothing to do
 51  }
 52  
 53  Bureaucrat&	Bureaucrat::operator=(Bureaucrat const& target)
 54  {
 55  	if (this != &target)
 56  	{
 57  		_copyConstWarning("mName");
 58  		this->mGrade = target.getGrade();
 59  	}
 60  	return *this;
 61  }
 62  
 63  Bureaucrat::~Bureaucrat()
 64  {
 65  	// nothing to do
 66  }
 67  
 68  /* ************************************************************************** */
 69  /*                         Getter & Setter Functions                          */
 70  /* ************************************************************************** */
 71  std::string const&	Bureaucrat::getName() const
 72  {
 73  	return this->mName;
 74  }
 75  
 76  int	Bureaucrat::getGrade() const
 77  {
 78  	return this->mGrade;
 79  }
 80  
 81  void	Bureaucrat::setGrade(int grade)
 82  {
 83  	if (grade < 1)
 84  	{
 85  		throw GradeTooHighException("Grade too high");
 86  	}
 87  	else if (grade > 150)
 88  	{
 89  		throw GradeTooLowException("Grade too low!");
 90  	}
 91  	else
 92  	{
 93  		this->mGrade = grade;
 94  	}
 95  }
 96  
 97  /* ************************************************************************** */
 98  /*                           Public Member Function                           */
 99  /* ************************************************************************** */
100  void	Bureaucrat::increaseGrade()
101  {
102  	int	grade = this->getGrade();
103  
104  	if (grade > 1)
105  	{
106  		this->setGrade(--grade);
107  	}
108  	else
109  	{
110  		throw GradeTooHighException("Grade too high!");
111  	}
112  }
113  
114  void	Bureaucrat::decreaseGrade()
115  {
116  	int	grade = this->getGrade();
117  
118  	if (grade < 150)
119  	{
120  		this->setGrade(++grade);
121  	}
122  	else
123  	{
124  		throw GradeTooLowException("Grade too low!");
125  	}
126  }
127  
128  void	Bureaucrat::signForm(AForm& form)
129  {
130  	try
131  	{
132  		form.beSigned(*this);
133  	}
134  	catch (std::exception & e)
135  	{
136  		throw;
137  	}
138  }
139  
140  void	Bureaucrat::executeForm(AForm const& form)
141  {
142  	try
143  	{
144  		form.execute(*this);
145  	}
146  	catch (std::exception & e)
147  	{
148  		throw;
149  	}
150  }
151  
152  /* ************************************************************************** */
153  /*              GradeTooHighException : Constructor & Destructor              */
154  /* ************************************************************************** */
155  Bureaucrat::GradeTooHighException::GradeTooHighException()
156  	: mMessage("Bureaucrat::GradeTooHighException")
157  {
158  	// nothing to do
159  }
160  
161  Bureaucrat::GradeTooHighException::GradeTooHighException(std::string const& msg)
162  	: mMessage("Bureaucrat::GradeTooHighException: " + msg)
163  {
164  	// nothing to do
165  }
166  
167  Bureaucrat::GradeTooHighException::GradeTooHighException
168  (GradeTooHighException const& target)
169  	: mMessage(target.getMessage())
170  {
171  	// nothing to do
172  }
173  
174  Bureaucrat::GradeTooHighException&
175  Bureaucrat::GradeTooHighException::operator=
176  (GradeTooHighException const& target)
177  {
178  	if (this != &target)
179  	{
180  		this->mMessage = target.getMessage();
181  	}
182  	return *this;
183  }
184  
185  Bureaucrat::GradeTooHighException::~GradeTooHighException() throw()
186  {
187  	// nothing to do
188  }
189  
190  /* ************************************************************************** */
191  /*                  GradeTooHighException : Getter Function                   */
192  /* ************************************************************************** */
193  std::string const&	Bureaucrat::GradeTooHighException::getMessage() const
194  {
195  	return this->mMessage;
196  }
197  
198  /* ************************************************************************** */
199  /*                 GradeTooHighException : Function Override                  */
200  /* ************************************************************************** */
201  const char*	Bureaucrat::GradeTooHighException::what() const throw()
202  {
203  	return this->getMessage().c_str();
204  }
205  
206  /* ************************************************************************** */
207  /*              GradeTooLowException : Constructor & Destructor               */
208  /* ************************************************************************** */
209  Bureaucrat::GradeTooLowException::GradeTooLowException()
210  	: mMessage("Bureaucrat::GradeTooLowException")
211  {
212  	// nothing to do
213  }
214  
215  Bureaucrat::GradeTooLowException::GradeTooLowException(std::string const& msg)
216  	: mMessage("Bureaucrat::GradeTooLowException: " + msg)
217  {
218  	// nothing to do
219  }
220  
221  Bureaucrat::GradeTooLowException::GradeTooLowException
222  (GradeTooLowException const& target)
223  	: mMessage(target.getMessage())
224  {
225  	// nothing to do
226  }
227  
228  Bureaucrat::GradeTooLowException&
229  Bureaucrat::GradeTooLowException::operator=
230  (GradeTooLowException const& target)
231  {
232  	if (this != &target)
233  	{
234  		this->mMessage = target.getMessage();
235  	}
236  	return *this;
237  }
238  
239  Bureaucrat::GradeTooLowException::~GradeTooLowException() throw()
240  {
241  	// nothing to do
242  }
243  
244  /* ************************************************************************** */
245  /*                   GradeTooLowException : Getter Function                   */
246  /* ************************************************************************** */
247  std::string const&	Bureaucrat::GradeTooLowException::getMessage() const
248  {
249  	return this->mMessage;
250  }
251  
252  /* ************************************************************************** */
253  /*                  GradeTooLowException : Function Override                  */
254  /* ************************************************************************** */
255  const char*	Bureaucrat::GradeTooLowException::what() const throw()
256  {
257  	return this->getMessage().c_str();
258  }
259  
260  /* ************************************************************************** */
261  /*                              Global Function                               */
262  /* ************************************************************************** */
263  std::ostream&	operator<<(std::ostream& os, Bureaucrat const& target)
264  {
265  	os << target.getName() << ", bureaucrat grade " << target.getGrade() << ".";
266  	return os;
267  }
268  
269  /* ************************************************************************** */
270  /*                              Helper Functions                              */
271  /* ************************************************************************** */
272  static void	_copyConstWarning(std::string const& variable)
273  {
274  	std::cout << "INFO: You are trying to overwrite a const type variable: " <<
275  		variable << ". " << "This operation will be ignored." << std::endl;
276  }