/ CPP_Modules / CPP_Module_05 / ex01 / 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/11/29 22:27:57 by gychoi           ###   ########.fr       */
 10  /*                                                                            */
 11  /* ************************************************************************** */
 12  
 13  #include "Bureaucrat.hpp"
 14  #include "Form.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(Form& form)
129  {
130  	try
131  	{
132  		form.beSigned(*this);
133  		std::cout
134  		<< this->getName()
135  		<< " signed "
136  		<< form.getName()
137  		<< std::endl;
138  	}
139  	catch (std::exception & e)
140  	{
141  		std::cout
142  		<< this->getName()
143  		<< " couldn't sign "
144  		<< form.getName()
145  		<< " because "
146  		<< e.what()
147  		<< std::endl;
148  	}
149  }
150  
151  /* ************************************************************************** */
152  /*              GradeTooHighException : Constructor & Destructor              */
153  /* ************************************************************************** */
154  Bureaucrat::GradeTooHighException::GradeTooHighException()
155  	: mMessage("Bureaucrat::GradeTooHighException")
156  {
157  	// nothing to do
158  }
159  
160  Bureaucrat::GradeTooHighException::GradeTooHighException(std::string const& msg)
161  	: mMessage("Bureaucrat::GradeTooHighException: " + msg)
162  {
163  	// nothing to do
164  }
165  
166  Bureaucrat::GradeTooHighException::GradeTooHighException
167  (GradeTooHighException const& target)
168  	: mMessage(target.getMessage())
169  {
170  	// nothing to do
171  }
172  
173  Bureaucrat::GradeTooHighException&
174  Bureaucrat::GradeTooHighException::operator=
175  (GradeTooHighException const& target)
176  {
177  	if (this != &target)
178  	{
179  		this->mMessage = target.getMessage();
180  	}
181  	return *this;
182  }
183  
184  Bureaucrat::GradeTooHighException::~GradeTooHighException() throw()
185  {
186  	// nothing to do
187  }
188  
189  /* ************************************************************************** */
190  /*                  GradeTooHighException : Getter Function                   */
191  /* ************************************************************************** */
192  std::string const&	Bureaucrat::GradeTooHighException::getMessage() const
193  {
194  	return this->mMessage;
195  }
196  
197  /* ************************************************************************** */
198  /*                 GradeTooHighException : Function Override                  */
199  /* ************************************************************************** */
200  const char*	Bureaucrat::GradeTooHighException::what() const throw()
201  {
202  	return this->getMessage().c_str();
203  }
204  
205  /* ************************************************************************** */
206  /*              GradeTooLowException : Constructor & Destructor               */
207  /* ************************************************************************** */
208  Bureaucrat::GradeTooLowException::GradeTooLowException()
209  	: mMessage("Bureaucrat::GradeTooLowException")
210  {
211  	// nothing to do
212  }
213  
214  Bureaucrat::GradeTooLowException::GradeTooLowException(std::string const& msg)
215  	: mMessage("Bureaucrat::GradeTooLowException: " + msg)
216  {
217  	// nothing to do
218  }
219  
220  Bureaucrat::GradeTooLowException::GradeTooLowException
221  (GradeTooLowException const& target)
222  	: mMessage(target.getMessage())
223  {
224  	// nothing to do
225  }
226  
227  Bureaucrat::GradeTooLowException&
228  Bureaucrat::GradeTooLowException::operator=
229  (GradeTooLowException const& target)
230  {
231  	if (this != &target)
232  	{
233  		this->mMessage = target.getMessage();
234  	}
235  	return *this;
236  }
237  
238  Bureaucrat::GradeTooLowException::~GradeTooLowException() throw()
239  {
240  	// nothing to do
241  }
242  
243  /* ************************************************************************** */
244  /*                   GradeTooLowException : Getter Function                   */
245  /* ************************************************************************** */
246  std::string const&	Bureaucrat::GradeTooLowException::getMessage() const
247  {
248  	return this->mMessage;
249  }
250  
251  /* ************************************************************************** */
252  /*                  GradeTooLowException : Function Override                  */
253  /* ************************************************************************** */
254  const char*	Bureaucrat::GradeTooLowException::what() const throw()
255  {
256  	return this->getMessage().c_str();
257  }
258  
259  /* ************************************************************************** */
260  /*                              Global Function                               */
261  /* ************************************************************************** */
262  std::ostream&	operator<<(std::ostream& os, Bureaucrat const& target)
263  {
264  	os << target.getName() << ", bureaucrat grade " << target.getGrade() << ".";
265  	return os;
266  }
267  
268  /* ************************************************************************** */
269  /*                              Helper Functions                              */
270  /* ************************************************************************** */
271  static void	_copyConstWarning(std::string const& variable)
272  {
273  	std::cout << "INFO: You are trying to overwrite a const type variable: " <<
274  		variable << ". " << "This operation will be ignored." << std::endl;
275  }