diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..7a2deec --- /dev/null +++ b/.clang-format @@ -0,0 +1,5 @@ +UseTab: Always +IndentWidth: 4 +TabWidth: 4 +AlignConsecutiveDeclarations: true +ConstructorInitializerIndentWidth: 4 diff --git a/ex00/Bureaucrat b/ex00/Bureaucrat new file mode 100755 index 0000000..55d06a9 Binary files /dev/null and b/ex00/Bureaucrat differ diff --git a/ex00/Bureaucrat.cpp b/ex00/Bureaucrat.cpp new file mode 100644 index 0000000..c7492a0 --- /dev/null +++ b/ex00/Bureaucrat.cpp @@ -0,0 +1,57 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Bureaucrat.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/12/23 13:59:45 by adjoly #+# #+# */ +/* Updated: 2025/03/08 20:00:23 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Bureaucrat.hpp" + +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; +} diff --git a/ex00/Bureaucrat.hpp b/ex00/Bureaucrat.hpp new file mode 100644 index 0000000..8664356 --- /dev/null +++ b/ex00/Bureaucrat.hpp @@ -0,0 +1,85 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Bureaucrat.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/12/23 13:59:43 by adjoly #+# #+# */ +/* Updated: 2025/03/08 19:58:18 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include +#include +#include +#include + +typedef unsigned char uint8_t; + +inline 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 +} + +#define MAXGRADE 1 +#define MINGRADE 150 + +class Bureaucrat { + public: + Bureaucrat(void) { + _log("➕", "Bureaucrat", "", "default constructor called"); + } + Bureaucrat(const Bureaucrat &cpy) { + _log("➕", "Bureaucrat", "", "copy constructor called"); + if (&cpy != this) + *this = cpy; + } + Bureaucrat(std::string name, uint8_t grade) : _name(name), _grade(grade) { + _log("➕", "Bureaucrat", "", "constructor called"); + } + ~Bureaucrat(void) { _log("➖", "Bureaucrat", "", "destructor called"); } + + const std::string &getName(void) { return _name; } + uint8_t getGrade(void) { return _grade; } + + class GradeTooHighException : public std::exception { + public: + virtual const char *what() const throw() { + return ("Grade is too high"); + } + }; + class GradeTooLowException : public std::exception { + public: + virtual const char *what() const throw() { + return ("Grade is too low"); + } + }; + + // Copy assignement operator + Bureaucrat &operator=(Bureaucrat const &); + + // Preincrement operator + Bureaucrat &operator++(void); + Bureaucrat &operator--(void); + + // Post increment operator + Bureaucrat operator++(int); + Bureaucrat operator--(int); + + private: + const 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..46eecde --- /dev/null +++ b/ex00/Makefile @@ -0,0 +1,60 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: adjoly +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2024/10/25 16:09:27 by adjoly #+# #+# # +# Updated: 2025/03/08 19:55:47 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/ex00/main.cpp b/ex00/main.cpp new file mode 100644 index 0000000..566beeb --- /dev/null +++ b/ex00/main.cpp @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/12/23 14:01:11 by adjoly #+# #+# */ +/* Updated: 2025/03/08 20:04:22 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Bureaucrat.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; + } + std::cout << knl << std::endl; +}