diff --git a/.gitignore b/.gitignore index 3f90193..f41fa31 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ compile_commands.json Form AForm Bureaucrat +Intern diff --git a/ex02/RobotomyRequestForm.cpp b/ex02/RobotomyRequestForm.cpp index d3b02fe..d206d37 100644 --- a/ex02/RobotomyRequestForm.cpp +++ b/ex02/RobotomyRequestForm.cpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/03 09:19:30 by adjoly #+# #+# */ -/* Updated: 2025/04/03 14:17:12 by adjoly ### ########.fr */ +/* Updated: 2025/04/03 16:02:35 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -46,8 +46,8 @@ void RobotomyRequestForm::_exec(const Bureaucrat &b) const { std::srand(time(0)); if (std::rand() % 2) { - std::cout << b.getName() << " has been robotomized successfully ! :D" << std::endl; + std::cout << b.getName() << " has been robotomised successfully ! :D" << std::endl; } else { - std::cout << b.getName() << " robotomization failed 😿" << std::endl; + std::cout << b.getName() << " robotomisation failed 😿" << std::endl; } } diff --git a/ex02/main.cpp b/ex02/main.cpp index d410212..c88deac 100644 --- a/ex02/main.cpp +++ b/ex02/main.cpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/23 14:01:11 by adjoly #+# #+# */ -/* Updated: 2025/04/03 14:32:57 by adjoly ### ########.fr */ +/* Updated: 2025/04/03 15:49:50 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -48,8 +48,8 @@ int main(void) { try { s.beSigned(knl); - knl.executeForm(s); } catch (std::exception &e) { + knl.executeForm(s); std::cerr << "Error : " << e.what() << std::endl; } diff --git a/ex03/AForm.cpp b/ex03/AForm.cpp new file mode 100644 index 0000000..224da3d --- /dev/null +++ b/ex03/AForm.cpp @@ -0,0 +1,89 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* AForm.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/03/09 17:38:19 by adjoly #+# #+# */ +/* Updated: 2025/04/03 14:33:17 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "AForm.hpp" +#include "Bureaucrat.hpp" + +AForm::AForm(void) : _name(""), _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("➕", "AForm", "", "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 ("not yet 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::execute(const Bureaucrat &b) const { + if (_signed == false) { + throw NotSigned(); + } + if (b.getGrade() > _minForExec) { + throw GradeTooLowException(); + } + _exec(b); +} + +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 << "AForm: " << val.getName() + << ", minGrade to sign: " << val.getMinForSign() + << ", min grade to execute: " << val.getMinForExec() << std::endl; + return (os); +} diff --git a/ex03/AForm.hpp b/ex03/AForm.hpp new file mode 100644 index 0000000..44347d3 --- /dev/null +++ b/ex03/AForm.hpp @@ -0,0 +1,64 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* AForm.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/03/08 20:10:59 by adjoly #+# #+# */ +/* Updated: 2025/04/03 13:50:38 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include + +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 beSigned(Bureaucrat); + void execute(const Bureaucrat &) const; + + private: + std::string _name; + bool _signed; + const uint8_t _minForSign; + const uint8_t _minForExec; + + virtual void _exec(const Bureaucrat &) const = 0; + void _gradeCheck(void) const; +}; + +std::ostream &operator<<(std::ostream &, AForm &); diff --git a/ex03/Bureaucrat.cpp b/ex03/Bureaucrat.cpp new file mode 100644 index 0000000..14219f1 --- /dev/null +++ b/ex03/Bureaucrat.cpp @@ -0,0 +1,115 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Bureaucrat.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/12/23 13:59:45 by adjoly #+# #+# */ +/* Updated: 2025/04/03 14:21:09 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Bureaucrat.hpp" +#include "AForm.hpp" +#include + +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) { + _log("➕", "Bureaucrat", "", "default constructor called"); +} + +Bureaucrat::Bureaucrat(const Bureaucrat &cpy) { + _log("➕", "Bureaucrat", "", "copy constructor called"); + if (&cpy != this) + *this = cpy; +} + +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::executeForm(const AForm &form) const { + form.execute(*this); +} + +Bureaucrat &Bureaucrat::operator++(void) { + _grade--; + if (_grade < MAXGRADE) + throw GradeTooHighException(); + return *this; +} + +Bureaucrat &Bureaucrat::operator--(void) { + _grade++; + if (_grade > MINGRADE) + throw GradeTooLowException(); + return *this; +} + +Bureaucrat Bureaucrat::operator++(int) { + Bureaucrat old = *this; + + _grade--; + if (_grade < MAXGRADE) + throw GradeTooHighException(); + return (old); +} + +Bureaucrat Bureaucrat::operator--(int) { + Bureaucrat old = *this; + + _grade++; + if (_grade > MINGRADE) + throw GradeTooLowException(); + return old; +} + +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"); +} + +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; + } +} diff --git a/ex03/Bureaucrat.hpp b/ex03/Bureaucrat.hpp new file mode 100644 index 0000000..ba8615e --- /dev/null +++ b/ex03/Bureaucrat.hpp @@ -0,0 +1,69 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Bureaucrat.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/12/23 13:59:43 by adjoly #+# #+# */ +/* Updated: 2025/04/03 13:50:01 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include +#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(std::string name, uint8_t grade); + ~Bureaucrat(void); + + const std::string &getName(void) const; + uint8_t getGrade(void) const; + + void executeForm(const AForm &) const; + + class GradeTooHighException : public std::exception { + public: + virtual const char *what() const throw(); + }; + class GradeTooLowException : public std::exception { + public: + virtual const char *what() const throw(); + }; + + // Copy assignement operator + Bureaucrat &operator=(Bureaucrat const &); + + // Preincrement operator + Bureaucrat &operator++(void); + Bureaucrat &operator--(void); + + // Post increment operator + Bureaucrat operator++(int); + Bureaucrat operator--(int); + + void signForm(AForm &); + private: + const 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..b0a6720 --- /dev/null +++ b/ex03/Intern.cpp @@ -0,0 +1,63 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Intern.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/04/03 15:24:46 by adjoly #+# #+# */ +/* Updated: 2025/04/03 15:57:18 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#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..ecef4db --- /dev/null +++ b/ex03/Intern.hpp @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Intern.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/04/03 15:04:27 by adjoly #+# #+# */ +/* Updated: 2025/04/03 15:12:56 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include +#include + +class AForm; + +class Intern { + public: + Intern(void); + Intern(const Intern &); + ~Intern(void); + + AForm *makeForm(std::string, std::string) const; + + Intern &operator=(const Intern &); + private: +}; diff --git a/ex03/Makefile b/ex03/Makefile new file mode 100644 index 0000000..ec70798 --- /dev/null +++ b/ex03/Makefile @@ -0,0 +1,60 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: adjoly +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2024/10/25 16:09:27 by adjoly #+# #+# # +# Updated: 2025/04/03 16:06:27 by adjoly ### ########.fr # +# # +# **************************************************************************** # + +SHELL = bash + +NAME = Intern + +CC = c++ + +OBJSDIR = obj/ + +SRCS = $(shell find . -name '*.cpp') + +OBJS = $(addprefix $(OBJSDIR), $(SRCS:.cpp=.o)) + +FLAGS = -Wall -Werror -Wextra -std=c++98 -MMD -MP + +RED = \033[0;31m +GREEN = \033[0;32m +YELLOW = \033[1;33m +PURPLE = \e[0;35m +NC = \033[0m +DELETE = \x1B[2K\r + +ifeq ($(VERBOSE),true) + FLAGS += -D VERBOSE +endif + +all: $(NAME) + +$(NAME): $(OBJS) + @$(CC) $(FLAGS) -I . $(OBJS) -o $(NAME) + @printf "$(YELLOW)「✨」($(NAME)) Program compiled\n" + +$(OBJSDIR)%.o: %.cpp + @mkdir -p $(@D) + @$(CC) $(FLAGS) -I . -c $< -o $@ + @printf "$(DELETE)$(GREEN)「🔨」($<) Object compiled\n" + +clean: + @rm -f $(OBJS) + @printf "$(DELETE)$(RED)「🗑️」($(OBJS)) Object deleted\n" + +fclean: clean + @rm -f $(NAME) + @rm -Rf $(OBJSDIR) + @printf "$(RED)「🗑️」($(NAME)) Program deleted\n" + +re: fclean all + +.PHONY: clean fclean all re diff --git a/ex03/PresidentialPardonForm.cpp b/ex03/PresidentialPardonForm.cpp new file mode 100644 index 0000000..2a81231 --- /dev/null +++ b/ex03/PresidentialPardonForm.cpp @@ -0,0 +1,48 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* PresidentialPardonForm.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/04/03 09:45:26 by adjoly #+# #+# */ +/* Updated: 2025/04/03 14:13:21 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "PresidentialPardonForm.hpp" +#include "AForm.hpp" + +PresidentialPardonForm::PresidentialPardonForm(void) + : AForm("PresidentialPardonForm", 25, 5) { + _log("➕", "PresidentialPardonForm", "", "default constructor called"); +} + +PresidentialPardonForm::PresidentialPardonForm( + const PresidentialPardonForm &cpy) : AForm(cpy) { + _log("➕", "PresidentialPardonForm", "", "copy constructor called"); + if (this != &cpy) + *this = cpy; +} + +PresidentialPardonForm::PresidentialPardonForm(std::string &target) + : AForm(target, 25, 5) { + _log("➕", "PresidentialPardonForm", "", "target constructor called"); +} + +PresidentialPardonForm::~PresidentialPardonForm(void) { + _log("➖", "PresidentialPardonForm", "", "destructor called"); +} + +PresidentialPardonForm & +PresidentialPardonForm::operator=(const PresidentialPardonForm &cpy) { + _log("➕", "PresidentialPardonForm", "", + "copy assignement constructor called"); + (void)cpy; + return *this; +} + +void PresidentialPardonForm::_exec(const Bureaucrat &b) const { + std::cout << b.getName() << " has been pardoned by Zaphod Beeblebrox 🙀" + << std::endl; +} diff --git a/ex03/PresidentialPardonForm.hpp b/ex03/PresidentialPardonForm.hpp new file mode 100644 index 0000000..75c909b --- /dev/null +++ b/ex03/PresidentialPardonForm.hpp @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* PresidentialPardonForm.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/04/03 09:44:16 by adjoly #+# #+# */ +/* Updated: 2025/04/03 14:04:03 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#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..d3b02fe --- /dev/null +++ b/ex03/RobotomyRequestForm.cpp @@ -0,0 +1,53 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* RobotomyRequestForm.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/04/03 09:19:30 by adjoly #+# #+# */ +/* Updated: 2025/04/03 14:17:12 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "RobotomyRequestForm.hpp" +#include "Bureaucrat.hpp" +#include + +RobotomyRequestForm::RobotomyRequestForm(void) + : AForm("RobotomyRequestForm", 72, 45) { + _log("➕", "RobotomyRequestForm", "", "default constructor called"); +} + +RobotomyRequestForm::RobotomyRequestForm(const RobotomyRequestForm &cpy) : AForm(cpy) { + _log("➕", "RobotomyRequestForm", "", "copy constructor called"); + if (this != &cpy) + *this = cpy; +} + +RobotomyRequestForm::RobotomyRequestForm(std::string &target) + : AForm(target, 72, 45) { + _log("➕", "RobotomyRequestForm", "", "target constructor called"); +} + +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 { + std::cout << "Drilingg noisseeeeeeee !!!" << std::endl; + + std::srand(time(0)); + + if (std::rand() % 2) { + std::cout << b.getName() << " has been robotomized successfully ! :D" << std::endl; + } else { + std::cout << b.getName() << " robotomization failed 😿" << std::endl; + } +} diff --git a/ex03/RobotomyRequestForm.hpp b/ex03/RobotomyRequestForm.hpp new file mode 100644 index 0000000..a3bef36 --- /dev/null +++ b/ex03/RobotomyRequestForm.hpp @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* RobotomyRequestForm.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/04/02 16:13:39 by adjoly #+# #+# */ +/* Updated: 2025/04/03 10:02:14 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#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..c7589c9 --- /dev/null +++ b/ex03/ShrubberyCreationForm.cpp @@ -0,0 +1,67 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ShrubberyCreationForm.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/04/01 09:25:06 by adjoly #+# #+# */ +/* Updated: 2025/04/03 14:33:50 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ShrubberyCreationForm.hpp" +#include "AForm.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, 72, 45) { + _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 { + std::ofstream file; + + file.open(std::string(b.getName() + "_shruberry").c_str()); + if (!file.is_open()) + throw std::runtime_error("Could not write to " + b.getName() + + "_shruberry"); + + file << " _-_\n" + " /~~ ~~\\\n" + " /~~ ~~\\\n" + "{ }\n" + " \\ _- -_ /\n" + " ~ \\ // ~\n" + "_- - | | _- _\n" + " _ - | | -_\n" + " // \\\n"; + + file.close(); + + std::cout << "ASCII tree created in : " + b.getName() + "_shruberry" + << std::endl; +} diff --git a/ex03/ShrubberyCreationForm.hpp b/ex03/ShrubberyCreationForm.hpp new file mode 100644 index 0000000..7d2bbd8 --- /dev/null +++ b/ex03/ShrubberyCreationForm.hpp @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ShrubberyCreationForm.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/04/01 09:19:24 by adjoly #+# #+# */ +/* Updated: 2025/04/03 13:34:46 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#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/main.cpp b/ex03/main.cpp new file mode 100644 index 0000000..03f8a1e --- /dev/null +++ b/ex03/main.cpp @@ -0,0 +1,62 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/12/23 14:01:11 by adjoly #+# #+# */ +/* Updated: 2025/04/03 15:51:43 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Bureaucrat.hpp" +#include "AForm.hpp" +#include "Intern.hpp" +#include "ShrubberyCreationForm.hpp" +#include + +int main(void) { + Bureaucrat knl("Kanel", 10); + + knl--; + std::cout << knl << std::endl; + knl++; + std::cout << knl << std::endl; + try { + knl++; + knl++; + knl++; + knl++; + knl++; + knl++; + knl++; + knl++; + knl++; + knl++; + knl++; + knl++; + knl++; + knl++; + knl++; + knl++; + knl++; + } catch (const std::exception &e) { + std::cerr << "Error : " << e.what() << std::endl; + } + + Intern intern; + + AForm *s; + + s = intern.makeForm("robotomy request", "Kanel"); + + try { + s->beSigned(knl); + knl.executeForm(*s); + } catch (std::exception &e) { + std::cerr << "Error : " << e.what() << std::endl; + } + + std::cout << knl << std::endl; +}