diff --git a/d05/ex00/bureaucrat b/d05/ex00/bureaucrat index 7a8a4e8..64ea8f4 100755 Binary files a/d05/ex00/bureaucrat and b/d05/ex00/bureaucrat differ diff --git a/d05/ex00/headers/Bureaucrat.hpp b/d05/ex00/headers/Bureaucrat.hpp index 40359b4..5db8c17 100644 --- a/d05/ex00/headers/Bureaucrat.hpp +++ b/d05/ex00/headers/Bureaucrat.hpp @@ -15,15 +15,6 @@ public: ~Bureaucrat(); Bureaucrat & operator=( Bureaucrat const & rhs ); - class GradeTooHighException : public std::exception { - public: - const char * what() const throw(); - }; - class GradeTooLowException : public std::exception { - public: - const char * what() const throw(); - }; - std::string getName() const; int getGrade() const; @@ -38,6 +29,11 @@ protected: private: Bureaucrat(); + + class GradeTooHighException : public std::exception { + const char * what() const throw();}; + class GradeTooLowException : public std::exception { + const char * what() const throw();}; }; std::ostream & operator<<(std::ostream & o, Bureaucrat const & rhs); diff --git a/d05/ex00/srcs/Bureaucrat.cpp b/d05/ex00/srcs/Bureaucrat.cpp index 8d683d8..75fe978 100644 --- a/d05/ex00/srcs/Bureaucrat.cpp +++ b/d05/ex00/srcs/Bureaucrat.cpp @@ -80,8 +80,8 @@ void Bureaucrat::gradeDown() { *********************************************/ const char * Bureaucrat::GradeTooHighException::what() const throw() { - return ("grade higher than 1"); + return (B_RED "grade higher than 1" RESET); } const char * Bureaucrat::GradeTooLowException::what() const throw() { - return ("grade lower than 150"); + return (B_RED "grade lower than 150" RESET); } diff --git a/d05/ex01/Makefile b/d05/ex01/Makefile new file mode 100644 index 0000000..cb65767 --- /dev/null +++ b/d05/ex01/Makefile @@ -0,0 +1,73 @@ +# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # +# . name = value \ . += append to a variable # +# VARIABLES . value . != set result of command # +# . name is case sensitive . ?= set if not already set # +# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # + +NAME = larves + +#CC = gcc +CXX = c++ + +#CFLAGS = -Wall -Wextra -Werror $(INCLUDES) +CXXFLAGS = -Wall -Wextra -Werror $(INCLUDES) -std=c++98 + +#EXT = c +EXT = cpp + +VPATH = $(D_SRCS) + +LIBS = + +INCLUDES = -I$(D_HEADERS) + +D_SRCS = srcs +SRCS = main.cpp \ + Bureaucrat.cpp \ + Form.cpp + +D_HEADERS = headers +HEADERS = Bureaucrat.hpp \ + Form.hpp + +D_OBJS = builds +OBJS = $(SRCS:%.$(EXT)=$(D_OBJS)/%.o) + +ifeq "$(D_OBJS)" "." + RM_OBJS = rm -f $(OBJS) +else + RM_OBJS = rm -rf $(D_OBJS) +endif + + +# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # +# . target: prerequisites . $@ : target # +# RULES . recipe . $< : 1st prerequisite # +# . recipe . $^ : all prerequisites # +# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # + +all: $(NAME) + +$(D_OBJS)/%.o: %.$(EXT) | $(D_OBJS) +# $(CC) $(CFLAGS) -c $< -o $@ + $(CXX) $(CXXFLAGS) -c $< -o $@ + +$(D_OBJS): + mkdir $@ + +$(OBJS): $(HEADERS:%=$(D_HEADERS)/%) + +$(NAME): $(OBJS) +# $(CC) $(OBJS) -o $@ $(LIBS) + $(CXX) $(OBJS) -o $@ $(LIBS) + +clean: + $(RM_OBJS) + +fclean: clean + rm -f $(NAME) + +re: fclean all + +.PHONY : all clean fclean re + diff --git a/d05/ex01/headers/Bureaucrat.hpp b/d05/ex01/headers/Bureaucrat.hpp new file mode 100644 index 0000000..01d4e9f --- /dev/null +++ b/d05/ex01/headers/Bureaucrat.hpp @@ -0,0 +1,47 @@ +#ifndef BUREAUCRAT_HPP +# define BUREAUCRAT_HPP + +# include "color.h" +# include +# include +# include + +class Form; +# include "Form.hpp" + +class Bureaucrat { + +public: + + Bureaucrat(std::string name, int grade); + Bureaucrat( Bureaucrat const & src ); + ~Bureaucrat(); + Bureaucrat & operator=( Bureaucrat const & rhs ); + + std::string getName() const; + int getGrade() const; + + void gradeUp(); + void gradeDown(); + + void signForm( Form & f ); + +protected: + + std::string const _name; + int _grade; + +private: + + Bureaucrat(); + + class GradeTooHighException : public std::exception { + const char * what() const throw();}; + class GradeTooLowException : public std::exception { + const char * what() const throw();}; +}; + +std::ostream & operator<<(std::ostream & o, Bureaucrat const & rhs); + +#endif + diff --git a/d05/ex01/headers/Form.hpp b/d05/ex01/headers/Form.hpp new file mode 100644 index 0000000..9aa97e6 --- /dev/null +++ b/d05/ex01/headers/Form.hpp @@ -0,0 +1,46 @@ +#ifndef FORM_HPP +# define FORM_HPP + +# include "color.h" +# include +# include + +class Bureaucrat; +# include "Bureaucrat.hpp" + +class Form { + +public: + + Form( std::string name, int signedGrade, int executeGrade ); + Form( Form const & src ); + ~Form(); + Form & operator=( Form const & rhs ); + + std::string getName() const; + bool getSigned() const; + int getSignedGrade() const; + int getExecuteGrade() const; + + void beSigned( Bureaucrat const & b ); + +private: + + Form(); + + class GradeTooHighException : public std::exception { + const char * what() const throw();}; + class GradeTooLowException : public std::exception { + const char * what() const throw();}; + + std::string const _name; + bool _signed; + int const _signedGrade; + int const _executeGrade; + +}; + +std::ostream & operator<<(std::ostream & o, Form const & rhs); + +#endif + diff --git a/d05/ex01/headers/color.h b/d05/ex01/headers/color.h new file mode 100644 index 0000000..f40596b --- /dev/null +++ b/d05/ex01/headers/color.h @@ -0,0 +1,25 @@ +#ifndef COLOR_H +# define COLOR_H + +# define GRAY "\e[0;30m" +# define RED "\e[0;31m" +# define GREEN "\e[0;32m" +# define YELLOW "\e[0;33m" +# define BLUE "\e[0;34m" +# define PURPLE "\e[0;35m" +# define CYAN "\e[0;36m" +# define WHITE "\e[0;37m" + +# define B_GRAY "\e[1;30m" +# define B_RED "\e[1;31m" +# define B_GREEN "\e[1;32m" +# define B_YELLOW "\e[1;33m" +# define B_BLUE "\e[1;34m" +# define B_PURPLE "\e[1;35m" +# define B_CYAN "\e[1;36m" +# define B_WHITE "\e[1;37m" + +# define RESET "\e[0m" + +#endif + diff --git a/d05/ex01/larves b/d05/ex01/larves new file mode 100755 index 0000000..922ce63 Binary files /dev/null and b/d05/ex01/larves differ diff --git a/d05/ex01/main.cpp b/d05/ex01/main.cpp new file mode 100644 index 0000000..2747ad5 --- /dev/null +++ b/d05/ex01/main.cpp @@ -0,0 +1,66 @@ +#include "Bureaucrat.hpp" +#define N_TEST "5" + +int main() { + + std::cout << B_YELLOW "\n[1/" N_TEST "] test too high :" RESET "\n"; + { + try { + Bureaucrat b("clarence", 151); + } + catch (std::exception& e) { + std::cout << e.what() << "\n"; + } + } + + std::cout << B_YELLOW "\n[2/" N_TEST "] test signe ko :" RESET "\n"; + { + { + Bureaucrat b("mikel", 50); + Form f("serpa_hell", 30, 100); + + std::cout << f << '\n'; + std::cout << b << '\n'; + std::cout << B_BLUE "b.signForm :" RESET "\n"; + b.signForm(f); + std::cout << f << '\n'; + } + } + + std::cout << B_YELLOW "\n[3/" N_TEST "] test signe ok :" RESET "\n"; + { + { + Bureaucrat b("coran", 50); + Form f("serpa_death", 70, 100); + + std::cout << f << '\n'; + std::cout << b << '\n'; + std::cout << B_BLUE "b.signForm :" RESET "\n"; + b.signForm(f); + std::cout << f << '\n'; + } + } + + std::cout << B_YELLOW "\n[4/" N_TEST "] test form too high :" RESET "\n"; + { + try { + Form f("serpa_horror", 155, 100); + } + catch (std::exception& e) { + std::cout << e.what() << "\n"; + } + } + + std::cout << B_YELLOW "\n[5/" N_TEST "] test form too low :" RESET "\n"; + { + try { + Form f("serpa_starvation", 70, 0); + } + catch (std::exception& e) { + std::cout << e.what() << "\n"; + } + } + + std::cout << "\n"; + return 0; +} diff --git a/d05/ex01/srcs/Bureaucrat.cpp b/d05/ex01/srcs/Bureaucrat.cpp new file mode 100644 index 0000000..8332551 --- /dev/null +++ b/d05/ex01/srcs/Bureaucrat.cpp @@ -0,0 +1,98 @@ +#include "Bureaucrat.hpp" + +#define COPLIEN_COLOR B_CYAN + +/********************************************* + * CONSTRUCTORS + *********************************************/ + +Bureaucrat::Bureaucrat( std::string name, int grade ) +: _name(name) +, _grade(grade) { + if (grade > 150) + throw GradeTooLowException(); + if (grade < 1) + throw GradeTooHighException(); + std::cout << COPLIEN_COLOR "Bureaucrat constructor" RESET "\n"; + return; +} + +Bureaucrat::Bureaucrat( Bureaucrat const & src ) { + std::cout << COPLIEN_COLOR "Bureaucrat copy constructor" RESET "\n"; + *this = src; + return; +} + +/********************************************* + * DESTRUCTORS + *********************************************/ + +Bureaucrat::~Bureaucrat() { + std::cout << COPLIEN_COLOR "Bureaucrat destructor" RESET "\n"; + return; +} + +/********************************************* + * OPERATORS + *********************************************/ + +Bureaucrat & Bureaucrat::operator=( Bureaucrat const & rhs ) { + if ( this != &rhs ) + _grade = rhs.getGrade(); + return *this; +} + +std::ostream & operator<<(std::ostream & o, Bureaucrat const & rhs) +{ + o + << rhs.getName() << ", bureaucrat grade " + << rhs.getGrade(); + return (o); +} + +/********************************************* + * ACCESSORS + *********************************************/ + +std::string Bureaucrat::getName() const {return _name;} +int Bureaucrat::getGrade() const {return _grade;} + +/********************************************* + * PUBLIC MEMBER FUNCTIONS + *********************************************/ + +void Bureaucrat::gradeUp() { + if (_grade > 1) + _grade--; + else + throw GradeTooHighException(); +} + +void Bureaucrat::gradeDown() { + if (_grade < 150) + _grade++; + else + throw GradeTooLowException(); +} + +void Bureaucrat::signForm( Form & f) { + try { + f.beSigned( *this ); + std::cout << _name << " signed " << f.getName() << "\n"; + } + catch (std::exception & e) { + std::cout << _name << " couldn't sign " << f.getName() + << " because [" << e.what() << "]\n"; + } +} + +/********************************************* + * NESTED CLASS + *********************************************/ + +const char * Bureaucrat::GradeTooHighException::what() const throw() { + return (B_RED "grade higher than 1" RESET); +} +const char * Bureaucrat::GradeTooLowException::what() const throw() { + return (B_RED "grade lower than 150" RESET); +} diff --git a/d05/ex01/srcs/Form.cpp b/d05/ex01/srcs/Form.cpp new file mode 100644 index 0000000..e29a7f8 --- /dev/null +++ b/d05/ex01/srcs/Form.cpp @@ -0,0 +1,90 @@ +#include "Form.hpp" + +#define COPLIEN_COLOR B_CYAN + +/********************************************* + * CONSTRUCTORS + *********************************************/ + +Form::Form( std::string name, int signedGrade, int executeGrade ) +: _name(name) +, _signed(false) +, _signedGrade(signedGrade) +, _executeGrade(executeGrade) { + if (signedGrade > 150 || executeGrade > 150) + throw Form::GradeTooLowException(); + if (signedGrade < 1 || executeGrade < 1) + throw Form::GradeTooHighException(); + std::cout << COPLIEN_COLOR "Form constructor" RESET "\n"; + return; +} + +Form::Form( Form const & src ) +: _name(src.getName()) +, _signedGrade(src.getSignedGrade()) +, _executeGrade(src.getExecuteGrade()) { + std::cout << COPLIEN_COLOR "Form copy constructor" RESET "\n"; + *this = src; + return; +} + +/********************************************* + * DESTRUCTORS + *********************************************/ + +Form::~Form() { + std::cout << COPLIEN_COLOR "Form destructor" RESET "\n"; + return; +} + +/********************************************* + * OPERATORS + *********************************************/ + +Form & Form::operator=( Form const & rhs ) { + if ( this != &rhs ) { + _signed = rhs.getSigned(); + } + return *this; +} + +std::ostream & operator<<(std::ostream & o, Form const & rhs) +{ + o << "form: " + << rhs.getName() << ", is signed: " + << rhs.getSigned() << ", need at least [" + << rhs.getSignedGrade() << "] to be signed, need at least [" + << rhs.getExecuteGrade() << "] to be executed"; + return (o); +} + +/********************************************* + * ACCESSORS + *********************************************/ + +std::string Form::getName() const {return _name;} +bool Form::getSigned() const {return _signed;} +int Form::getSignedGrade() const {return _signedGrade;} +int Form::getExecuteGrade() const {return _executeGrade;} + +/********************************************* + * PUBLIC MEMBER FUNCTIONS + *********************************************/ + +void Form::beSigned( Bureaucrat const & b) { + if (b.getGrade() < _signedGrade) + _signed = true; + else + throw Form::GradeTooLowException(); +} + +/********************************************* + * NESTED CLASS + *********************************************/ + +const char * Form::GradeTooHighException::what() const throw() { + return (B_RED "grade too high" RESET); +} +const char * Form::GradeTooLowException::what() const throw() { + return (B_RED "grade too low" RESET); +}