AForm.cpp
  1  /* ************************************************************************** */
  2  /*                                                                            */
  3  /*                                                        :::      ::::::::   */
  4  /*   AForm.cpp                                          :+:      :+:    :+:   */
  5  /*                                                    +:+ +:+         +:+     */
  6  /*   By: gychoi <gychoi@student.42seoul.kr>         +#+  +:+       +#+        */
  7  /*                                                +#+#+#+#+#+   +#+           */
  8  /*   Created: 2023/11/28 21:46:36 by gychoi            #+#    #+#             */
  9  /*   Updated: 2023/11/29 21:26:10 by gychoi           ###   ########.fr       */
 10  /*                                                                            */
 11  /* ************************************************************************** */
 12  
 13  #include "AForm.hpp"
 14  
 15  static void	_copyConstWarning(std::string const& object);
 16  
 17  /* ************************************************************************** */
 18  /*                          Constructor & Destructor                          */
 19  /* ************************************************************************** */
 20  AForm::AForm()
 21  	: mName(std::string()),
 22  	  mbIsSigned(false),
 23  	  mSignGrade(0),
 24  	  mExecuteGrade(0)
 25  {
 26  	// nothing to do
 27  }
 28  
 29  AForm::AForm(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  AForm::AForm(AForm 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  AForm&	AForm::operator=(AForm 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  AForm::~AForm()
 71  {
 72  	// nothing to do
 73  }
 74  
 75  /* ************************************************************************** */
 76  /*                         Getter & Setter Functions                          */
 77  /* ************************************************************************** */
 78  std::string const&	AForm::getName() const
 79  {
 80  	return this->mName;
 81  }
 82  
 83  bool	AForm::getIsSigned() const
 84  {
 85  	return this->mbIsSigned;
 86  }
 87  
 88  int	AForm::getSignGrade() const
 89  {
 90  	return this->mSignGrade;
 91  }
 92  
 93  int	AForm::getExecuteGrade() const
 94  {
 95  	return this->mExecuteGrade;
 96  }
 97  
 98  void	AForm::setIsSigned(bool isSigned)
 99  {
100  	this->mbIsSigned = isSigned;
101  }
102  
103  /* ************************************************************************** */
104  /*              GradeTooHighException : Constructor & Destructor              */
105  /* ************************************************************************** */
106  AForm::GradeTooHighException::GradeTooHighException()
107  	: mMessage("AForm::GradeTooHighException")
108  {
109  	// nothing to do
110  }
111  
112  AForm::GradeTooHighException::GradeTooHighException(std::string const& msg)
113  	: mMessage("AForm::GradeTooHighException: " + msg)
114  {
115  	// nothing to do
116  }
117  
118  AForm::GradeTooHighException::GradeTooHighException
119  (GradeTooHighException const& target)
120  	: mMessage(target.getMessage())
121  {
122  	// nothing to do
123  }
124  
125  AForm::GradeTooHighException&
126  AForm::GradeTooHighException::operator=
127  (GradeTooHighException const& target)
128  {
129  	if (this != &target)
130  	{
131  		this->mMessage = target.getMessage();
132  	}
133  	return *this;
134  }
135  
136  AForm::GradeTooHighException::~GradeTooHighException() throw()
137  {
138  	// nothing to do
139  }
140  
141  /* ************************************************************************** */
142  /*                  GradeTooHighException : Getter Function                   */
143  /* ************************************************************************** */
144  std::string const&	AForm::GradeTooHighException::getMessage() const
145  {
146  	return this->mMessage;
147  }
148  
149  /* ************************************************************************** */
150  /*                 GradeTooHighException : Function Override                  */
151  /* ************************************************************************** */
152  const char*	AForm::GradeTooHighException::what() const throw()
153  {
154  	return this->getMessage().c_str();
155  }
156  
157  /* ************************************************************************** */
158  /*              GradeTooLowException : Constructor & Destructor               */
159  /* ************************************************************************** */
160  AForm::GradeTooLowException::GradeTooLowException()
161  	: mMessage("AForm::GradeTooLowException")
162  {
163  	// nothing to do
164  }
165  
166  AForm::GradeTooLowException::GradeTooLowException(std::string const& msg)
167  	: mMessage("AForm::GradeTooLowException: " + msg)
168  {
169  	// nothing to do
170  }
171  
172  AForm::GradeTooLowException::GradeTooLowException
173  (GradeTooLowException const& target)
174  	: mMessage(target.getMessage())
175  {
176  	// nothing to do
177  }
178  
179  AForm::GradeTooLowException&
180  AForm::GradeTooLowException::operator=
181  (GradeTooLowException const& target)
182  {
183  	if (this != &target)
184  	{
185  		this->mMessage = target.getMessage();
186  	}
187  	return *this;
188  }
189  
190  AForm::GradeTooLowException::~GradeTooLowException() throw()
191  {
192  	// nothing to do
193  }
194  
195  /* ************************************************************************** */
196  /*                   GradeTooLowException : Getter Function                   */
197  /* ************************************************************************** */
198  std::string const&	AForm::GradeTooLowException::getMessage() const
199  {
200  	return this->mMessage;
201  }
202  
203  /* ************************************************************************** */
204  /*                  GradeTooLowException : Function Override                  */
205  /* ************************************************************************** */
206  const char*	AForm::GradeTooLowException::what() const throw()
207  {
208  	return this->getMessage().c_str();
209  }
210  
211  /* ************************************************************************** */
212  /*                              Global Function                               */
213  /* ************************************************************************** */
214  std::ostream&	operator<<(std::ostream& os, AForm const& target)
215  {
216  	std::string	isAFormSigned = target.getIsSigned() ? "true" : "false";
217  
218  	os << target.getName()
219  	<< ", AForm signed " << isAFormSigned
220  	<< ", AForm sign grade " << target.getSignGrade()
221  	<< ", AForm execution grade " << target.getExecuteGrade()
222  	<< ".";
223  	return os;
224  }
225  
226  /* ************************************************************************** */
227  /*                              Helper Functions                              */
228  /* ************************************************************************** */
229  static void	_copyConstWarning(std::string const& variable)
230  {
231  	std::cout << "INFO: You are trying to overwrite a const type variable: " <<
232  		variable << ". " << "This operation will be ignored." << std::endl;
233  }