diff --git a/d05/ex02/main.cpp b/d05/ex02/main.cpp index 002a817..d4d113d 100644 --- a/d05/ex02/main.cpp +++ b/d05/ex02/main.cpp @@ -4,7 +4,7 @@ #include "RobotomyRequestForm.hpp" #include "PresidentialPardonForm.hpp" -#define N_TEST "7" +#define N_TEST "9" int main() { int i = 0; @@ -71,6 +71,26 @@ int main() { std::cout << s2 << "\n"; } + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " + << "Presidential copy test :" RESET "\n"; + { + PresidentialPardonForm p1("turnips"); + PresidentialPardonForm p2(p1); + + std::cout << p1 << "\n"; + std::cout << p2 << "\n"; + } + + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " + << "Robotomy copy test :" RESET "\n"; + { + RobotomyRequestForm r1("artichokes"); + RobotomyRequestForm r2(r1); + + std::cout << r1 << "\n"; + std::cout << r2 << "\n"; + } + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " << "Robotomy :" RESET "\n"; { diff --git a/d05/ex02/srcs/PresidentialPardonForm.cpp b/d05/ex02/srcs/PresidentialPardonForm.cpp index b22cffb..dfb9e15 100644 --- a/d05/ex02/srcs/PresidentialPardonForm.cpp +++ b/d05/ex02/srcs/PresidentialPardonForm.cpp @@ -13,7 +13,7 @@ PresidentialPardonForm::PresidentialPardonForm( std::string target ) } PresidentialPardonForm::PresidentialPardonForm( PresidentialPardonForm const & src ) -: AForm("presidential_creation", this->getTarget(), 25, 5) { +: AForm("presidential_creation", src.getTarget(), 25, 5) { std::cout << COPLIEN_COLOR "RobotomyRequestForm copy constructor" RESET "\n"; *this = src; return; diff --git a/d05/ex02/srcs/RobotomyRequestForm.cpp b/d05/ex02/srcs/RobotomyRequestForm.cpp index a361f4a..468f1fd 100644 --- a/d05/ex02/srcs/RobotomyRequestForm.cpp +++ b/d05/ex02/srcs/RobotomyRequestForm.cpp @@ -13,7 +13,7 @@ RobotomyRequestForm::RobotomyRequestForm( std::string target ) } RobotomyRequestForm::RobotomyRequestForm( RobotomyRequestForm const & src ) -: AForm("robotomy_creation", this->getTarget(), 72, 45) { +: AForm("robotomy_creation", src.getTarget(), 72, 45) { std::cout << COPLIEN_COLOR "RobotomyRequestForm copy constructor" RESET "\n"; *this = src; return; diff --git a/d05/ex02/srcs/ShrubberyCreationForm.cpp b/d05/ex02/srcs/ShrubberyCreationForm.cpp index f018052..264f883 100644 --- a/d05/ex02/srcs/ShrubberyCreationForm.cpp +++ b/d05/ex02/srcs/ShrubberyCreationForm.cpp @@ -13,7 +13,7 @@ ShrubberyCreationForm::ShrubberyCreationForm( std::string target ) } ShrubberyCreationForm::ShrubberyCreationForm( ShrubberyCreationForm const & src ) -: AForm("shrubbery_creation", this->getTarget(), 145, 137) { +: AForm("shrubbery_creation", src.getTarget(), 145, 137) { std::cout << COPLIEN_COLOR "ShrubberyCreationForm copy constructor" RESET "\n"; *this = src; return; diff --git a/d05/ex03/Makefile b/d05/ex03/Makefile new file mode 100644 index 0000000..7b56a70 --- /dev/null +++ b/d05/ex03/Makefile @@ -0,0 +1,86 @@ +# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # +# . name = value \ . += append to a variable # +# VARIABLES . value . != set result of command # +# . name is case sensitive . ?= set if not already set # +# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # + +NAME = interns + +#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 \ + AForm.cpp \ + ShrubberyCreationForm.cpp \ + RobotomyRequestForm.cpp \ + PresidentialPardonForm.cpp \ + Intern.cpp + +D_HEADERS = headers +HEADERS = colors.h \ + Bureaucrat.hpp \ + AForm.hpp \ + ShrubberyCreationForm.hpp \ + RobotomyRequestForm.hpp \ + PresidentialPardonForm.hpp \ + Intern.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) + +leaks: $(NAME) + valgrind --leak-check=full --show-leak-kinds=all ./$(NAME) + +clean: + $(RM_OBJS) + rm -f *_shrubbery + +fclean: clean + rm -f $(NAME) + +re: fclean all + +.PHONY : all clean fclean re + diff --git a/d05/ex03/headers/AForm.hpp b/d05/ex03/headers/AForm.hpp new file mode 100644 index 0000000..00646af --- /dev/null +++ b/d05/ex03/headers/AForm.hpp @@ -0,0 +1,54 @@ +#ifndef AFORM_HPP +# define AFORM_HPP + +# include "color.h" +# include +# include + +class Bureaucrat; +# include "Bureaucrat.hpp" + +class AForm { + +public: + + AForm( std::string name, std::string target, int signedGrade, int executeGrade ); + AForm( AForm const & src ); + virtual ~AForm() = 0; + AForm & operator=( AForm const & rhs ); + + std::string getName() const; + std::string getTarget() const; + bool getSigned() const; + int getSignedGrade() const; + int getExecuteGrade() const; + + void beSigned( Bureaucrat const & b ); + void execute(Bureaucrat const & executor) const; + virtual void formAction() const = 0; + +private: + + AForm(); + +protected: + + class GradeTooHighException : public std::exception { + const char * what() const throw();}; + class GradeTooLowException : public std::exception { + const char * what() const throw();}; + class NotSignedException : public std::exception { + const char * what() const throw();}; + + std::string const _name; + std::string const _target; + bool _signed; + int const _signedGrade; + int const _executeGrade; + +}; + +std::ostream & operator<<(std::ostream & o, AForm const & rhs); + +#endif + diff --git a/d05/ex03/headers/Bureaucrat.hpp b/d05/ex03/headers/Bureaucrat.hpp new file mode 100644 index 0000000..8bafb48 --- /dev/null +++ b/d05/ex03/headers/Bureaucrat.hpp @@ -0,0 +1,48 @@ +#ifndef BUREAUCRAT_HPP +# define BUREAUCRAT_HPP + +# include "color.h" +# include +# include +# include + +class AForm; +# include "AForm.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( AForm & f ); + void executeForm( AForm const & form ); + +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/ex03/headers/Intern.hpp b/d05/ex03/headers/Intern.hpp new file mode 100644 index 0000000..59b03d5 --- /dev/null +++ b/d05/ex03/headers/Intern.hpp @@ -0,0 +1,40 @@ +#ifndef INTERN_HPP +# define INTERN_HPP + +# include "colors.h" +# include +# include + +# include +# include +# include +# include + +typedef AForm * (*t_func)(std::string const & target); + +typedef struct s_formModel { + std::string const name; + t_func const create; +} t_formModel; + +class Intern { + +public: + Intern(); + Intern( Intern const & src ); + ~Intern(); + Intern & operator=( Intern const & rhs ); + + AForm * makeForm(std::string formName, std::string formTarget) const; + +private: + static const t_formModel _chooseForm[]; + + static AForm * makeShrubbery(std::string const &target); + static AForm * makePresidential(std::string const &target); + static AForm * makeRobotomy(std::string const &target); + +}; + +#endif + diff --git a/d05/ex03/headers/PresidentialPardonForm.hpp b/d05/ex03/headers/PresidentialPardonForm.hpp new file mode 100644 index 0000000..027661a --- /dev/null +++ b/d05/ex03/headers/PresidentialPardonForm.hpp @@ -0,0 +1,28 @@ +#ifndef PRESIDENTIALPARDONFORM_HPP +# define PRESIDENTIALPARDONFORM_HPP + +# include "color.h" +# include +# include +# include + +# include "AForm.hpp" + +class PresidentialPardonForm : public AForm { + +public: + + PresidentialPardonForm( std::string target ); + PresidentialPardonForm( PresidentialPardonForm const & src ); + ~PresidentialPardonForm(); + PresidentialPardonForm & operator=( PresidentialPardonForm const & rhs ); + + void formAction() const; + +private: + + PresidentialPardonForm(); +}; + +#endif + diff --git a/d05/ex03/headers/RobotomyRequestForm.hpp b/d05/ex03/headers/RobotomyRequestForm.hpp new file mode 100644 index 0000000..abeb7a9 --- /dev/null +++ b/d05/ex03/headers/RobotomyRequestForm.hpp @@ -0,0 +1,28 @@ +#ifndef ROBOTOMYREQUESTFORM_HPP +# define ROBOTOMYREQUESTFORM_HPP + +# include "color.h" +# include +# include +# include + +# include "AForm.hpp" + +class RobotomyRequestForm : public AForm { + +public: + + RobotomyRequestForm( std::string target ); + RobotomyRequestForm( RobotomyRequestForm const & src ); + ~RobotomyRequestForm(); + RobotomyRequestForm & operator=( RobotomyRequestForm const & rhs ); + + void formAction() const; + +private: + + RobotomyRequestForm(); +}; + +#endif + diff --git a/d05/ex03/headers/ShrubberyCreationForm.hpp b/d05/ex03/headers/ShrubberyCreationForm.hpp new file mode 100644 index 0000000..0906a12 --- /dev/null +++ b/d05/ex03/headers/ShrubberyCreationForm.hpp @@ -0,0 +1,28 @@ +#ifndef SHRUBBERYCREATIONFORM_HPP +# define SHRUBBERYCREATIONFORM_HPP + +# include "color.h" +# include +# include +# include + +# include "AForm.hpp" + +class ShrubberyCreationForm : public AForm { + +public: + + ShrubberyCreationForm( std::string target ); + ShrubberyCreationForm( ShrubberyCreationForm const & src ); + ~ShrubberyCreationForm(); + ShrubberyCreationForm & operator=( ShrubberyCreationForm const & rhs ); + + void formAction() const; + +private: + + ShrubberyCreationForm(); +}; + +#endif + diff --git a/d05/ex03/headers/color.h b/d05/ex03/headers/color.h new file mode 100644 index 0000000..f40596b --- /dev/null +++ b/d05/ex03/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/ex03/headers/colors.h b/d05/ex03/headers/colors.h new file mode 100644 index 0000000..0374e42 --- /dev/null +++ b/d05/ex03/headers/colors.h @@ -0,0 +1,25 @@ +#ifndef COLORS_H +# define COLORS_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/ex03/main.cpp b/d05/ex03/main.cpp new file mode 100644 index 0000000..e91fc1d --- /dev/null +++ b/d05/ex03/main.cpp @@ -0,0 +1,182 @@ +#include "colors.h" +#include "Bureaucrat.hpp" +#include "AForm.hpp" +#include "ShrubberyCreationForm.hpp" +#include "RobotomyRequestForm.hpp" +#include "PresidentialPardonForm.hpp" +#include "Intern.hpp" + +#define N_TEST "10" + +int main() { + int i = 0; + + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " + << "Shrubbery ok :" RESET "\n"; + { + Bureaucrat b("natasha", 1); + ShrubberyCreationForm s("sekoia"); + + std::cout << s << '\n'; + std::cout << b << '\n'; + std::cout << B_BLUE "b.signForm :" RESET "\n"; + b.signForm(s); + b.executeForm(s); + std::cout << s << '\n'; + } + + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " + << "Shrubbery too low sign and execute :" RESET "\n"; + { + Bureaucrat b("jordan", 150); + ShrubberyCreationForm s("chemney"); + + std::cout << s << '\n'; + std::cout << b << '\n'; + std::cout << B_BLUE "b.signForm :" RESET "\n"; + b.signForm(s); + b.executeForm(s); + std::cout << s << '\n'; + } + + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " + << "Shrubbery too low execute only :" RESET "\n"; + { + Bureaucrat b("bernadette", 140); + ShrubberyCreationForm s("rutabaga"); + + std::cout << s << '\n'; + std::cout << b << '\n'; + std::cout << B_BLUE "b.signForm :" RESET "\n"; + b.signForm(s); + b.executeForm(s); + std::cout << s << '\n'; + } + + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " + << "Bureaucrat copy test :" RESET "\n"; + { + Bureaucrat b1("pantoufle", 14); + Bureaucrat b2(b1); + + std::cout << b1 << "\n"; + std::cout << b2 << "\n"; + } + + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " + << "Shrubbery copy test :" RESET "\n"; + { + ShrubberyCreationForm s1("rutabagas"); + ShrubberyCreationForm s2(s1); + + std::cout << s1 << "\n"; + std::cout << s2 << "\n"; + } + + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " + << "Presidential copy test :" RESET "\n"; + { + PresidentialPardonForm p1("turnips"); + PresidentialPardonForm p2(p1); + + std::cout << p1 << "\n"; + std::cout << p2 << "\n"; + } + + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " + << "Robotomy copy test :" RESET "\n"; + { + RobotomyRequestForm r1("artichokes"); + RobotomyRequestForm r2(r1); + + std::cout << r1 << "\n"; + std::cout << r2 << "\n"; + } + + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " + << "Robotomy :" RESET "\n"; + { + Bureaucrat b("romeo", 15); + RobotomyRequestForm r("oven"); + + std::cout << r << '\n'; + std::cout << b << '\n'; + std::cout << B_BLUE "b.signForm :" RESET "\n"; + b.signForm(r); + b.executeForm(r); + b.executeForm(r); + b.executeForm(r); + b.executeForm(r); + b.executeForm(r); + b.executeForm(r); + b.executeForm(r); + b.executeForm(r); + b.executeForm(r); + b.executeForm(r); + b.executeForm(r); + b.executeForm(r); + b.executeForm(r); + b.executeForm(r); + std::cout << r << '\n'; + } + + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " + << "Presidential :" RESET "\n"; + { + Bureaucrat b("sylvestre", 1); + PresidentialPardonForm p("queen"); + + std::cout << p << '\n'; + std::cout << b << '\n'; + std::cout << B_BLUE "b.signForm :" RESET "\n"; + b.signForm(p); + b.executeForm(p); + std::cout << p << '\n'; + } + + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " + << "intern test :" RESET "\n"; + { + Intern i; + AForm * f1; + AForm * f2; + AForm * f3; + AForm * f4; + + std::cout << B_BLUE "intern tries to create a Shrubbery form" RESET "\n"; + f1 = i.makeForm("Shrubbery", "smoking"); + if (f1) + std::cout << *f1 << '\n'; + else + std::cout << "intern make an error and cannot create the form\n"; + + std::cout << B_BLUE "\nintern tries to create a Robotomy form" RESET "\n"; + f2 = i.makeForm("Robotomy", "building"); + if (f2) + std::cout << *f2 << '\n'; + else + std::cout << "intern make an error and cannot create the form\n"; + + std::cout << B_BLUE "\nintern tries to create a Presidential form" RESET "\n"; + f3 = i.makeForm("Presidential", "building"); + if (f3) + std::cout << *f3 << '\n'; + else + std::cout << "intern make an error and cannot create the form\n"; + + std::cout << B_BLUE "\nintern tries to create a blueberry form" RESET "\n"; + f4 = i.makeForm("blueberry", "giv_me_gold"); + if (f4) + std::cout << *f4 << '\n'; + else + std::cout << "intern make an error and cannot create the form\n"; + + delete f1; + delete f2; + delete f3; + delete f4; + } + + std::cout << "\n"; + return 0; +} diff --git a/d05/ex03/srcs/AForm.cpp b/d05/ex03/srcs/AForm.cpp new file mode 100644 index 0000000..0d6fa3b --- /dev/null +++ b/d05/ex03/srcs/AForm.cpp @@ -0,0 +1,103 @@ +#include "AForm.hpp" +#define COPLIEN_COLOR B_CYAN + +/********************************************* + * CONSTRUCTORS + *********************************************/ + +AForm::AForm( std::string name, std::string target, int signedGrade, int executeGrade ) +: _name(name) +, _target(target) +, _signed(false) +, _signedGrade(signedGrade) +, _executeGrade(executeGrade) { + if (signedGrade > 150 || executeGrade > 150) + throw AForm::GradeTooLowException(); + if (signedGrade < 1 || executeGrade < 1) + throw AForm::GradeTooHighException(); + std::cout << COPLIEN_COLOR "AForm constructor" RESET "\n"; + return; +} + +AForm::AForm( AForm const & src ) +: _name(src.getName()) +, _signedGrade(src.getSignedGrade()) +, _executeGrade(src.getExecuteGrade()) { + std::cout << COPLIEN_COLOR "AForm copy constructor" RESET "\n"; + *this = src; + return; +} + +/********************************************* + * DESTRUCTORS + *********************************************/ + +AForm::~AForm() { + std::cout << COPLIEN_COLOR "AForm destructor" RESET "\n"; + return; +} + +/********************************************* + * OPERATORS + *********************************************/ + +AForm & AForm::operator=( AForm const & rhs ) { + if ( this != &rhs ) { + _signed = rhs.getSigned(); + } + return *this; +} + +std::ostream & operator<<(std::ostream & o, AForm const & rhs) +{ + o << "[form name]" + << rhs.getName() << ", [target]" + << rhs.getTarget() << ", [signed]" + << rhs.getSigned() << ", [sign grade]" + << rhs.getSignedGrade() << ", [exec grade]" + << rhs.getExecuteGrade(); + return (o); +} + +/********************************************* + * ACCESSORS + *********************************************/ + +std::string AForm::getName() const {return _name;} +std::string AForm::getTarget() const {return _target;} +bool AForm::getSigned() const {return _signed;} +int AForm::getSignedGrade() const {return _signedGrade;} +int AForm::getExecuteGrade() const {return _executeGrade;} + +/********************************************* + * PUBLIC MEMBER FUNCTIONS + *********************************************/ + +void AForm::beSigned( Bureaucrat const & b) { + if (b.getGrade() < _signedGrade) + _signed = true; + else + throw AForm::GradeTooLowException(); +} + +void AForm::execute(Bureaucrat const & executor) const { + if (!_signed) + throw NotSignedException(); + if (executor.getGrade() > _executeGrade) + throw GradeTooLowException(); + formAction(); +} + +/********************************************* + * NESTED CLASS + *********************************************/ + +const char * AForm::GradeTooHighException::what() const throw() { + return (B_RED "grade too high" RESET); +} +const char * AForm::GradeTooLowException::what() const throw() { + return (B_RED "grade too low" RESET); +} +const char * AForm::NotSignedException::what() const throw() { + return (B_RED "form is not signed" RESET); +} diff --git a/d05/ex03/srcs/Bureaucrat.cpp b/d05/ex03/srcs/Bureaucrat.cpp new file mode 100644 index 0000000..83c2b2c --- /dev/null +++ b/d05/ex03/srcs/Bureaucrat.cpp @@ -0,0 +1,117 @@ +#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 ) +: _name(src.getName()) { + 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( AForm & f) { + try { + f.beSigned( *this ); + std::cout << _name << " signed " + << f.getName() << "(" + << f.getTarget() << ")\n"; + } + catch (std::exception & e) { + std::cout << _name << " couldn't sign " << f.getName() + << f.getName() << "(" + << f.getTarget() << ")" + << " because [" << e.what() << "]\n"; + } +} +void Bureaucrat::executeForm( AForm const & f ) { + try { + f.execute( *this ); + std::cout << _name << " executed " + << f.getName() << "(" + << f.getTarget() << ")\n"; + } + catch (std::exception & e) { + std::cout << _name << " couldn't execute " + << f.getName() << "(" + << f.getTarget() << ")" + << " 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/ex03/srcs/Intern.cpp b/d05/ex03/srcs/Intern.cpp new file mode 100644 index 0000000..ee5ebd1 --- /dev/null +++ b/d05/ex03/srcs/Intern.cpp @@ -0,0 +1,93 @@ +#include "Intern.hpp" + +#define COPLIEN_COLOR B_CYAN + +/********************************************* + * STATICS + *********************************************/ + +const t_formModel Intern::_chooseForm[] = { + {"Shrubbery", Intern::makeShrubbery}, + {"Presidential", Intern::makePresidential}, + {"Robotomy", Intern::makeRobotomy} +}; + +/********************************************* + * CONSTRUCTORS + *********************************************/ + +Intern::Intern() { + std::cout << COPLIEN_COLOR "Intern constructor" RESET "\n"; + return; +} + +Intern::Intern( Intern const & src __attribute__((unused))) { + std::cout << COPLIEN_COLOR "Intern copy constructor" RESET "\n"; + *this = src; + return; +} + +/********************************************* + * DESTRUCTORS + *********************************************/ + +Intern::~Intern() { + std::cout << COPLIEN_COLOR "Intern destructor" RESET "\n"; + return; +} + +/********************************************* + * OPERATORS + *********************************************/ + +Intern & Intern::operator=( Intern const & rhs __attribute__((unused))) { + return *this; +} + +//std::ostream & operator<<(std::ostream & o, Intern const & rhs) +//{ +// o << rhs.getFoo(); +// return (o); +//} + +/********************************************* + * ACCESSORS + *********************************************/ + +//std::string Intern::getFoo() const {return _foo;} + +/********************************************* + * PRIVATE MEMBER FUNCTIONS + *********************************************/ + +AForm * Intern::makeForm(std::string formName, std::string formTarget) const { + unsigned int len = sizeof _chooseForm / sizeof _chooseForm[0]; + + for (unsigned int i = 0; i < len; i++) + if (!formName.compare(_chooseForm[i].name)) + { + std::cout << "Intern creates " << formName << "\n"; + return _chooseForm[i].create(formTarget); + } + std::cout << "Intern cannot create " << formName << "\n"; + return NULL; +} + +/********************************************* + * PUBLIC MEMBER FUNCTIONS + *********************************************/ + +AForm * Intern::makeShrubbery(std::string const &target) { + return new ShrubberyCreationForm(target);} +AForm * Intern::makePresidential(std::string const &target) { + return new PresidentialPardonForm(target);} +AForm * Intern::makeRobotomy(std::string const &target) { + return new RobotomyRequestForm(target);} + +/********************************************* + * NESTED CLASS + *********************************************/ + +//void Intern::Class::function() {} + + diff --git a/d05/ex03/srcs/PresidentialPardonForm.cpp b/d05/ex03/srcs/PresidentialPardonForm.cpp new file mode 100644 index 0000000..9cc2ac6 --- /dev/null +++ b/d05/ex03/srcs/PresidentialPardonForm.cpp @@ -0,0 +1,47 @@ +#include "PresidentialPardonForm.hpp" + +#define COPLIEN_COLOR B_CYAN + +/********************************************* + * CONSTRUCTORS + *********************************************/ + +PresidentialPardonForm::PresidentialPardonForm( std::string target ) +: AForm("presidential_creation", target, 25, 5){ + std::cout << COPLIEN_COLOR "RobotomyRequestForm constructor" RESET "\n"; + return; +} + +PresidentialPardonForm::PresidentialPardonForm( PresidentialPardonForm const & src ) +: AForm("presidential_creation", src.getTarget(), 25, 5) { + std::cout << COPLIEN_COLOR "PresidentialPardonForm copy constructor" RESET "\n"; + *this = src; + return; +} + +/********************************************* + * DESTRUCTORS + *********************************************/ + +PresidentialPardonForm::~PresidentialPardonForm() { + std::cout << COPLIEN_COLOR "RobotomyRequestForm destructor" RESET "\n"; + return; +} + +/********************************************* + * OPERATORS + *********************************************/ + +PresidentialPardonForm & PresidentialPardonForm::operator=( PresidentialPardonForm const & rhs ) { + AForm::operator=(rhs); + return *this; +} + +/********************************************* + * PUBLIC MEMBER FUNCTIONS + *********************************************/ + +void PresidentialPardonForm::formAction() const { + std::cout << "Zaphod Beeblebrox pardon " << _target << "\n"; +} + diff --git a/d05/ex03/srcs/RobotomyRequestForm.cpp b/d05/ex03/srcs/RobotomyRequestForm.cpp new file mode 100644 index 0000000..468f1fd --- /dev/null +++ b/d05/ex03/srcs/RobotomyRequestForm.cpp @@ -0,0 +1,51 @@ +#include "RobotomyRequestForm.hpp" + +#define COPLIEN_COLOR B_CYAN + +/********************************************* + * CONSTRUCTORS + *********************************************/ + +RobotomyRequestForm::RobotomyRequestForm( std::string target ) +: AForm("robotomy_creation", target, 72, 45){ + std::cout << COPLIEN_COLOR "RobotomyRequestForm constructor" RESET "\n"; + return; +} + +RobotomyRequestForm::RobotomyRequestForm( RobotomyRequestForm const & src ) +: AForm("robotomy_creation", src.getTarget(), 72, 45) { + std::cout << COPLIEN_COLOR "RobotomyRequestForm copy constructor" RESET "\n"; + *this = src; + return; +} + +/********************************************* + * DESTRUCTORS + *********************************************/ + +RobotomyRequestForm::~RobotomyRequestForm() { + std::cout << COPLIEN_COLOR "RobotomyRequestForm destructor" RESET "\n"; + return; +} + +/********************************************* + * OPERATORS + *********************************************/ + +RobotomyRequestForm & RobotomyRequestForm::operator=( RobotomyRequestForm const & rhs ) { + AForm::operator=(rhs); + return *this; +} + +/********************************************* + * PUBLIC MEMBER FUNCTIONS + *********************************************/ + +void RobotomyRequestForm::formAction() const { + std::cout << "*drill sounds*\n"; + if (std::rand() % 2) + std::cout << _target << " robotomized with success\n"; + else + std::cout << _target << " robotomization failed\n"; +} + diff --git a/d05/ex03/srcs/ShrubberyCreationForm.cpp b/d05/ex03/srcs/ShrubberyCreationForm.cpp new file mode 100644 index 0000000..264f883 --- /dev/null +++ b/d05/ex03/srcs/ShrubberyCreationForm.cpp @@ -0,0 +1,74 @@ +#include "ShrubberyCreationForm.hpp" + +#define COPLIEN_COLOR B_CYAN + +/********************************************* + * CONSTRUCTORS + *********************************************/ + +ShrubberyCreationForm::ShrubberyCreationForm( std::string target ) +: AForm("shrubbery_creation", target, 145, 137){ + std::cout << COPLIEN_COLOR "ShrubberyCreationForm constructor" RESET "\n"; + return; +} + +ShrubberyCreationForm::ShrubberyCreationForm( ShrubberyCreationForm const & src ) +: AForm("shrubbery_creation", src.getTarget(), 145, 137) { + std::cout << COPLIEN_COLOR "ShrubberyCreationForm copy constructor" RESET "\n"; + *this = src; + return; +} + +/********************************************* + * DESTRUCTORS + *********************************************/ + +ShrubberyCreationForm::~ShrubberyCreationForm() { + std::cout << COPLIEN_COLOR "ShrubberyCreationForm destructor" RESET "\n"; + return; +} + +/********************************************* + * OPERATORS + *********************************************/ + +ShrubberyCreationForm & ShrubberyCreationForm::operator=( ShrubberyCreationForm const & rhs ) { + AForm::operator=(rhs); + return *this; +} + +/********************************************* + * PUBLIC MEMBER FUNCTIONS + *********************************************/ + +void ShrubberyCreationForm::formAction() const { + std::string name = _target + "_shrubbery"; + std::ofstream ofs(name.c_str(), std::ofstream::out); + + if (!ofs) + { + std::cout << "opening Shrubbery.txt file failed\n"; + return ; + } + + ofs << " * *\n"; + ofs << " * * *\n"; + ofs << " * * * * *\n"; + ofs << " * * * * *\n"; + ofs << " * * * * * * *\n"; + ofs << " * * * * * .# * *\n"; + ofs << " * * * #. .# * *\n"; + ofs << " * \"#. #: #\" * * *\n"; + ofs << " * * * \"#. ##\" *\n"; + ofs << " * \"###\n"; + ofs << " \"##\n"; + ofs << " ##.\n"; + ofs << " .##:\n"; + ofs << " :###\n"; + ofs << " ;###\n"; + ofs << " ,####.\n"; + ofs << " /\\/\\/\\/\\/\\/.######.\\/\\/\\/\\/\\\n"; + + ofs.close(); +} +