/ CPP_Modules / CPP_Module_05 / ex02 / 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 22:57:44 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::cout
 89  		<< "INFO: "
 90  		<< this->getName()
 91  		<< " has been pardoned by "
 92  		<< "\033[34m"
 93  		<< "Zaphod Beeblebrox!"
 94  		<< "\033[0m"
 95  		<< std::endl;
 96  	}
 97  }
 98  
 99  /* ************************************************************************** */
100  /*              GradeTooHighException : Constructor & Destructor              */
101  /* ************************************************************************** */
102  PresidentialPardonForm::GradeTooHighException::GradeTooHighException()
103  	: mMessage("PresidentialPardonForm::GradeTooHighException")
104  {
105  	// nothing to do
106  }
107  
108  PresidentialPardonForm::GradeTooHighException::GradeTooHighException
109  (std::string const& msg)
110  	: mMessage("PresidentialPardonForm::GradeTooHighException: " + msg)
111  {
112  	// nothing to do
113  }
114  
115  PresidentialPardonForm::GradeTooHighException::GradeTooHighException
116  (GradeTooHighException const& target)
117  	: AForm::GradeTooHighException(target.getMessage())
118  {
119  	// nothing to do
120  }
121  
122  PresidentialPardonForm::GradeTooHighException&
123  PresidentialPardonForm::GradeTooHighException::operator=
124  (GradeTooHighException const& target)
125  {
126  	if (this != &target)
127  	{
128  		this->mMessage = target.getMessage();
129  	}
130  	return *this;
131  }
132  
133  PresidentialPardonForm::GradeTooHighException::~GradeTooHighException() throw()
134  {
135  	// nothing to do
136  }
137  
138  /* ************************************************************************** */
139  /*                  GradeTooHighException : Getter Function                   */
140  /* ************************************************************************** */
141  std::string const&	PresidentialPardonForm::GradeTooHighException::getMessage()
142  	const
143  {
144  	return this->mMessage;
145  }
146  
147  /* ************************************************************************** */
148  /*                 GradeTooHighException : Function Override                  */
149  /* ************************************************************************** */
150  const char*	PresidentialPardonForm::GradeTooHighException::what() const throw()
151  {
152  	return this->getMessage().c_str();
153  }
154  
155  /* ************************************************************************** */
156  /*              GradeTooLowException : Constructor & Destructor               */
157  /* ************************************************************************** */
158  PresidentialPardonForm::GradeTooLowException::GradeTooLowException()
159  	: mMessage("PresidentialPardonForm::GradeTooLowException")
160  {
161  	// nothing to do
162  }
163  
164  PresidentialPardonForm::GradeTooLowException::GradeTooLowException
165  (std::string const& msg)
166  	: mMessage("PresidentialPardonForm::GradeTooLowException: " + msg)
167  {
168  	// nothing to do
169  }
170  
171  PresidentialPardonForm::GradeTooLowException::GradeTooLowException
172  (GradeTooLowException const& target)
173  	: AForm::GradeTooLowException(target.getMessage())
174  {
175  	// nothing to do
176  }
177  
178  PresidentialPardonForm::GradeTooLowException&
179  PresidentialPardonForm::GradeTooLowException::operator=
180  (GradeTooLowException const& target)
181  {
182  	if (this != &target)
183  	{
184  		this->mMessage = target.getMessage();
185  	}
186  	return *this;
187  }
188  
189  PresidentialPardonForm::GradeTooLowException::~GradeTooLowException() throw()
190  {
191  	// nothing to do
192  }
193  
194  /* ************************************************************************** */
195  /*                   GradeTooLowException : Getter Function                   */
196  /* ************************************************************************** */
197  std::string const&	PresidentialPardonForm::GradeTooLowException::getMessage()
198  	const
199  {
200  	return this->mMessage;
201  }
202  
203  /* ************************************************************************** */
204  /*                  GradeTooLowException : Function Override                  */
205  /* ************************************************************************** */
206  const char*	PresidentialPardonForm::GradeTooLowException::what() const throw()
207  {
208  	return this->getMessage().c_str();
209  }
210  
211  /* ************************************************************************** */
212  /*             FormNotSignedException : Constructor & Destructor              */
213  /* ************************************************************************** */
214  PresidentialPardonForm::FormNotSignedException::FormNotSignedException()
215  	: mMessage("PresidentialPardonForm::FormNotSignedException")
216  {
217  	// nothing to do
218  }
219  
220  PresidentialPardonForm::FormNotSignedException::FormNotSignedException
221  (std::string const& msg)
222  	: mMessage("PresidentialPardonForm::FormNotSignedException: " + msg)
223  {
224  	// nothing to do
225  }
226  
227  PresidentialPardonForm::FormNotSignedException::FormNotSignedException
228  (FormNotSignedException const& target)
229  	: mMessage(target.getMessage())
230  {
231  	// nothing to do
232  }
233  
234  PresidentialPardonForm::FormNotSignedException&
235  PresidentialPardonForm::FormNotSignedException::operator=
236  (FormNotSignedException const& target)
237  {
238  	if (this != &target)
239  	{
240  		this->mMessage = target.getMessage();
241  	}
242  	return *this;
243  }
244  
245  PresidentialPardonForm::FormNotSignedException::~FormNotSignedException()
246  throw()
247  {
248  	// nothing to do
249  }
250  
251  /* ************************************************************************** */
252  /*                  FormNotSigendException : Getter Function                  */
253  /* ************************************************************************** */
254  std::string const&	PresidentialPardonForm::FormNotSignedException::getMessage()
255  	const
256  {
257  	return this->mMessage;
258  }
259  
260  /* ************************************************************************** */
261  /*                 FormNotSigendException : Function Override                 */
262  /* ************************************************************************** */
263  const char*	PresidentialPardonForm::FormNotSignedException::what() const throw()
264  {
265  	return this->getMessage().c_str();
266  }
267  
268  /* ************************************************************************** */
269  /*                              Global Function                               */
270  /* ************************************************************************** */
271  
272  std::ostream&	operator<<
273  (std::ostream& os, PresidentialPardonForm const& target)
274  {
275  	std::string	isAFormSigned = target.getIsSigned() ? "true" : "false";
276  
277  	os << target.getName()
278  	<< ", PresidentialPardonForm signed " << isAFormSigned
279  	<< ", PresidentialPardonForm sign grade " << target.getSignGrade()
280  	<< ", PresidentialPardonForm execution grade " << target.getExecuteGrade()
281  	<< ".";
282  	return os;
283  }
284  
285  /* ************************************************************************** */
286  /*                              Helper Functions                              */
287  /* ************************************************************************** */
288  static void	_copyConstWarning(std::string const& variable)
289  {
290  	std::cout << "INFO: You are trying to overwrite a const type variable: " <<
291  		variable << ". " << "This operation will be ignored." << std::endl;
292  }