diff options
Diffstat (limited to 'cpp05')
| -rw-r--r-- | cpp05/ex02/Form.cpp | 26 | ||||
| -rw-r--r-- | cpp05/ex02/Form.hpp | 13 | ||||
| -rw-r--r-- | cpp05/ex02/PresidentialPardonForm.cpp (renamed from cpp05/ex02/PresidentialPardonFrom.cpp) | 18 | ||||
| -rw-r--r-- | cpp05/ex02/PresidentialPardonForm.hpp (renamed from cpp05/ex02/PresidentialPardonFrom.hpp) | 20 | ||||
| -rw-r--r-- | cpp05/ex02/RobotomyRequestForm.cpp | 4 | ||||
| -rw-r--r-- | cpp05/ex02/ShrubberyCreationForm.cpp | 20 | ||||
| -rw-r--r-- | cpp05/ex02/main.cpp | 158 | ||||
| -rw-r--r-- | cpp05/ex03/Bureaucrat.cpp | 127 | ||||
| -rw-r--r-- | cpp05/ex03/Bureaucrat.hpp | 71 | ||||
| -rw-r--r-- | cpp05/ex03/Form.cpp | 150 | ||||
| -rw-r--r-- | cpp05/ex03/Form.hpp | 86 | ||||
| -rw-r--r-- | cpp05/ex03/Intern.cpp | 27 | ||||
| -rw-r--r-- | cpp05/ex03/Intern.hpp | 19 | ||||
| -rw-r--r-- | cpp05/ex03/PresidentialPardonForm.cpp | 35 | ||||
| -rw-r--r-- | cpp05/ex03/PresidentialPardonForm.hpp | 34 | ||||
| -rw-r--r-- | cpp05/ex03/RobotomyRequestForm.cpp | 38 | ||||
| -rw-r--r-- | cpp05/ex03/RobotomyRequestForm.hpp | 34 | ||||
| -rw-r--r-- | cpp05/ex03/ShrubberyCreationForm.cpp | 51 | ||||
| -rw-r--r-- | cpp05/ex03/ShrubberyCreationForm.hpp | 38 | ||||
| -rw-r--r-- | cpp05/ex03/main.cpp | 255 |
20 files changed, 1172 insertions, 52 deletions
diff --git a/cpp05/ex02/Form.cpp b/cpp05/ex02/Form.cpp index 8f3afc0..9208f7f 100644 --- a/cpp05/ex02/Form.cpp +++ b/cpp05/ex02/Form.cpp @@ -6,7 +6,7 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/14 19:26:39 by charles #+# #+# */ -/* Updated: 2020/11/17 13:35:10 by cacharle ### ########.fr */ +/* Updated: 2020/11/17 17:09:53 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -53,7 +53,9 @@ void Form::beSigned(Bureaucrat const& b) void Form::execute(Bureaucrat const& executor) const { - if (executor.getGrade() <= m_gradeExecute && m_signed) + if (!m_signed) + throw Form::NoSignatureException(); + if (executor.getGrade() <= m_gradeExecute) executeUnsafe(); else throw Form::GradeTooLowException(); @@ -126,3 +128,23 @@ Form::GradeTooLowException::operator=(GradeTooLowException const& other) Form::GradeTooLowException::~GradeTooLowException() throw() {} char const* Form::GradeTooLowException::what() const throw() { return "Grade is too low for form"; } + +/////////////////////////////////////////////////////////////////////////////// +// Exception signature +/////////////////////////////////////////////////////////////////////////////// + +Form::NoSignatureException::NoSignatureException() : std::exception() {} + +Form::NoSignatureException::NoSignatureException(NoSignatureException const& other) + : std::exception(other) {} + +Form::NoSignatureException& +Form::NoSignatureException::operator=(NoSignatureException const& other) +{ + std::exception::operator=(other); + return *this; +} + +Form::NoSignatureException::~NoSignatureException() throw() {} + +char const* Form::NoSignatureException::what() const throw() { return "No signature"; } diff --git a/cpp05/ex02/Form.hpp b/cpp05/ex02/Form.hpp index a371426..78828da 100644 --- a/cpp05/ex02/Form.hpp +++ b/cpp05/ex02/Form.hpp @@ -6,7 +6,7 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/14 19:19:45 by charles #+# #+# */ -/* Updated: 2020/11/17 12:56:36 by cacharle ### ########.fr */ +/* Updated: 2020/11/17 17:09:15 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -68,6 +68,17 @@ private: ~GradeTooLowException() throw(); virtual char const* what() const throw(); }; + + class NoSignatureException : public std::exception + { + public: + NoSignatureException(); + NoSignatureException(NoSignatureException const& other); + NoSignatureException& operator=(NoSignatureException const& other); + ~NoSignatureException() throw(); + virtual char const* what() const throw(); + }; + }; std::ostream& operator<<(std::ostream& out, Form const& f); diff --git a/cpp05/ex02/PresidentialPardonFrom.cpp b/cpp05/ex02/PresidentialPardonForm.cpp index 46e2d3c..3855841 100644 --- a/cpp05/ex02/PresidentialPardonFrom.cpp +++ b/cpp05/ex02/PresidentialPardonForm.cpp @@ -1,35 +1,35 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* PresidentialPardonFrom.cpp :+: :+: :+: */ +/* PresidentialPardonForm.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/11/17 13:01:25 by cacharle #+# #+# */ -/* Updated: 2020/11/17 13:04:10 by cacharle ### ########.fr */ +/* Updated: 2020/11/17 17:02:14 by charles ### ########.fr */ /* */ /* ************************************************************************** */ -#include "PresidentialPardonFrom.hpp" +#include "PresidentialPardonForm.hpp" -PresidentialPardonFrom::PresidentialPardonFrom(std::string const& target) +PresidentialPardonForm::PresidentialPardonForm(std::string const& target) : Form("presidential pardon", 25, 5), m_target(target) {} -PresidentialPardonFrom::PresidentialPardonFrom(const PresidentialPardonFrom& other) +PresidentialPardonForm::PresidentialPardonForm(const PresidentialPardonForm& other) : Form(other) { *this = other; } -PresidentialPardonFrom& PresidentialPardonFrom::operator=(const PresidentialPardonFrom& other) +PresidentialPardonForm& PresidentialPardonForm::operator=(const PresidentialPardonForm& other) { Form::operator=(other); m_target = other.m_target; return *this; } -PresidentialPardonFrom::~PresidentialPardonFrom() {} +PresidentialPardonForm::~PresidentialPardonForm() {} -void PresidentialPardonFrom::executeUnsafe() const +void PresidentialPardonForm::executeUnsafe() const { std::cout << m_target << " has been pardoned by Zafod Beeblebrox" << std::endl; } -PresidentialPardonFrom::PresidentialPardonFrom() : Form("", 0, 0) {} +PresidentialPardonForm::PresidentialPardonForm() : Form("", 0, 0) {} diff --git a/cpp05/ex02/PresidentialPardonFrom.hpp b/cpp05/ex02/PresidentialPardonForm.hpp index 3993685..fcf8a70 100644 --- a/cpp05/ex02/PresidentialPardonFrom.hpp +++ b/cpp05/ex02/PresidentialPardonForm.hpp @@ -1,31 +1,31 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* PresidentialPardonFrom.hpp :+: :+: :+: */ +/* PresidentialPardonForm.hpp :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/10/19 13:42:46 by cacharle #+# #+# */ -/* Updated: 2020/11/17 12:57:57 by cacharle ### ########.fr */ +/* Updated: 2020/11/17 17:02:03 by charles ### ########.fr */ /* */ /* ************************************************************************** */ -#ifndef PRESIDENTIALPARDONFROM_HPP -# define PRESIDENTIALPARDONFROM_HPP +#ifndef PRESIDENTIALPARDONFORM_HPP +# define PRESIDENTIALPARDONFORM_HPP # include "Form.hpp" -class PresidentialPardonFrom : public Form +class PresidentialPardonForm : public Form { public: - PresidentialPardonFrom(const PresidentialPardonFrom& other); - PresidentialPardonFrom& operator=(const PresidentialPardonFrom& other); - ~PresidentialPardonFrom(); + PresidentialPardonForm(const PresidentialPardonForm& other); + PresidentialPardonForm& operator=(const PresidentialPardonForm& other); + ~PresidentialPardonForm(); - PresidentialPardonFrom(std::string const& target); + PresidentialPardonForm(std::string const& target); private: - PresidentialPardonFrom(); + PresidentialPardonForm(); virtual void executeUnsafe() const; std::string m_target; diff --git a/cpp05/ex02/RobotomyRequestForm.cpp b/cpp05/ex02/RobotomyRequestForm.cpp index 113bcd1..ed05d5f 100644 --- a/cpp05/ex02/RobotomyRequestForm.cpp +++ b/cpp05/ex02/RobotomyRequestForm.cpp @@ -6,14 +6,14 @@ /* By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/10/19 13:45:45 by cacharle #+# #+# */ -/* Updated: 2020/11/17 13:27:05 by cacharle ### ########.fr */ +/* Updated: 2020/11/17 17:20:03 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "RobotomyRequestForm.hpp" RobotomyRequestForm::RobotomyRequestForm(std::string const& target) - : Form("robotomy request", 145, 137), m_target(target) {} + : Form("robotomy request", 72, 45), m_target(target) {} RobotomyRequestForm::RobotomyRequestForm(const RobotomyRequestForm& other) : Form(other) { *this = other; } diff --git a/cpp05/ex02/ShrubberyCreationForm.cpp b/cpp05/ex02/ShrubberyCreationForm.cpp index 70bc570..262f166 100644 --- a/cpp05/ex02/ShrubberyCreationForm.cpp +++ b/cpp05/ex02/ShrubberyCreationForm.cpp @@ -6,7 +6,7 @@ /* By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/10/19 13:45:45 by cacharle #+# #+# */ -/* Updated: 2020/11/17 16:36:09 by charles ### ########.fr */ +/* Updated: 2020/11/17 17:15:43 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -36,15 +36,15 @@ void ShrubberyCreationForm::executeUnsafe() const return; } file << - " ## " - " #### " - " ###### " - " ########## " - " ############## " - " ######ntm####### " - " ################### " - " ##################### " - " |___| "; + " ## \n" + " #### \n" + " ###### \n" + " ########## \n" + " ############## \n" + " ######ntm####### \n" + " ################### \n" + " ##################### \n" + " |___| \n"; file.close(); } diff --git a/cpp05/ex02/main.cpp b/cpp05/ex02/main.cpp index ae272c9..0f3a893 100644 --- a/cpp05/ex02/main.cpp +++ b/cpp05/ex02/main.cpp @@ -6,7 +6,7 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/14 18:13:05 by charles #+# #+# */ -/* Updated: 2020/11/17 16:35:40 by charles ### ########.fr */ +/* Updated: 2020/11/17 17:50:20 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,9 @@ #include <ctime> #include <cstdlib> #include "Bureaucrat.hpp" +#include "ShrubberyCreationForm.hpp" +#include "RobotomyRequestForm.hpp" +#include "PresidentialPardonForm.hpp" int main() { @@ -78,32 +81,161 @@ int main() } } - // Bureaucrat c("baraucrat", 10); - // std::cout << "############### SIGN FORM" << std::endl; - // Form f4("foo", 150, 1); c.signForm(f4); - // Form f3("foo", 10, 1); c.signForm(f3); - // Form f2("foo", 9, 1); c.signForm(f2); - // Form f1("foo", 1, 1); c.signForm(f1); - } + { + std::cout << "############### SHRUBERRY SIGN FORM" << std::endl; + ShrubberyCreationForm sh("shi"); + Bureaucrat c1("foo", 150); c1.signForm(sh); + Bureaucrat c2("foo", 146); c2.signForm(sh); + Bureaucrat c3("foo", 145); c3.signForm(sh); + Bureaucrat c4("foo", 1); c4.signForm(sh); + } + { + std::cout << "############### PRESIDENTIAL SIGN FORM" << std::endl; + PresidentialPardonForm pr("fu"); + Bureaucrat c1("foo", 150); c1.signForm(pr); + Bureaucrat c2("foo", 26); c2.signForm(pr); + Bureaucrat c3("foo", 25); c3.signForm(pr); + Bureaucrat c4("foo", 1); c4.signForm(pr); + } + { + std::cout << "############### ROBOTOMY SIGN FORM" << std::endl; + RobotomyRequestForm ro("mi"); + Bureaucrat c1("foo", 150); c1.signForm(ro); + Bureaucrat c2("foo", 73); c2.signForm(ro); + Bureaucrat c3("foo", 72); c3.signForm(ro); + Bureaucrat c4("foo", 1); c4.signForm(ro); + } + { + std::cout << "############### SHRUBERRY EXECUTE FORM" << std::endl; + ShrubberyCreationForm sh("shi"); + sh.beSigned(Bureaucrat("foo", 1)); + Bureaucrat c1("foo", 150); c1.executeForm(sh); + Bureaucrat c2("foo", 138); c2.executeForm(sh); + Bureaucrat c3("foo", 137); c3.executeForm(sh); + Bureaucrat c4("foo", 1); c4.executeForm(sh); + } + { + std::cout << "############### PRESIDENTIAL SIGN FORM" << std::endl; + PresidentialPardonForm pr("fu"); + pr.beSigned(Bureaucrat("foo", 1)); + Bureaucrat c1("foo", 150); c1.executeForm(pr); + Bureaucrat c2("foo", 6); c2.executeForm(pr); + Bureaucrat c3("foo", 5); c3.executeForm(pr); + Bureaucrat c4("foo", 1); c4.executeForm(pr); + } + { + std::cout << "############### ROBOTOMY SIGN FORM" << std::endl; + RobotomyRequestForm ro("mi"); + ro.beSigned(Bureaucrat("foo", 1)); + Bureaucrat c1("foo", 150); c1.executeForm(ro); + Bureaucrat c2("foo", 46); c2.executeForm(ro); + Bureaucrat c3("foo", 45); c3.executeForm(ro); + Bureaucrat c4("foo", 1); c4.executeForm(ro); + } + } + std::cout << std::endl; { - std::cout << "================= SHRUBERRY CREATION =================" << std::endl; - + std::cout << "================= SHRUBBERY CREATION =================" << std::endl; + ShrubberyCreationForm sh("home"); + ShrubberyCreationForm sh2(sh); + ShrubberyCreationForm sh3("SHOULD NOT BE PRINTED"); + sh3 = sh; + std::cout << sh; + std::cout << sh2; + std::cout << sh3; + + try { sh.execute(Bureaucrat("", 1)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + + try { sh.beSigned(Bureaucrat("foo", 1)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + try { sh.beSigned(Bureaucrat("foo", 145)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + try { sh.beSigned(Bureaucrat("foo", 146)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + try { sh.beSigned(Bureaucrat("foo", 150)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + std::cout << sh; + + try { sh.execute(Bureaucrat("bar", 1)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + try { sh.execute(Bureaucrat("bar", 137)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + try { sh.execute(Bureaucrat("bar", 138)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + try { sh.execute(Bureaucrat("bar", 150)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } } std::cout << std::endl; - { std::cout << "================= PRESIDENTIAL PARDON =================" << std::endl; + PresidentialPardonForm pr("Didier"); + PresidentialPardonForm pr2(pr); + PresidentialPardonForm pr3("SHOULD NOT BE PRINTED"); + pr3 = pr; + std::cout << pr; + std::cout << pr2; + std::cout << pr3; + + try { pr.execute(Bureaucrat("", 1)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + + try { pr.beSigned(Bureaucrat("foo", 1)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + try { pr.beSigned(Bureaucrat("foo", 25)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + try { pr.beSigned(Bureaucrat("foo", 26)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + try { pr.beSigned(Bureaucrat("foo", 150)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + std::cout << pr; + + try { pr.execute(Bureaucrat("bar", 1)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + try { pr.execute(Bureaucrat("bar", 5)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + try { pr.execute(Bureaucrat("bar", 6)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + try { pr.execute(Bureaucrat("bar", 150)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } } std::cout << std::endl; - { std::cout << "================= ROBOTOMY REQUEST =================" << std::endl; + RobotomyRequestForm ro("Jonathan"); + RobotomyRequestForm ro2(ro); + RobotomyRequestForm ro3("SHOULD NOT BE PRINTED"); + ro3 = ro; + std::cout << ro; + std::cout << ro2; + std::cout << ro3; + + try { ro.execute(Bureaucrat("", 1)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + + try { ro.beSigned(Bureaucrat("foo", 1)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + try { ro.beSigned(Bureaucrat("foo", 72)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + try { ro.beSigned(Bureaucrat("foo", 73)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + try { ro.beSigned(Bureaucrat("foo", 150)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + std::cout << ro; + + try { ro.execute(Bureaucrat("bar", 1)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + try { ro.execute(Bureaucrat("bar", 45)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + try { ro.execute(Bureaucrat("bar", 46)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + try { ro.execute(Bureaucrat("bar", 150)); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } } - return 0; } diff --git a/cpp05/ex03/Bureaucrat.cpp b/cpp05/ex03/Bureaucrat.cpp new file mode 100644 index 0000000..64bbd25 --- /dev/null +++ b/cpp05/ex03/Bureaucrat.cpp @@ -0,0 +1,127 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Bureaucrat.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/14 18:08:13 by charles #+# #+# */ +/* Updated: 2020/11/17 13:37:49 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Bureaucrat.hpp" + +Bureaucrat::Bureaucrat(Bureaucrat const& other) { *this = other; } + +Bureaucrat& Bureaucrat::operator=(Bureaucrat const& other) +{ + m_name = other.m_name; + m_grade = other.m_grade; + return *this; +} + +Bureaucrat::~Bureaucrat() {} + +Bureaucrat::Bureaucrat(std::string const& name, int grade) + : m_name(name), m_grade(grade) +{ + checkGrade(); +} + +std::string const& Bureaucrat::getName() const { return m_name; } +int Bureaucrat::getGrade() const { return m_grade; } + +void Bureaucrat::incrementGrade() +{ + m_grade--; + checkGrade(); +} + +void Bureaucrat::decrementGrade() +{ + m_grade++; + checkGrade(); +} + +void Bureaucrat::signForm(Form& form) +{ + try + { + form.beSigned(*this); + std::cout << m_name << " signs " << form.getName() << std::endl; + } + catch (std::exception &e) + { + std::cout << m_name << " cannot sign " << form.getName() << " " << e.what() << std::endl; + } +} + +void Bureaucrat::executeForm(Form& form) +{ + try + { + form.execute(*this); + std::cout << m_name << " executes " << form.getName() << std::endl; + } + catch (std::exception &e) + { + std::cout << m_name << " cannot execute " << form.getName() << " " << e.what() << std::endl; + } +} + +void Bureaucrat::checkGrade() +{ + if (m_grade > 150) + throw Bureaucrat::GradeTooLowException(); + if (m_grade < 1) + throw Bureaucrat::GradeTooHighException(); +} + +std::ostream& operator<<(std::ostream& out, Bureaucrat const& b) +{ + std::cout << b.getName() << ", bureaucrat grade " << b.getGrade() << std::endl; + return out; +} + +/////////////////////////////////////////////////////////////////////////////// +// Exception grade too high +/////////////////////////////////////////////////////////////////////////////// + +Bureaucrat::GradeTooHighException::GradeTooHighException() : std::exception() {} + +Bureaucrat::GradeTooHighException::GradeTooHighException(GradeTooHighException const& other) + : std::exception(other) {} + +Bureaucrat::GradeTooHighException& +Bureaucrat::GradeTooHighException::operator=(GradeTooHighException const& other) +{ + std::exception::operator=(other); + return *this; +} + +Bureaucrat::GradeTooHighException::~GradeTooHighException() throw() {} + +char const* Bureaucrat::GradeTooHighException::what() const throw() { return "Grade is too high"; } + +/////////////////////////////////////////////////////////////////////////////// +// Exception grade too low +/////////////////////////////////////////////////////////////////////////////// + +Bureaucrat::GradeTooLowException::GradeTooLowException() : std::exception() {} + +Bureaucrat::GradeTooLowException::GradeTooLowException(GradeTooLowException const& other) + : std::exception(other) {} + +Bureaucrat::GradeTooLowException& +Bureaucrat::GradeTooLowException::operator=(GradeTooLowException const& other) +{ + std::exception::operator=(other); + return *this; +} + +Bureaucrat::GradeTooLowException::~GradeTooLowException() throw() {} + +char const* Bureaucrat::GradeTooLowException::what() const throw() { return "Grade is too low"; } + +Bureaucrat::Bureaucrat() {} diff --git a/cpp05/ex03/Bureaucrat.hpp b/cpp05/ex03/Bureaucrat.hpp new file mode 100644 index 0000000..8e44af0 --- /dev/null +++ b/cpp05/ex03/Bureaucrat.hpp @@ -0,0 +1,71 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Bureaucrat.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/14 18:04:32 by charles #+# #+# */ +/* Updated: 2020/11/17 13:36:56 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef BUREAUCRAT_HPP +# define BUREAUCRAT_HPP + +# include <iostream> +# include <exception> +# include "Form.hpp" + +class Form; + +class Bureaucrat +{ +public: + Bureaucrat(Bureaucrat const& other); + Bureaucrat& operator=(Bureaucrat const& other); + ~Bureaucrat(); + + Bureaucrat(std::string const& name, int grade); + + std::string const& getName() const; + int getGrade() const; + void incrementGrade(); + void decrementGrade(); + + void signForm(Form& form); + + void executeForm(Form& form); + +private: + Bureaucrat(); + void checkGrade(); + + std::string m_name; + int m_grade; + + class GradeTooHighException : public std::exception + { + public: + GradeTooHighException(); + GradeTooHighException(GradeTooHighException const& other); + GradeTooHighException& operator=(GradeTooHighException const& other); + ~GradeTooHighException() throw(); + virtual char const* what() const throw(); + }; + + class GradeTooLowException : public std::exception + { + public: + GradeTooLowException(); + GradeTooLowException(GradeTooLowException const& other); + GradeTooLowException& operator=(GradeTooLowException const& other); + ~GradeTooLowException() throw(); + virtual char const* what() const throw(); + }; + +}; + +std::ostream& operator<<(std::ostream& out, Bureaucrat const& b); + +#endif diff --git a/cpp05/ex03/Form.cpp b/cpp05/ex03/Form.cpp new file mode 100644 index 0000000..9208f7f --- /dev/null +++ b/cpp05/ex03/Form.cpp @@ -0,0 +1,150 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Form.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/14 19:26:39 by charles #+# #+# */ +/* Updated: 2020/11/17 17:09:53 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Form.hpp" + + +Form::Form(Form const& other) + : m_name(other.m_name), + m_signed(other.m_signed), + m_gradeSign(other.m_gradeSign), + m_gradeExecute(other.m_gradeExecute) +{} + +// The other attributes are const (asked by subject) +Form& Form::operator=(Form const& other) +{ + m_signed = other.m_signed; + return *this; +} + +Form::~Form() {} + +Form::Form(std::string const& name, int gradeSign, int gradeExecute) + : m_name(name), + m_signed(false), + m_gradeSign(gradeSign), + m_gradeExecute(gradeExecute) +{ + checkGrade(); +} + +std::string const& Form::getName() const { return m_name; } +bool Form::getSigned() const { return m_signed; } +int Form::getGradeSign() const { return m_gradeSign; } +int Form::getGradeExecute() const { return m_gradeExecute; } + +void Form::beSigned(Bureaucrat const& b) +{ + if (b.getGrade() <= m_gradeSign) + m_signed = true; + else + throw Form::GradeTooLowException(); +} + +void Form::execute(Bureaucrat const& executor) const +{ + if (!m_signed) + throw Form::NoSignatureException(); + if (executor.getGrade() <= m_gradeExecute) + executeUnsafe(); + else + throw Form::GradeTooLowException(); +} + +void Form::checkGrade() +{ + if (m_gradeSign > 150) + throw Form::GradeTooLowException(); + if (m_gradeSign < 1) + throw Form::GradeTooHighException(); + if (m_gradeExecute > 150) + throw Form::GradeTooLowException(); + if (m_gradeExecute < 1) + throw Form::GradeTooHighException(); +} + +std::ostream& operator<<(std::ostream& out, Form const& f) +{ + out << f.getName() << " is " + << (f.getSigned() ? "" : "not ") << "signed and needs at least " + << f.getGradeSign() << " to be signed and " + << f.getGradeExecute() << " to be executed" << std::endl; + return out; +} +// compilation error if const members are not initialized +Form::Form() + : m_name(""), + m_signed(false), + m_gradeSign(0), + m_gradeExecute(0) +{} + +/////////////////////////////////////////////////////////////////////////////// +// Exception grade too high +/////////////////////////////////////////////////////////////////////////////// + +Form::GradeTooHighException::GradeTooHighException() : std::exception() {} + +Form::GradeTooHighException::GradeTooHighException(GradeTooHighException const& other) + : std::exception(other) {} + +Form::GradeTooHighException& +Form::GradeTooHighException::operator=(GradeTooHighException const& other) +{ + std::exception::operator=(other); + return *this; +} + +Form::GradeTooHighException::~GradeTooHighException() throw() {} + +char const* Form::GradeTooHighException::what() const throw() { return "Grade is too high for form"; } + +/////////////////////////////////////////////////////////////////////////////// +// Exception grade too high +/////////////////////////////////////////////////////////////////////////////// + +Form::GradeTooLowException::GradeTooLowException() : std::exception() {} + +Form::GradeTooLowException::GradeTooLowException(GradeTooLowException const& other) + : std::exception(other) {} + +Form::GradeTooLowException& +Form::GradeTooLowException::operator=(GradeTooLowException const& other) +{ + std::exception::operator=(other); + return *this; |
