From d56e8011de5661d5760d665762c7365cd88be83d Mon Sep 17 00:00:00 2001 From: adjoly Date: Sun, 30 Mar 2025 18:07:11 +0200 Subject: [PATCH] =?UTF-8?q?=E3=80=8C=F0=9F=8F=97=EF=B8=8F=E3=80=8D=20wip:?= =?UTF-8?q?=20Added=20all=20files=20for=20ex02?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ex02/AForm.cpp | 50 ++++++++++++++ ex02/AForm.hpp | 54 ++++++++++++++++ ex02/Bureaucrat.cpp | 111 ++++++++++++++++++++++++++++++++ ex02/Bureaucrat.hpp | 67 +++++++++++++++++++ ex02/Makefile | 60 +++++++++++++++++ ex02/PresidentialPardonForm.cpp | 0 ex02/PresidentialPardonForm.hpp | 12 ++++ ex02/RobotomyRequestForm.cpp | 0 ex02/RobotomyRequestForm.hpp | 0 ex02/ShrubberyCreationForm.cpp | 0 ex02/ShrubberyCreationForm.hpp | 0 ex02/main.cpp | 51 +++++++++++++++ 12 files changed, 405 insertions(+) create mode 100644 ex02/AForm.cpp create mode 100644 ex02/AForm.hpp create mode 100644 ex02/Bureaucrat.cpp create mode 100644 ex02/Bureaucrat.hpp create mode 100644 ex02/Makefile create mode 100644 ex02/PresidentialPardonForm.cpp create mode 100644 ex02/PresidentialPardonForm.hpp create mode 100644 ex02/RobotomyRequestForm.cpp create mode 100644 ex02/RobotomyRequestForm.hpp create mode 100644 ex02/ShrubberyCreationForm.cpp create mode 100644 ex02/ShrubberyCreationForm.hpp create mode 100644 ex02/main.cpp diff --git a/ex02/AForm.cpp b/ex02/AForm.cpp new file mode 100644 index 0000000..28b50c8 --- /dev/null +++ b/ex02/AForm.cpp @@ -0,0 +1,50 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Form.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/03/09 17:38:19 by adjoly #+# #+# */ +/* Updated: 2025/03/30 15:14:17 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Form.hpp" +#include "Bureaucrat.hpp" + +Form::Form(void) : _name(""), _signed(false), _minForSign(1), _minForExec(1) { + _log("➕", "Form", "", "default constructor called"); +} +Form::Form(std::string name, uint8_t minForSign, uint8_t minForExec) + : _name(name), _signed(false), _minForSign(minForSign), + _minForExec(minForExec) { + _log("➕", "Form", "", "default constructor called"); +} + +Form::~Form(void) { _log("➖", "Form", "", "destructor called"); } + +const char *Form::GradeTooHighException::what() const throw() { + return ("grade is too high"); +} + +const char *Form::GradeTooLowException::what() const throw() { + return ("grade is too low"); +} + +std::string Form::getName(void) const { return _name; } +bool Form::getSigned(void) const { return _signed; } +uint8_t Form::getMinForSign(void) const { return _minForSign; } +uint8_t Form::getMinForExec(void) const { return _minForExec; } + +void Form::beSigned(Bureaucrat b) { + if (_minForSign > b.getGrade()) { + throw GradeTooLowException(); + } + _signed = true; +} + +std::ostream &operator<<(std::ostream &os, Form &val) { + os << "Form: " << val.getName() << ", minGrade to sign: " << val.getMinForSign() << ", min grade to execute: " << val.getMinForExec() << std::endl; + return (os); +} diff --git a/ex02/AForm.hpp b/ex02/AForm.hpp new file mode 100644 index 0000000..582ab98 --- /dev/null +++ b/ex02/AForm.hpp @@ -0,0 +1,54 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Form.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/03/08 20:10:59 by adjoly #+# #+# */ +/* Updated: 2025/03/30 15:11:58 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 Bureaucrat; + +class Form { + public: + Form(void); + Form(std::string name, uint8_t minForSign, uint8_t minForExec); + ~Form(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(); + }; + + std::string getName(void) const; + bool getSigned(void) const; + uint8_t getMinForSign(void) const; + uint8_t getMinForExec(void) const; + + void beSigned(Bureaucrat); + + private: + const std::string _name; + bool _signed; + const uint8_t _minForSign; + const uint8_t _minForExec; +}; + +std::ostream &operator<<(std::ostream &, Form &); diff --git a/ex02/Bureaucrat.cpp b/ex02/Bureaucrat.cpp new file mode 100644 index 0000000..db2b89c --- /dev/null +++ b/ex02/Bureaucrat.cpp @@ -0,0 +1,111 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Bureaucrat.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/12/23 13:59:45 by adjoly #+# #+# */ +/* Updated: 2025/03/30 15:13:43 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Bureaucrat.hpp" +#include "Form.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) { return _name; } +uint8_t Bureaucrat::getGrade(void) { 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; +} + +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(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; + } +} diff --git a/ex02/Bureaucrat.hpp b/ex02/Bureaucrat.hpp new file mode 100644 index 0000000..65eb429 --- /dev/null +++ b/ex02/Bureaucrat.hpp @@ -0,0 +1,67 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Bureaucrat.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/12/23 13:59:43 by adjoly #+# #+# */ +/* Updated: 2025/03/30 15:10:46 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 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); + + const std::string &getName(void); + uint8_t getGrade(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(); + }; + + // 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(Form &); + private: + const 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..5636370 --- /dev/null +++ b/ex02/Makefile @@ -0,0 +1,60 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: adjoly +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2024/10/25 16:09:27 by adjoly #+# #+# # +# Updated: 2025/03/30 15:14:32 by adjoly ### ########.fr # +# # +# **************************************************************************** # + +SHELL = bash + +NAME = Form + +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/ex02/PresidentialPardonForm.cpp b/ex02/PresidentialPardonForm.cpp new file mode 100644 index 0000000..e69de29 diff --git a/ex02/PresidentialPardonForm.hpp b/ex02/PresidentialPardonForm.hpp new file mode 100644 index 0000000..2b4519d --- /dev/null +++ b/ex02/PresidentialPardonForm.hpp @@ -0,0 +1,12 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* PresidentialPardonForm.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/03/30 16:25:52 by adjoly #+# #+# */ +/* Updated: 2025/03/30 18:06:49 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + diff --git a/ex02/RobotomyRequestForm.cpp b/ex02/RobotomyRequestForm.cpp new file mode 100644 index 0000000..e69de29 diff --git a/ex02/RobotomyRequestForm.hpp b/ex02/RobotomyRequestForm.hpp new file mode 100644 index 0000000..e69de29 diff --git a/ex02/ShrubberyCreationForm.cpp b/ex02/ShrubberyCreationForm.cpp new file mode 100644 index 0000000..e69de29 diff --git a/ex02/ShrubberyCreationForm.hpp b/ex02/ShrubberyCreationForm.hpp new file mode 100644 index 0000000..e69de29 diff --git a/ex02/main.cpp b/ex02/main.cpp new file mode 100644 index 0000000..16995a1 --- /dev/null +++ b/ex02/main.cpp @@ -0,0 +1,51 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/12/23 14:01:11 by adjoly #+# #+# */ +/* Updated: 2025/03/30 15:13:07 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Bureaucrat.hpp" +#include "Form.hpp" +#include + +int main(void) { + Bureaucrat knl("Kanel", 10); + Bureaucrat su("Suki", 1); + + 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; + } + Form f("le contrattt", 5, 5); + + su.signForm(f); + + std::cout << knl << std::endl; +}