diff --git a/ex01/Bureaucrat.cpp b/ex01/Bureaucrat.cpp new file mode 100644 index 0000000..e4248b1 --- /dev/null +++ b/ex01/Bureaucrat.cpp @@ -0,0 +1,110 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Bureaucrat.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/12/23 13:59:45 by adjoly #+# #+# */ +/* Updated: 2025/03/29 16:49:20 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Bureaucrat.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.getSigned(); + 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/ex01/Bureaucrat.hpp b/ex01/Bureaucrat.hpp new file mode 100644 index 0000000..78196a7 --- /dev/null +++ b/ex01/Bureaucrat.hpp @@ -0,0 +1,67 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Bureaucrat.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/12/23 13:59:43 by adjoly #+# #+# */ +/* Updated: 2025/03/29 15:42:43 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/ex01/Form.cpp b/ex01/Form.cpp new file mode 100644 index 0000000..f4153da --- /dev/null +++ b/ex01/Form.cpp @@ -0,0 +1,49 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Form.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/03/09 17:38:19 by adjoly #+# #+# */ +/* Updated: 2025/03/29 16:49:11 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Form.hpp" +#include "Bureaucrat.hpp" + +Form::Form(void) : _name(""), _signed(false), _minForExec(1), _minForSign(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"); +} + +const std::string Form::getName(void) const { return _name; } +bool Form::getSigned(void) { return _signed; } +const uint8_t Form::getMinForSign(void) { return _minForSign; } +const uint8_t Form::getMinForExec(void) { 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: " << +} diff --git a/ex01/Form.hpp b/ex01/Form.hpp new file mode 100644 index 0000000..7681545 --- /dev/null +++ b/ex01/Form.hpp @@ -0,0 +1,53 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Form.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/03/08 20:10:59 by adjoly #+# #+# */ +/* Updated: 2025/03/29 15:43:02 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; + +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(); + }; + + const std::string getName(void) const; + bool getSigned(void); + const uint8_t getMinForSign(void); + const uint8_t getMinForExec(void); + + 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/ex01/Makefile b/ex01/Makefile new file mode 100644 index 0000000..47d94bd --- /dev/null +++ b/ex01/Makefile @@ -0,0 +1,60 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: adjoly +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2024/10/25 16:09:27 by adjoly #+# #+# # +# Updated: 2025/03/08 20:20:59 by adjoly ### ########.fr # +# # +# **************************************************************************** # + +SHELL = bash + +NAME = Bureaucrat + +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/ex01/main.cpp b/ex01/main.cpp new file mode 100644 index 0000000..32e35ba --- /dev/null +++ b/ex01/main.cpp @@ -0,0 +1,51 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/12/23 14:01:11 by adjoly #+# #+# */ +/* Updated: 2025/03/29 15:44:03 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; + + su.signForm(f); + + std::cout << knl << std::endl; +}