diff options
| author | Charles Cabergs <me@cacharle.xyz> | 2020-11-17 19:02:35 +0100 |
|---|---|---|
| committer | Charles Cabergs <me@cacharle.xyz> | 2020-11-17 19:02:35 +0100 |
| commit | 452e6bfa7bb4bca75dc4659bf9d707101b411977 (patch) | |
| tree | f4ee27a96437d6d17a89bbbbc135ae45f9f3faae /cpp05/ex03 | |
| parent | a92013c92bfcd50b0e2561280c9eaa604843ade0 (diff) | |
| download | piscine_cpp-452e6bfa7bb4bca75dc4659bf9d707101b411977.tar.gz piscine_cpp-452e6bfa7bb4bca75dc4659bf9d707101b411977.tar.bz2 piscine_cpp-452e6bfa7bb4bca75dc4659bf9d707101b411977.zip | |
Added cpp05/ex02 main, Added cpp05/ex03
Diffstat (limited to 'cpp05/ex03')
| -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 |
13 files changed, 960 insertions, 5 deletions
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; +} + +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/ex03/Form.hpp b/cpp05/ex03/Form.hpp new file mode 100644 index 0000000..78828da --- /dev/null +++ b/cpp05/ex03/Form.hpp @@ -0,0 +1,86 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Form.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/14 19:19:45 by charles #+# #+# */ +/* Updated: 2020/11/17 17:09:15 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FORM_HPP +# define FORM_HPP + +# include <iostream> +# include <exception> +# include "Bureaucrat.hpp" + +class Bureaucrat; + +class Form +{ +public: + Form(Form const& other); + Form& operator=(Form const& other); + ~Form(); + + Form(std::string const& name, int gradeSign, int gradeExecute); + + std::string const& getName() const; + bool getSigned() const; + int getGradeSign() const; + int getGradeExecute() const; + + void beSigned(Bureaucrat const& b); + + void execute(Bureaucrat const& executor) const; + +protected: + virtual void executeUnsafe() const = 0; + +private: + Form(); + void checkGrade(); + + std::string const m_name; + bool m_signed; + int const m_gradeSign; + int const m_gradeExecute; + + 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(); + }; + + 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); + +#endif diff --git a/cpp05/ex03/Intern.cpp b/cpp05/ex03/Intern.cpp index bb3e188..3328957 100644 --- a/cpp05/ex03/Intern.cpp +++ b/cpp05/ex03/Intern.cpp @@ -6,18 +6,37 @@ /* By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/11/17 13:41:09 by cacharle #+# #+# */ -/* Updated: 2020/11/17 13:48:01 by cacharle ### ########.fr */ +/* Updated: 2020/11/17 18:08:56 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "Intern.hpp" Intern::Intern() {} -Intern::Intern(const Intern& other) {} -Intern& Intern::operator=(const Intern& other) {} +Intern::Intern(const Intern& other) { (void)other; } +Intern& Intern::operator=(const Intern& other) { (void)other; return *this; } Intern::~Intern() {} -Form *Intern::makeForm(std::string const& name, std::string const& target) +Form *Intern::makeShrubberyCreationForm(std::string const& target) + { return new ShrubberyCreationForm(target); } + +Form *Intern::makePresidentialPardonForm(std::string const& target) + { return new PresidentialPardonForm(target); } + +Form *Intern::makeRobotomyRequestForm(std::string const& target) + { return new RobotomyRequestForm(target); } + +Intern::makeFormEntry Intern::makeFormDispatch[] = { + {"shrubbery creation", &Intern::makeShrubberyCreationForm}, + {"presidential pardon", &Intern::makePresidentialPardonForm}, + {"robotomy request", &Intern::makeRobotomyRequestForm}, +}; +Form *Intern::makeForm(std::string const& name, std::string const& target) +{ + for (size_t i = 0; i < sizeof(Intern::makeFormDispatch) / sizeof(Intern::makeFormEntry); i++) + if ((this->makeFormDispatch[i]).name == name) + return (this->*(makeFormDispatch[i].func))(target); + return NULL; } diff --git a/cpp05/ex03/Intern.hpp b/cpp05/ex03/Intern.hpp index 60c2ac8..c27874a 100644 --- a/cpp05/ex03/Intern.hpp +++ b/cpp05/ex03/Intern.hpp @@ -6,13 +6,18 @@ /* By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/11/17 13:39:59 by cacharle #+# #+# */ -/* Updated: 2020/11/17 13:48:08 by cacharle ### ########.fr */ +/* Updated: 2020/11/17 17:58:02 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef INTERN_HPP # define INTERN_HPP +# include "Form.hpp" +# include "ShrubberyCreationForm.hpp" +# include "PresidentialPardonForm.hpp" +# include "RobotomyRequestForm.hpp" + class Intern { public: @@ -22,7 +27,19 @@ public: ~Intern(); Form *makeForm(std::string const& name, std::string const& target); + private: + Form *makeShrubberyCreationForm(std::string const& target); + Form *makePresidentialPardonForm(std::string const& target); + Form *makeRobotomyRequestForm(std::string const& target); + + typedef Form* (Intern::* makeFormFunc)(std::string const&); + struct makeFormEntry + { + std::string const name; + makeFormFunc const func; + }; + static makeFormEntry makeFormDispatch[]; }; #endif diff --git a/cpp05/ex03/PresidentialPardonForm.cpp b/cpp05/ex03/PresidentialPardonForm.cpp new file mode 100644 index 0000000..3855841 --- /dev/null +++ b/cpp05/ex03/PresidentialPardonForm.cpp @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* PresidentialPardonForm.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/11/17 13:01:25 by cacharle #+# #+# */ +/* Updated: 2020/11/17 17:02:14 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "PresidentialPardonForm.hpp" + +PresidentialPardonForm::PresidentialPardonForm(std::string const& target) + : Form("presidential pardon", 25, 5), m_target(target) {} + +PresidentialPardonForm::PresidentialPardonForm(const PresidentialPardonForm& other) + : Form(other) { *this = other; } + +PresidentialPardonForm& PresidentialPardonForm::operator=(const PresidentialPardonForm& other) +{ + Form::operator=(other); + m_target = other.m_target; + return *this; +} + +PresidentialPardonForm::~PresidentialPardonForm() {} + +void PresidentialPardonForm::executeUnsafe() const +{ + std::cout << m_target << " has been pardoned by Zafod Beeblebrox" << std::endl; +} + +PresidentialPardonForm::PresidentialPardonForm() : Form("", 0, 0) {} diff --git a/cpp05/ex03/PresidentialPardonForm.hpp b/cpp05/ex03/PresidentialPardonForm.hpp new file mode 100644 index 0000000..fcf8a70 --- /dev/null +++ b/cpp05/ex03/PresidentialPardonForm.hpp @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* PresidentialPardonForm.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/10/19 13:42:46 by cacharle #+# #+# */ +/* Updated: 2020/11/17 17:02:03 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef PRESIDENTIALPARDONFORM_HPP +# define PRESIDENTIALPARDONFORM_HPP + +# include "Form.hpp" + +class PresidentialPardonForm : public Form +{ +public: + PresidentialPardonForm(const PresidentialPardonForm& other); + PresidentialPardonForm& operator=(const PresidentialPardonForm& other); + ~PresidentialPardonForm(); + + PresidentialPardonForm(std::string const& target); + +private: + PresidentialPardonForm(); + virtual void executeUnsafe() const; + + std::string m_target; +}; + +#endif diff --git a/cpp05/ex03/RobotomyRequestForm.cpp b/cpp05/ex03/RobotomyRequestForm.cpp new file mode 100644 index 0000000..ed05d5f --- /dev/null +++ b/cpp05/ex03/RobotomyRequestForm.cpp @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* RobotomyRequestForm.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/10/19 13:45:45 by cacharle #+# #+# */ +/* Updated: 2020/11/17 17:20:03 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "RobotomyRequestForm.hpp" + +RobotomyRequestForm::RobotomyRequestForm(std::string const& target) + : Form("robotomy request", 72, 45), m_target(target) {} + +RobotomyRequestForm::RobotomyRequestForm(const RobotomyRequestForm& other) + : Form(other) { *this = other; } + +RobotomyRequestForm& RobotomyRequestForm::operator=(const RobotomyRequestForm& other) +{ + Form::operator=(other); + m_target = other.m_target; + return *this; +} + +RobotomyRequestForm::~RobotomyRequestForm() {} + +void RobotomyRequestForm::executeUnsafe() const +{ + if (rand() % 100 <= 50) + std::cout << m_target << " has successfully been robotomized" << std::endl; + else + std::cout << m_target << " couldn't be robotomized" << std::endl; +} + +RobotomyRequestForm::RobotomyRequestForm() : Form("", 0, 0) {} diff --git a/cpp05/ex03/RobotomyRequestForm.hpp b/cpp05/ex03/RobotomyRequestForm.hpp new file mode 100644 index 0000000..a851b04 --- /dev/null +++ b/cpp05/ex03/RobotomyRequestForm.hpp @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* RobotomyRequestForm.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/10/19 13:43:37 by cacharle #+# #+# */ +/* Updated: 2020/11/17 13:27:32 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef ROBOTOMYREQUESTFORM_HPP +# define ROBOTOMYREQUESTFORM_HPP + +# include "Form.hpp" + +class RobotomyRequestForm : public Form +{ +public: + RobotomyRequestForm(const RobotomyRequestForm& other); + RobotomyRequestForm& operator=(const RobotomyRequestForm& other); + ~RobotomyRequestForm(); + + RobotomyRequestForm(std::string const& target); + +private: + RobotomyRequestForm(); + virtual void executeUnsafe() const; + + std::string m_target; +}; + +#endif diff --git a/cpp05/ex03/ShrubberyCreationForm.cpp b/cpp05/ex03/ShrubberyCreationForm.cpp new file mode 100644 index 0000000..262f166 --- /dev/null +++ b/cpp05/ex03/ShrubberyCreationForm.cpp @@ -0,0 +1,51 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ShrubberyCreationForm.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/10/19 13:45:45 by cacharle #+# #+# */ +/* Updated: 2020/11/17 17:15:43 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ShrubberyCreationForm.hpp" + +ShrubberyCreationForm::ShrubberyCreationForm(std::string const& target) + : Form("shrubbery creation", 145, 137), m_target(target) {} + +ShrubberyCreationForm::ShrubberyCreationForm(const ShrubberyCreationForm& other) + : Form(other) { *this = other; } + +ShrubberyCreationForm& ShrubberyCreationForm::operator=(const ShrubberyCreationForm& other) +{ + Form::operator=(other); + m_target = other.m_target; + return *this; +} + +ShrubberyCreationForm::~ShrubberyCreationForm() {} + +void ShrubberyCreationForm::executeUnsafe() const +{ + std::ofstream file(m_target + "_shrubbery"); + if (!file.is_open()) + { + std::cerr << "Error: " << m_target + "_shrubbery" << ": " << std::strerror(errno) << std::endl; + return; + } + file << + " ## \n" + " #### \n" + " ###### \n" + " ########## \n" + " ############## \n" + " ######ntm####### \n" + " ################### \n" + " ##################### \n" + " |___| \n"; + file.close(); +} + +ShrubberyCreationForm::ShrubberyCreationForm() : Form("", 0, 0) {} diff --git a/cpp05/ex03/ShrubberyCreationForm.hpp b/cpp05/ex03/ShrubberyCreationForm.hpp new file mode 100644 index 0000000..4799d9d --- /dev/null +++ b/cpp05/ex03/ShrubberyCreationForm.hpp @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ShrubberyCreationForm.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/10/19 13:43:27 by cacharle #+# #+# */ +/* Updated: 2020/11/17 16:37:56 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef SHRUBBERYCREATIONFORM_HPP +# define SHRUBBERYCREATIONFORM_HPP + +# include <string> +# include <cstring> +# include <fstream> +# include <iostream> +# include "Form.hpp" + +class ShrubberyCreationForm : public Form +{ +public: + ShrubberyCreationForm(const ShrubberyCreationForm& other); + ShrubberyCreationForm& operator=(const ShrubberyCreationForm& other); + ~ShrubberyCreationForm(); + + ShrubberyCreationForm(std::string const& target); + +private: + ShrubberyCreationForm(); + virtual void executeUnsafe() const; + + std::string m_target; +}; + +#endif diff --git a/cpp05/ex03/main.cpp b/cpp05/ex03/main.cpp new file mode 100644 index 0000000..aceb722 --- /dev/null +++ b/cpp05/ex03/main.cpp @@ -0,0 +1,255 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/14 18:13:05 by charles #+# #+# */ +/* Updated: 2020/11/17 19:02:02 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include <fstream> +#include <ctime> +#include <cstdlib> +#include "Bureaucrat.hpp" +#include "ShrubberyCreationForm.hpp" +#include "RobotomyRequestForm.hpp" +#include "PresidentialPardonForm.hpp" +#include "Intern.hpp" + +int main() +{ + int seed; + std::ifstream devRandom("/dev/random"); + if (devRandom.is_open()) + { + devRandom.read((char*)&seed, sizeof(int)); + devRandom.close(); + } + else + { + seed = time(NULL); + } + srand(seed); + + { + std::cout << "=============== BUREAUCRAT ===============" << std::endl; + std::cout << "############### CREATION" << std::endl; + try { Bureaucrat bu("YO", 0); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + try { Bureaucrat bu("YO2", 151); } + catch (std::exception& e) { std::cout << e.what() << std::endl; } + + Bureaucrat yep("YEP", 1); + Bureaucrat yep2(yep); + Bureaucrat yep3("SHOULD NOT BE PRINTED", 42); + yep3 = yep; + std::cout << yep; + std::cout << yep2; + std::cout << yep3; + + std::cout << "############### DECREMENT" << std::endl; + Bureaucrat a("jean", 140); + while (true) + { + try + { + a.decrementGrade(); + std::cout << a; + } + catch (std::exception& e) + { + std::cout << e.what() << std::endl; + break; + } + } + + std::cout << "############### INCREMENT" << std::endl; + Bureaucrat b("didier", 10); + while (true) + { + try + { + b.incrementGrade(); + std::cout << b; + } + catch (std::exception& e) + { + std::cout << e.what() << std::endl; + break; + } + } + + { + 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 << "================= 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; } + } + + std::cout << std::endl; + { + std::cout << "================= INTERN =================" << std::endl; + Intern in; + Form *sh = in.makeForm("shrubbery creation", "yo"); + Form *pr = in.makeForm("presidential pardon", "ya"); + Form *ro = in.makeForm("robotomy request", "yu"); + Form *nu = in.makeForm("foo", "yu"); + std::cout << nu << std::endl; + std::cout << *sh; + std::cout << *pr; + std::cout << *ro; + } + return 0; +} |
