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