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