commit 224ed0cf99d4a4a082a53244d050e331a5091915 Author: yosyo Date: Sat Aug 2 13:51:40 2025 +0200 「🎉」 init: hello world ! diff --git a/ex00/Bureaucrat.cpp b/ex00/Bureaucrat.cpp new file mode 100644 index 0000000..2150f54 --- /dev/null +++ b/ex00/Bureaucrat.cpp @@ -0,0 +1,95 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Bureaucrat.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mmoussou + +void _log(std::string emoji, std::string what, std::string who, std::string str) +{ +#ifdef VERBOSE + if (who.empty()) + std::cout << "「" << emoji << "」" << what << ": " << str << std::endl; + else + std::cout << "「" << emoji << "」" << what << "(" << who << "): " << str + << std::endl; +#else + (void)emoji, (void)what, (void)who, (void)str; +#endif +} + +Bureaucrat::Bureaucrat(void) + : _name("kanel"), _grade(69) +{ +_log("➕", "Bureaucrat", "", "default constructor called"); +} + +Bureaucrat::Bureaucrat(const Bureaucrat &cpy) +{ +_log("➕", "Bureaucrat", "", "copy constructor called"); + if (&cpy != this) + { + _name = cpy.getName(); + _grade = cpy.getGrade(); + } +} + +Bureaucrat::Bureaucrat(std::string name, uint8_t grade) + : _name(name), _grade(grade) +{ +_log("➕", "Bureaucrat", "", "constructor called"); +} + +const std::string &Bureaucrat::getName(void) const { return _name; } +uint8_t Bureaucrat::getGrade(void) const { return _grade; } + +Bureaucrat::~Bureaucrat(void) +{ +_log("➖", "Bureaucrat", "", "destructor called"); +} + +Bureaucrat &Bureaucrat::operator=(Bureaucrat const &cpy) +{ + if (&cpy == this) + return *this; + _grade = cpy._grade; + return *this; +} + +void Bureaucrat::IncrementGrade(void) +{ + if (_grade - 1 < MAXGRADE) + throw GradeTooHighException(); + _grade--; +} + +void Bureaucrat::DecrementGrade(void) +{ + if (_grade + 1 > MINGRADE) + throw GradeTooLowException(); + _grade++; +} + +std::ostream &operator<<(std::ostream &os, Bureaucrat &val) +{ + os << val.getName() << ", bureaucrat grade " << (int)val.getGrade(); + return os; +} + +const char *Bureaucrat::GradeTooHighException::what() const throw() +{ + return ("Grade is too high"); +} + +const char *Bureaucrat::GradeTooLowException::what() const throw() +{ + return ("Grade is too low"); +} diff --git a/ex00/Bureaucrat.hpp b/ex00/Bureaucrat.hpp new file mode 100644 index 0000000..5b83b56 --- /dev/null +++ b/ex00/Bureaucrat.hpp @@ -0,0 +1,59 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Bureaucrat.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mmoussou +#include +#include +#include +#include + +void _log(std::string emoji, std::string what, std::string who, std::string str); + +class Form; + +typedef unsigned char uint8_t; + +#define MAXGRADE 1 +#define MINGRADE 150 + +class Bureaucrat { +public: + Bureaucrat(void); + Bureaucrat(const Bureaucrat &cpy); + Bureaucrat(std::string name, uint8_t grade); + ~Bureaucrat(void); + + Bureaucrat &operator=(const Bureaucrat& other); + + const std::string &getName(void) const; + uint8_t getGrade(void) const; + + void IncrementGrade(void); + void DecrementGrade(void); + + class GradeTooHighException : public std::exception { + public: + virtual const char *what() const throw(); + }; + class GradeTooLowException : public std::exception { + public: + virtual const char *what() const throw(); + }; + +private: + std::string _name; + uint8_t _grade; +}; + +std::ostream &operator<<(std::ostream &, Bureaucrat &); diff --git a/ex00/Makefile b/ex00/Makefile new file mode 100644 index 0000000..6704a83 --- /dev/null +++ b/ex00/Makefile @@ -0,0 +1,58 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: mmoussou + +int main(void) +{ + Bureaucrat knl("kanel", 10); + + knl.DecrementGrade(); + std::cout << knl << std::endl; + knl.IncrementGrade(); + std::cout << knl << std::endl; + try + { + knl.IncrementGrade(); + knl.IncrementGrade(); + knl.IncrementGrade(); + knl.IncrementGrade(); + knl.IncrementGrade(); + knl.IncrementGrade(); + knl.IncrementGrade(); + knl.IncrementGrade(); + knl.IncrementGrade(); + knl.IncrementGrade(); + knl.IncrementGrade(); + knl.IncrementGrade(); + knl.IncrementGrade(); + knl.IncrementGrade(); + knl.IncrementGrade(); + knl.IncrementGrade(); + knl.IncrementGrade(); + } + catch (const std::exception &e) + { + std::cerr << "error: " << e.what() << std::endl; + } + std::cout << knl << std::endl; +} diff --git a/ex01/Bureaucrat.cpp b/ex01/Bureaucrat.cpp new file mode 100644 index 0000000..69798d8 --- /dev/null +++ b/ex01/Bureaucrat.cpp @@ -0,0 +1,122 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Bureaucrat.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mmoussou + +void _log(std::string emoji, std::string what, std::string who, std::string str) +{ +#ifdef VERBOSE + if (who.empty()) + std::cout << "「" << emoji << "」" << what << ": " << str << std::endl; + else + std::cout << "「" << emoji << "」" << what << "(" << who << "): " << str + << std::endl; +#else + (void)emoji, (void)what, (void)who, (void)str; +#endif +} + +Bureaucrat::Bureaucrat(void) + : _name("kanel"), _grade(150) +{ +_log("➕", "Bureaucrat", "", "default constructor called"); +} + +Bureaucrat::Bureaucrat(const Bureaucrat &cpy) +{ +_log("➕", "Bureaucrat", "", "copy constructor called"); + if (&cpy != this) + { + _name = cpy.getName(); + _grade = cpy.getGrade(); + } +} + +Bureaucrat::Bureaucrat(uint8_t grade) + : _name("kanel"), _grade(grade) +{ +_log("➕", "Bureaucrat", "", "constructor called"); +} + +Bureaucrat::Bureaucrat(std::string name) + : _name(name), _grade(150) +{ +_log("➕", "Bureaucrat", "", "constructor called"); +} + +Bureaucrat::Bureaucrat(std::string name, uint8_t grade) + : _name(name), _grade(grade) +{ +_log("➕", "Bureaucrat", "", "constructor called"); +} + +const std::string &Bureaucrat::getName(void) const { return _name; } +uint8_t Bureaucrat::getGrade(void) const { return _grade; } + +Bureaucrat::~Bureaucrat(void) +{ +_log("➖", "Bureaucrat", "", "destructor called"); +} + +void Bureaucrat::signForm(Form &f) +{ + try + { + f.beSigned(*this); + std::cout << _name << " signed " << f.getName() << std::endl; + } + catch (std::exception &e) + { + std::cout << _name << " counldn't sign " << f.getName() << " because " << e.what() << std::endl; + } +} + + +Bureaucrat &Bureaucrat::operator=(Bureaucrat const &cpy) +{ + if (&cpy == this) + return *this; + _grade = cpy._grade; + return *this; +} + +void Bureaucrat::IncrementGrade(void) +{ + if (_grade - 1 < MAXGRADE) + throw GradeTooHighException(); + _grade--; +} + +void Bureaucrat::DecrementGrade(void) +{ + if (_grade + 1 > MINGRADE) + throw GradeTooLowException(); + _grade++; +} + +std::ostream &operator<<(std::ostream &os, Bureaucrat &val) +{ + os << val.getName() << ", bureaucrat grade " << (int)val.getGrade(); + return os; +} + +const char *Bureaucrat::GradeTooHighException::what() const throw() +{ + return ("Grade is too high"); +} + +const char *Bureaucrat::GradeTooLowException::what() const throw() +{ + return ("Grade is too low"); +} diff --git a/ex01/Bureaucrat.hpp b/ex01/Bureaucrat.hpp new file mode 100644 index 0000000..f98aa1c --- /dev/null +++ b/ex01/Bureaucrat.hpp @@ -0,0 +1,63 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Bureaucrat.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mmoussou +#include +#include +#include +#include + +void _log(std::string emoji, std::string what, std::string who, std::string str); + +class Form; + +typedef unsigned char uint8_t; + +#define MAXGRADE 1 +#define MINGRADE 150 + +class Bureaucrat { +public: + Bureaucrat(void); + Bureaucrat(const Bureaucrat &cpy); + Bureaucrat(uint8_t grade); + Bureaucrat(std::string name); + Bureaucrat(std::string name, uint8_t grade); + ~Bureaucrat(void); + + Bureaucrat &operator=(const Bureaucrat& other); + + const std::string &getName(void) const; + uint8_t getGrade(void) const; + + void IncrementGrade(void); + void DecrementGrade(void); + + class GradeTooHighException : public std::exception { + public: + virtual const char *what() const throw(); + }; + class GradeTooLowException : public std::exception { + public: + virtual const char *what() const throw(); + }; + + void signForm(Form &); + +private: + std::string _name; + uint8_t _grade; +}; + +std::ostream &operator<<(std::ostream &, Bureaucrat &); diff --git a/ex01/Form.cpp b/ex01/Form.cpp new file mode 100644 index 0000000..97ce604 --- /dev/null +++ b/ex01/Form.cpp @@ -0,0 +1,78 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Form.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mmoussou b.getGrade()) { + throw GradeTooLowException(); + } + _signed = true; +} + +void Form::_gradeCheck(void) const { + if (_minForExec < MAXGRADE || _minForSign < MAXGRADE) + throw GradeTooHighException(); + if (_minForExec > MINGRADE || _minForExec > MINGRADE) + throw GradeTooLowException(); +} + +std::ostream &operator<<(std::ostream &os, Form &val) { + os << "Form: " << val.getName() + << ", minGrade to sign: " << int(val.getMinForSign()) + << ", minGrade to execute: " << int(val.getMinForExec()); + return (os); +} diff --git a/ex01/Form.hpp b/ex01/Form.hpp new file mode 100644 index 0000000..bb3ccb3 --- /dev/null +++ b/ex01/Form.hpp @@ -0,0 +1,58 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Form.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mmoussou + +void _log(std::string emoji, std::string what, std::string who, std::string str); + +typedef unsigned char uint8_t; + +#include "Bureaucrat.hpp" + +class Form { + public: + Form(void); + Form(std::string name, uint8_t minForSign, uint8_t minForExec); + Form(const Form &); + ~Form(void); + + Form &operator=(const Form &); + + class GradeTooHighException : public std::exception { + public: + virtual const char *what() const throw(); + }; + class GradeTooLowException : public std::exception { + public: + virtual const char *what() const throw(); + }; + + std::string getName(void) const; + bool getSigned(void) const; + uint8_t getMinForSign(void) const; + uint8_t getMinForExec(void) const; + + void beSigned(Bureaucrat); + + private: + std::string _name; + bool _signed; + const uint8_t _minForSign; + const uint8_t _minForExec; + + void _gradeCheck(void) const; +}; + +std::ostream &operator<<(std::ostream &, Form &); + diff --git a/ex01/Makefile b/ex01/Makefile new file mode 100644 index 0000000..03eb87f --- /dev/null +++ b/ex01/Makefile @@ -0,0 +1,58 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: mmoussou _minForExec) { + throw GradeTooLowException(); + } + std::cout << b.getName() << " executed " << this->getName() << std::endl; + _exec(b); +} + + +AForm::AForm(void) + : _name("Form"), _signed(false), _minForSign(1), _minForExec(1) +{ + _log("➕", "AForm", "", "default constructor called"); +} + +AForm::AForm(std::string name, uint8_t minForSign, uint8_t minForExec) + : _name(name), _signed(false), _minForSign(minForSign), _minForExec(minForExec) +{ + _log("➕", "AForm", "", "constructor called"); + _gradeCheck(); +} + +AForm::AForm(const AForm &f) + : _name(f.getName()), _signed(f.getSigned()), _minForSign(f.getMinForSign()), _minForExec(f.getMinForExec()) +{ + _log("➕", "Form", "", "copy constructor called"); + _gradeCheck(); +} + +AForm::~AForm(void) { _log("➖", "AForm", "", "destructor called"); } + +AForm &AForm::operator=(const AForm &f) { + if (this == &f) + return *this; + _name = f.getName(); + _signed = f.getSigned(); + return *this; +} + +const char *AForm::GradeTooHighException::what() const throw() { + return ("grade is too high"); +} + +const char *AForm::GradeTooLowException::what() const throw() { + return ("grade is too low"); +} + +const char *AForm::NotSigned::what() const throw() { + return "Form exception: Form not signed"; +} + +std::string AForm::getName(void) const { return _name; } +bool AForm::getSigned(void) const { return _signed; } +uint8_t AForm::getMinForSign(void) const { return _minForSign; } +uint8_t AForm::getMinForExec(void) const { return _minForExec; } + +void AForm::beSigned(Bureaucrat b) { + if (_minForSign > b.getGrade()) { + throw GradeTooLowException(); + } + _signed = true; +} + +void AForm::_gradeCheck(void) const { + if (_minForExec < MAXGRADE || _minForSign < MAXGRADE) + throw GradeTooHighException(); + if (_minForExec > MINGRADE || _minForExec > MINGRADE) + throw GradeTooLowException(); +} + +std::ostream &operator<<(std::ostream &os, AForm &val) { + os << "Form: " << val.getName() + << ", minGrade to sign: " << int(val.getMinForSign()) + << ", minGrade to execute: " << int(val.getMinForExec()); + return (os); +} diff --git a/ex02/AForm.hpp b/ex02/AForm.hpp new file mode 100644 index 0000000..906c261 --- /dev/null +++ b/ex02/AForm.hpp @@ -0,0 +1,65 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* AForm.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mmoussou + +void _log(std::string emoji, std::string what, std::string who, std::string str); + +typedef unsigned char uint8_t; + +#include "Bureaucrat.hpp" + +class AForm { + public: + AForm(void); + AForm(std::string name, uint8_t minForSign, uint8_t minForExec); + AForm(const AForm &); + ~AForm(void); + + AForm &operator=(const AForm &); + + class GradeTooHighException : public std::exception { + public: + virtual const char *what() const throw(); + }; + class GradeTooLowException : public std::exception { + public: + virtual const char *what() const throw(); + }; + class NotSigned : public std::exception { + public: + virtual const char *what() const throw(); + }; + + std::string getName(void) const; + bool getSigned(void) const; + uint8_t getMinForSign(void) const; + uint8_t getMinForExec(void) const; + + void execute(const Bureaucrat &) const; + + void beSigned(Bureaucrat); + + private: + std::string _name; + bool _signed; + const uint8_t _minForSign; + const uint8_t _minForExec; + + void _gradeCheck(void) const; + virtual void _exec(const Bureaucrat &) const = 0; +}; + +std::ostream &operator<<(std::ostream &, AForm &); + diff --git a/ex02/Bureaucrat.cpp b/ex02/Bureaucrat.cpp new file mode 100644 index 0000000..bc9b5b5 --- /dev/null +++ b/ex02/Bureaucrat.cpp @@ -0,0 +1,126 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Bureaucrat.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mmoussou + +void _log(std::string emoji, std::string what, std::string who, std::string str) +{ +#ifdef VERBOSE + if (who.empty()) + std::cout << "「" << emoji << "」" << what << ": " << str << std::endl; + else + std::cout << "「" << emoji << "」" << what << "(" << who << "): " << str + << std::endl; +#else + (void)emoji, (void)what, (void)who, (void)str; +#endif +} + +void Bureaucrat::executeForm(const AForm &form) const { + form.execute(*this); +} + +Bureaucrat::Bureaucrat(void) + : _name("kanel"), _grade(150) +{ +_log("➕", "Bureaucrat", "", "default constructor called"); +} + +Bureaucrat::Bureaucrat(const Bureaucrat &cpy) +{ +_log("➕", "Bureaucrat", "", "copy constructor called"); + if (&cpy != this) + { + _name = cpy.getName(); + _grade = cpy.getGrade(); + } +} + +Bureaucrat::Bureaucrat(uint8_t grade) + : _name("kanel"), _grade(grade) +{ +_log("➕", "Bureaucrat", "", "constructor called"); +} + +Bureaucrat::Bureaucrat(std::string name) + : _name(name), _grade(150) +{ +_log("➕", "Bureaucrat", "", "constructor called"); +} + +Bureaucrat::Bureaucrat(std::string name, uint8_t grade) + : _name(name), _grade(grade) +{ +_log("➕", "Bureaucrat", "", "constructor called"); +} + +const std::string &Bureaucrat::getName(void) const { return _name; } +uint8_t Bureaucrat::getGrade(void) const { return _grade; } + +Bureaucrat::~Bureaucrat(void) +{ +_log("➖", "Bureaucrat", "", "destructor called"); +} + +void Bureaucrat::signForm(AForm &f) +{ + try + { + f.beSigned(*this); + std::cout << _name << " signed " << f.getName() << std::endl; + } + catch (std::exception &e) + { + std::cout << _name << " counldn't sign " << f.getName() << " because " << e.what() << std::endl; + } +} + + +Bureaucrat &Bureaucrat::operator=(Bureaucrat const &cpy) +{ + if (&cpy == this) + return *this; + _grade = cpy._grade; + return *this; +} + +void Bureaucrat::IncrementGrade(void) +{ + if (_grade - 1 < MAXGRADE) + throw GradeTooHighException(); + _grade--; +} + +void Bureaucrat::DecrementGrade(void) +{ + if (_grade + 1 > MINGRADE) + throw GradeTooLowException(); + _grade++; +} + +std::ostream &operator<<(std::ostream &os, Bureaucrat &val) +{ + os << val.getName() << ", bureaucrat grade " << (int)val.getGrade(); + return os; +} + +const char *Bureaucrat::GradeTooHighException::what() const throw() +{ + return ("Grade is too high"); +} + +const char *Bureaucrat::GradeTooLowException::what() const throw() +{ + return ("Grade is too low"); +} diff --git a/ex02/Bureaucrat.hpp b/ex02/Bureaucrat.hpp new file mode 100644 index 0000000..db4baef --- /dev/null +++ b/ex02/Bureaucrat.hpp @@ -0,0 +1,65 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Bureaucrat.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mmoussou +#include +#include +#include +#include + +void _log(std::string emoji, std::string what, std::string who, std::string str); + +class AForm; + +typedef unsigned char uint8_t; + +#define MAXGRADE 1 +#define MINGRADE 150 + +class Bureaucrat { +public: + Bureaucrat(void); + Bureaucrat(const Bureaucrat &cpy); + Bureaucrat(uint8_t grade); + Bureaucrat(std::string name); + Bureaucrat(std::string name, uint8_t grade); + ~Bureaucrat(void); + + + + void executeForm(const AForm &) const; + + + + Bureaucrat &operator=(const Bureaucrat& other); + const std::string &getName(void) const; + uint8_t getGrade(void) const; + void IncrementGrade(void); + void DecrementGrade(void); + class GradeTooHighException : public std::exception { + public: + virtual const char *what() const throw(); + }; + class GradeTooLowException : public std::exception { + public: + virtual const char *what() const throw(); + }; + void signForm(AForm &); + +private: + std::string _name; + uint8_t _grade; +}; + +std::ostream &operator<<(std::ostream &, Bureaucrat &); diff --git a/ex02/Makefile b/ex02/Makefile new file mode 100644 index 0000000..fe95385 --- /dev/null +++ b/ex02/Makefile @@ -0,0 +1,58 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: mmoussou getName() << " has been pardoned by Zaphod Beeblebroxm ggs..." << std::endl; + } + diff --git a/ex02/PresidentialPardonForm.hpp b/ex02/PresidentialPardonForm.hpp new file mode 100644 index 0000000..be09d69 --- /dev/null +++ b/ex02/PresidentialPardonForm.hpp @@ -0,0 +1,17 @@ +#pragma once + +#include "Bureaucrat.hpp" +#include "AForm.hpp" + +class PresidentialPardonForm : public AForm { + public: + PresidentialPardonForm(void); + PresidentialPardonForm(const PresidentialPardonForm &); + PresidentialPardonForm(std::string); + ~PresidentialPardonForm(void); + + PresidentialPardonForm &operator=(const PresidentialPardonForm &); + private: + void _exec(const Bureaucrat &) const; +}; + diff --git a/ex02/RobotomyRequestForm.cpp b/ex02/RobotomyRequestForm.cpp new file mode 100644 index 0000000..6fcdee2 --- /dev/null +++ b/ex02/RobotomyRequestForm.cpp @@ -0,0 +1,42 @@ +#include "RobotomyRequestForm.hpp" +#include "Bureaucrat.hpp" +#include + +RobotomyRequestForm::RobotomyRequestForm(void): AForm("RobotomyRequestForm", 72, 45) { + _log("➕", "RobotomyRequestForm", "", "default constructor called"); + std::srand(time(0)); +} + +RobotomyRequestForm::RobotomyRequestForm(const RobotomyRequestForm &cpy) : AForm(cpy) { + _log("➕", "RobotomyRequestForm", "", "copy constructor called"); + std::srand(time(0)); + if (this != &cpy) + *this = cpy; +} + +RobotomyRequestForm::RobotomyRequestForm(std::string target): AForm(target, 72, 45) { + _log("➕", "RobotomyRequestForm", "", "target constructor called"); + std::srand(time(0)); +} + +RobotomyRequestForm::~RobotomyRequestForm(void) { + _log("➖", "RobotomyRequestForm", "", "destructor called"); +} + +RobotomyRequestForm &RobotomyRequestForm::operator=(const RobotomyRequestForm &cpy) { + _log("➕", "RobotomyRequestForm", "", "copy assignement constructor called"); + (void) cpy; + return *this; +} + +void RobotomyRequestForm::_exec(const Bureaucrat &b) const { + (void) b; + std::cout << "Driling noise or smth idk" << std::endl; + + if (std::rand() % 2) { + std::cout << this->getName() << " has been robotomised successfully ! :D" << std::endl; + } else { + std::cout << this->getName() << " robotomisation failed D:" << std::endl; + } +} + diff --git a/ex02/RobotomyRequestForm.hpp b/ex02/RobotomyRequestForm.hpp new file mode 100644 index 0000000..5868ea7 --- /dev/null +++ b/ex02/RobotomyRequestForm.hpp @@ -0,0 +1,15 @@ +#pragma once + +#include "AForm.hpp" + +class RobotomyRequestForm : public AForm { + public: + RobotomyRequestForm(void); + RobotomyRequestForm(const RobotomyRequestForm &); + RobotomyRequestForm(std::string); + ~RobotomyRequestForm(void); + + RobotomyRequestForm &operator=(const RobotomyRequestForm &); + private: + void _exec(const Bureaucrat &) const; +}; diff --git a/ex02/ShrubberyCreationForm.cpp b/ex02/ShrubberyCreationForm.cpp new file mode 100644 index 0000000..6d21c5e --- /dev/null +++ b/ex02/ShrubberyCreationForm.cpp @@ -0,0 +1,54 @@ +#include "ShrubberyCreationForm.hpp" +#include +#include +#include + +ShrubberyCreationForm::ShrubberyCreationForm(void) : AForm("ShrubberyCreationForm", 145, 137) { + _log("➕", "ShrubberyCreationForm", "", "default constructor called"); +} + +ShrubberyCreationForm::ShrubberyCreationForm(const ShrubberyCreationForm &cpy) : AForm(cpy) { + _log("➕", "ShrubberyCreationForm", "", "copy constructor called"); + if (this != &cpy) + *this = cpy; +} + +ShrubberyCreationForm::ShrubberyCreationForm(std::string target) : AForm(target, 145, 137) { + _log("➕", "ShrubberyCreationForm", "", "target constructor called"); +} + +ShrubberyCreationForm::~ShrubberyCreationForm(void) { + _log("➖", "ShrubberyCreationForm", "", "destructor called"); +} + +ShrubberyCreationForm &ShrubberyCreationForm::operator=(const ShrubberyCreationForm &cpy) { + _log("➕", "ShrubberyCreationForm", "", "copy assignement constructor called"); + (void) cpy; + return (*this); +} + +void ShrubberyCreationForm::_exec(const Bureaucrat &b) const { + (void) b; + std::ofstream file; + + file.open(std::string(this->getName() + "_shruberry").c_str()); + if (!file.is_open()) + throw std::runtime_error("Could not write to " + this->getName() + + "_shruberry"); + + file << " _-_\n" + " /~~ ~~\\\n" + " /~~ ~~\\\n" + "{ }\n" + " \\ _- -_ /\n" + " ~ \\ // ~\n" + "_- - | | _- _\n" + " _ - | | -_\n" + " // \\\n"; + + file.close(); + + std::cout << "ASCII tree created in : " + this->getName() + "_shruberry" + << std::endl; +} + diff --git a/ex02/ShrubberyCreationForm.hpp b/ex02/ShrubberyCreationForm.hpp new file mode 100644 index 0000000..de17ba0 --- /dev/null +++ b/ex02/ShrubberyCreationForm.hpp @@ -0,0 +1,18 @@ +#pragma once + +#include "AForm.hpp" +#include +#include + +class ShrubberyCreationForm : public AForm { + public: + ShrubberyCreationForm(void); + ShrubberyCreationForm(const ShrubberyCreationForm &); + ShrubberyCreationForm(std::string); + ~ShrubberyCreationForm(void); + + ShrubberyCreationForm &operator=(const ShrubberyCreationForm &); + private: + void _exec(const Bureaucrat &) const; +}; + diff --git a/ex02/main.cpp b/ex02/main.cpp new file mode 100644 index 0000000..a64cf03 --- /dev/null +++ b/ex02/main.cpp @@ -0,0 +1,81 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mmoussou _minForExec) { + throw GradeTooLowException(); + } + std::cout << b.getName() << " executed " << this->getName() << std::endl; + _exec(b); +} + + +AForm::AForm(void) + : _name("Form"), _signed(false), _minForSign(1), _minForExec(1) +{ + _log("➕", "AForm", "", "default constructor called"); +} + +AForm::AForm(std::string name, uint8_t minForSign, uint8_t minForExec) + : _name(name), _signed(false), _minForSign(minForSign), _minForExec(minForExec) +{ + _log("➕", "AForm", "", "constructor called"); + _gradeCheck(); +} + +AForm::AForm(const AForm &f) + : _name(f.getName()), _signed(f.getSigned()), _minForSign(f.getMinForSign()), _minForExec(f.getMinForExec()) +{ + _log("➕", "Form", "", "copy constructor called"); + _gradeCheck(); +} + +AForm::~AForm(void) { _log("➖", "AForm", "", "destructor called"); } + +AForm &AForm::operator=(const AForm &f) { + if (this == &f) + return *this; + _name = f.getName(); + _signed = f.getSigned(); + return *this; +} + +const char *AForm::GradeTooHighException::what() const throw() { + return ("grade is too high"); +} + +const char *AForm::GradeTooLowException::what() const throw() { + return ("grade is too low"); +} + +const char *AForm::NotSigned::what() const throw() { + return "Form exception: Form not signed"; +} + +std::string AForm::getName(void) const { return _name; } +bool AForm::getSigned(void) const { return _signed; } +uint8_t AForm::getMinForSign(void) const { return _minForSign; } +uint8_t AForm::getMinForExec(void) const { return _minForExec; } + +void AForm::beSigned(Bureaucrat b) { + if (_minForSign > b.getGrade()) { + throw GradeTooLowException(); + } + _signed = true; +} + +void AForm::_gradeCheck(void) const { + if (_minForExec < MAXGRADE || _minForSign < MAXGRADE) + throw GradeTooHighException(); + if (_minForExec > MINGRADE || _minForExec > MINGRADE) + throw GradeTooLowException(); +} + +std::ostream &operator<<(std::ostream &os, AForm &val) { + os << "Form: " << val.getName() + << ", minGrade to sign: " << int(val.getMinForSign()) + << ", minGrade to execute: " << int(val.getMinForExec()); + return (os); +} diff --git a/ex03/AForm.hpp b/ex03/AForm.hpp new file mode 100644 index 0000000..be66bb7 --- /dev/null +++ b/ex03/AForm.hpp @@ -0,0 +1,65 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* AForm.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mmoussou + +void _log(std::string emoji, std::string what, std::string who, std::string str); + +typedef unsigned char uint8_t; + +#include "Bureaucrat.hpp" + +class AForm { + public: + AForm(void); + AForm(std::string name, uint8_t minForSign, uint8_t minForExec); + AForm(const AForm &); + virtual ~AForm(void); + + AForm &operator=(const AForm &); + + class GradeTooHighException : public std::exception { + public: + virtual const char *what() const throw(); + }; + class GradeTooLowException : public std::exception { + public: + virtual const char *what() const throw(); + }; + class NotSigned : public std::exception { + public: + virtual const char *what() const throw(); + }; + + std::string getName(void) const; + bool getSigned(void) const; + uint8_t getMinForSign(void) const; + uint8_t getMinForExec(void) const; + + void execute(const Bureaucrat &) const; + + void beSigned(Bureaucrat); + + private: + std::string _name; + bool _signed; + const uint8_t _minForSign; + const uint8_t _minForExec; + + void _gradeCheck(void) const; + virtual void _exec(const Bureaucrat &) const = 0; +}; + +std::ostream &operator<<(std::ostream &, AForm &); + diff --git a/ex03/Bureaucrat.cpp b/ex03/Bureaucrat.cpp new file mode 100644 index 0000000..bc9b5b5 --- /dev/null +++ b/ex03/Bureaucrat.cpp @@ -0,0 +1,126 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Bureaucrat.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mmoussou + +void _log(std::string emoji, std::string what, std::string who, std::string str) +{ +#ifdef VERBOSE + if (who.empty()) + std::cout << "「" << emoji << "」" << what << ": " << str << std::endl; + else + std::cout << "「" << emoji << "」" << what << "(" << who << "): " << str + << std::endl; +#else + (void)emoji, (void)what, (void)who, (void)str; +#endif +} + +void Bureaucrat::executeForm(const AForm &form) const { + form.execute(*this); +} + +Bureaucrat::Bureaucrat(void) + : _name("kanel"), _grade(150) +{ +_log("➕", "Bureaucrat", "", "default constructor called"); +} + +Bureaucrat::Bureaucrat(const Bureaucrat &cpy) +{ +_log("➕", "Bureaucrat", "", "copy constructor called"); + if (&cpy != this) + { + _name = cpy.getName(); + _grade = cpy.getGrade(); + } +} + +Bureaucrat::Bureaucrat(uint8_t grade) + : _name("kanel"), _grade(grade) +{ +_log("➕", "Bureaucrat", "", "constructor called"); +} + +Bureaucrat::Bureaucrat(std::string name) + : _name(name), _grade(150) +{ +_log("➕", "Bureaucrat", "", "constructor called"); +} + +Bureaucrat::Bureaucrat(std::string name, uint8_t grade) + : _name(name), _grade(grade) +{ +_log("➕", "Bureaucrat", "", "constructor called"); +} + +const std::string &Bureaucrat::getName(void) const { return _name; } +uint8_t Bureaucrat::getGrade(void) const { return _grade; } + +Bureaucrat::~Bureaucrat(void) +{ +_log("➖", "Bureaucrat", "", "destructor called"); +} + +void Bureaucrat::signForm(AForm &f) +{ + try + { + f.beSigned(*this); + std::cout << _name << " signed " << f.getName() << std::endl; + } + catch (std::exception &e) + { + std::cout << _name << " counldn't sign " << f.getName() << " because " << e.what() << std::endl; + } +} + + +Bureaucrat &Bureaucrat::operator=(Bureaucrat const &cpy) +{ + if (&cpy == this) + return *this; + _grade = cpy._grade; + return *this; +} + +void Bureaucrat::IncrementGrade(void) +{ + if (_grade - 1 < MAXGRADE) + throw GradeTooHighException(); + _grade--; +} + +void Bureaucrat::DecrementGrade(void) +{ + if (_grade + 1 > MINGRADE) + throw GradeTooLowException(); + _grade++; +} + +std::ostream &operator<<(std::ostream &os, Bureaucrat &val) +{ + os << val.getName() << ", bureaucrat grade " << (int)val.getGrade(); + return os; +} + +const char *Bureaucrat::GradeTooHighException::what() const throw() +{ + return ("Grade is too high"); +} + +const char *Bureaucrat::GradeTooLowException::what() const throw() +{ + return ("Grade is too low"); +} diff --git a/ex03/Bureaucrat.hpp b/ex03/Bureaucrat.hpp new file mode 100644 index 0000000..db4baef --- /dev/null +++ b/ex03/Bureaucrat.hpp @@ -0,0 +1,65 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Bureaucrat.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mmoussou +#include +#include +#include +#include + +void _log(std::string emoji, std::string what, std::string who, std::string str); + +class AForm; + +typedef unsigned char uint8_t; + +#define MAXGRADE 1 +#define MINGRADE 150 + +class Bureaucrat { +public: + Bureaucrat(void); + Bureaucrat(const Bureaucrat &cpy); + Bureaucrat(uint8_t grade); + Bureaucrat(std::string name); + Bureaucrat(std::string name, uint8_t grade); + ~Bureaucrat(void); + + + + void executeForm(const AForm &) const; + + + + Bureaucrat &operator=(const Bureaucrat& other); + const std::string &getName(void) const; + uint8_t getGrade(void) const; + void IncrementGrade(void); + void DecrementGrade(void); + class GradeTooHighException : public std::exception { + public: + virtual const char *what() const throw(); + }; + class GradeTooLowException : public std::exception { + public: + virtual const char *what() const throw(); + }; + void signForm(AForm &); + +private: + std::string _name; + uint8_t _grade; +}; + +std::ostream &operator<<(std::ostream &, Bureaucrat &); diff --git a/ex03/Intern.cpp b/ex03/Intern.cpp new file mode 100644 index 0000000..ad4bb6f --- /dev/null +++ b/ex03/Intern.cpp @@ -0,0 +1,52 @@ +#include "Intern.hpp" +#include "Bureaucrat.hpp" +#include "PresidentialPardonForm.hpp" +#include "RobotomyRequestForm.hpp" +#include "ShrubberyCreationForm.hpp" +#include + +Intern::Intern(void) { + _log("➕", "Intern", "", "default constructor called"); +} + +Intern::Intern(const Intern &cpy) { + _log("➕", "Intern", "", "copy constructor called"); + *this = cpy; +} + +Intern::~Intern(void) { + _log("➖", "Intern", "", "destructor called"); +} + +Intern &Intern::operator=(const Intern &) { + _log("➕", "Intern", "", "copy assignement constructor called"); + return *this; +} + +AForm *newShrubbery(std::string &target) { + return new ShrubberyCreationForm(target); +} + +AForm *newPresidential(std::string &target) { + return new PresidentialPardonForm(target); +} + +AForm *newRobotomy(std::string &target) { + return new RobotomyRequestForm(target); +} + +AForm *Intern::makeForm(std::string name, std::string target) const { + std::string formName[] = {"robotomy request", "shrubbery request", "presidential pardon request"}; + AForm *(*newForm[])(std::string &) = {&newRobotomy, &newShrubbery, &newPresidential}; + + for (size_t i = 0; i < 3; i++) { + if (formName[i] == name) { + AForm *n = newForm[i](target); + std::cout << "Intern creates " << name << std::endl; + return n; + } + } + std::cerr << "Intern doesn't know " << name << std::endl; + return NULL; +} + diff --git a/ex03/Intern.hpp b/ex03/Intern.hpp new file mode 100644 index 0000000..a4c991e --- /dev/null +++ b/ex03/Intern.hpp @@ -0,0 +1,18 @@ +#pragma once + +#include +#include + +class AForm; + +class Intern { + public: + Intern(void); + Intern(const Intern &); + ~Intern(void); + Intern &operator=(const Intern &); + + AForm *makeForm(std::string, std::string) const; + +}; + diff --git a/ex03/Makefile b/ex03/Makefile new file mode 100644 index 0000000..f290a4e --- /dev/null +++ b/ex03/Makefile @@ -0,0 +1,58 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: mmoussou getName() << " has been pardoned by Zaphod Beeblebroxm ggs..." << std::endl; + } + diff --git a/ex03/PresidentialPardonForm.hpp b/ex03/PresidentialPardonForm.hpp new file mode 100644 index 0000000..be09d69 --- /dev/null +++ b/ex03/PresidentialPardonForm.hpp @@ -0,0 +1,17 @@ +#pragma once + +#include "Bureaucrat.hpp" +#include "AForm.hpp" + +class PresidentialPardonForm : public AForm { + public: + PresidentialPardonForm(void); + PresidentialPardonForm(const PresidentialPardonForm &); + PresidentialPardonForm(std::string); + ~PresidentialPardonForm(void); + + PresidentialPardonForm &operator=(const PresidentialPardonForm &); + private: + void _exec(const Bureaucrat &) const; +}; + diff --git a/ex03/RobotomyRequestForm.cpp b/ex03/RobotomyRequestForm.cpp new file mode 100644 index 0000000..6fcdee2 --- /dev/null +++ b/ex03/RobotomyRequestForm.cpp @@ -0,0 +1,42 @@ +#include "RobotomyRequestForm.hpp" +#include "Bureaucrat.hpp" +#include + +RobotomyRequestForm::RobotomyRequestForm(void): AForm("RobotomyRequestForm", 72, 45) { + _log("➕", "RobotomyRequestForm", "", "default constructor called"); + std::srand(time(0)); +} + +RobotomyRequestForm::RobotomyRequestForm(const RobotomyRequestForm &cpy) : AForm(cpy) { + _log("➕", "RobotomyRequestForm", "", "copy constructor called"); + std::srand(time(0)); + if (this != &cpy) + *this = cpy; +} + +RobotomyRequestForm::RobotomyRequestForm(std::string target): AForm(target, 72, 45) { + _log("➕", "RobotomyRequestForm", "", "target constructor called"); + std::srand(time(0)); +} + +RobotomyRequestForm::~RobotomyRequestForm(void) { + _log("➖", "RobotomyRequestForm", "", "destructor called"); +} + +RobotomyRequestForm &RobotomyRequestForm::operator=(const RobotomyRequestForm &cpy) { + _log("➕", "RobotomyRequestForm", "", "copy assignement constructor called"); + (void) cpy; + return *this; +} + +void RobotomyRequestForm::_exec(const Bureaucrat &b) const { + (void) b; + std::cout << "Driling noise or smth idk" << std::endl; + + if (std::rand() % 2) { + std::cout << this->getName() << " has been robotomised successfully ! :D" << std::endl; + } else { + std::cout << this->getName() << " robotomisation failed D:" << std::endl; + } +} + diff --git a/ex03/RobotomyRequestForm.hpp b/ex03/RobotomyRequestForm.hpp new file mode 100644 index 0000000..5868ea7 --- /dev/null +++ b/ex03/RobotomyRequestForm.hpp @@ -0,0 +1,15 @@ +#pragma once + +#include "AForm.hpp" + +class RobotomyRequestForm : public AForm { + public: + RobotomyRequestForm(void); + RobotomyRequestForm(const RobotomyRequestForm &); + RobotomyRequestForm(std::string); + ~RobotomyRequestForm(void); + + RobotomyRequestForm &operator=(const RobotomyRequestForm &); + private: + void _exec(const Bureaucrat &) const; +}; diff --git a/ex03/ShrubberyCreationForm.cpp b/ex03/ShrubberyCreationForm.cpp new file mode 100644 index 0000000..6d21c5e --- /dev/null +++ b/ex03/ShrubberyCreationForm.cpp @@ -0,0 +1,54 @@ +#include "ShrubberyCreationForm.hpp" +#include +#include +#include + +ShrubberyCreationForm::ShrubberyCreationForm(void) : AForm("ShrubberyCreationForm", 145, 137) { + _log("➕", "ShrubberyCreationForm", "", "default constructor called"); +} + +ShrubberyCreationForm::ShrubberyCreationForm(const ShrubberyCreationForm &cpy) : AForm(cpy) { + _log("➕", "ShrubberyCreationForm", "", "copy constructor called"); + if (this != &cpy) + *this = cpy; +} + +ShrubberyCreationForm::ShrubberyCreationForm(std::string target) : AForm(target, 145, 137) { + _log("➕", "ShrubberyCreationForm", "", "target constructor called"); +} + +ShrubberyCreationForm::~ShrubberyCreationForm(void) { + _log("➖", "ShrubberyCreationForm", "", "destructor called"); +} + +ShrubberyCreationForm &ShrubberyCreationForm::operator=(const ShrubberyCreationForm &cpy) { + _log("➕", "ShrubberyCreationForm", "", "copy assignement constructor called"); + (void) cpy; + return (*this); +} + +void ShrubberyCreationForm::_exec(const Bureaucrat &b) const { + (void) b; + std::ofstream file; + + file.open(std::string(this->getName() + "_shruberry").c_str()); + if (!file.is_open()) + throw std::runtime_error("Could not write to " + this->getName() + + "_shruberry"); + + file << " _-_\n" + " /~~ ~~\\\n" + " /~~ ~~\\\n" + "{ }\n" + " \\ _- -_ /\n" + " ~ \\ // ~\n" + "_- - | | _- _\n" + " _ - | | -_\n" + " // \\\n"; + + file.close(); + + std::cout << "ASCII tree created in : " + this->getName() + "_shruberry" + << std::endl; +} + diff --git a/ex03/ShrubberyCreationForm.hpp b/ex03/ShrubberyCreationForm.hpp new file mode 100644 index 0000000..de17ba0 --- /dev/null +++ b/ex03/ShrubberyCreationForm.hpp @@ -0,0 +1,18 @@ +#pragma once + +#include "AForm.hpp" +#include +#include + +class ShrubberyCreationForm : public AForm { + public: + ShrubberyCreationForm(void); + ShrubberyCreationForm(const ShrubberyCreationForm &); + ShrubberyCreationForm(std::string); + ~ShrubberyCreationForm(void); + + ShrubberyCreationForm &operator=(const ShrubberyCreationForm &); + private: + void _exec(const Bureaucrat &) const; +}; + diff --git a/ex03/ex03 b/ex03/ex03 new file mode 100755 index 0000000..68b0df9 Binary files /dev/null and b/ex03/ex03 differ diff --git a/ex03/main.cpp b/ex03/main.cpp new file mode 100644 index 0000000..4dc167d --- /dev/null +++ b/ex03/main.cpp @@ -0,0 +1,67 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mmoussou