diff --git a/d04/ex02/main.cpp b/d04/ex02/main.cpp index 5166e80..b443872 100644 --- a/d04/ex02/main.cpp +++ b/d04/ex02/main.cpp @@ -9,7 +9,9 @@ #define N_TEST "7" int main() { - std::cout << B_YELLOW "\n[1/" N_TEST "] test subject :" RESET "\n"; + int i = 0; + + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] test subject :" RESET "\n"; { const Animal* j = new Dog(); const Animal* i = new Cat(); @@ -17,7 +19,7 @@ int main() { delete i; } - std::cout << B_YELLOW "\n[2/" N_TEST "] test with brain :" RESET "\n"; + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] test with brain :" RESET "\n"; { Dog * dog; Cat * cat1; @@ -64,7 +66,7 @@ int main() { delete brain2; } - std::cout << B_YELLOW "\n[3/" N_TEST "] array animal test :" RESET "\n"; + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] array animal test :" RESET "\n"; { Animal *animals[10]; int i; @@ -79,7 +81,7 @@ int main() { delete animals[i]; } - std::cout << B_YELLOW "\n[4/" N_TEST "] copy constructor test1/2 :" RESET "\n"; + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] copy constructor test1/2 :" RESET "\n"; { std::cout << B_BLUE "Cat a_cat :" RESET "\n"; Cat a_cat; @@ -87,7 +89,7 @@ int main() { Cat a_cpy_cat(a_cat); } - std::cout << B_YELLOW "\n[5/" N_TEST "] copy constructor test2/2 :" RESET "\n"; + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] copy constructor test2/2 :" RESET "\n"; { std::cout << B_BLUE "Cat a_cat :" RESET "\n"; Cat a_cat; @@ -95,7 +97,7 @@ int main() { Cat a_cpy_cat = a_cat; } - std::cout << B_YELLOW "\n[6/" N_TEST "] assignation operator test1 :" RESET "\n"; + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] assignation operator test1 :" RESET "\n"; { std::cout << B_BLUE "Cat a_cat :" RESET "\n"; Cat a_cat; @@ -105,7 +107,7 @@ int main() { a_cpy_cat = a_cat; } - std::cout << B_YELLOW "\n[7/" N_TEST "] assignation operator test2 :" RESET "\n"; + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] assignation operator test2 :" RESET "\n"; { std::cout << B_BLUE "const Cat *a_cat :" RESET "\n"; const Cat *a_cat = new Cat(); @@ -117,7 +119,32 @@ int main() { delete a_cat; } - return 0; } +/* +#include +#include +#include + +int main() +{ + std::vector> tests; + tests.push_back( + [](){ + std::cout << "hello test\n"; + } + ); + tests.push_back( + [](){ + std::cout << "hello another test.\n"; + } + ); + + int counter = 0; + for (const auto& test : tests){ + std::cout << ++counter << "/" << tests.size() << " "; + test(); + } +} +*/ diff --git a/d04/ex02/pure b/d04/ex02/pure index fe31b46..52f6480 100755 Binary files a/d04/ex02/pure and b/d04/ex02/pure differ diff --git a/d05/ex02/28b_28c b/d05/ex02/28b_28c new file mode 100755 index 0000000..4298f09 Binary files /dev/null and b/d05/ex02/28b_28c differ diff --git a/d05/ex02/Makefile b/d05/ex02/Makefile index cb65767..53d7be2 100644 --- a/d05/ex02/Makefile +++ b/d05/ex02/Makefile @@ -4,7 +4,7 @@ # . name is case sensitive . ?= set if not already set # # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # -NAME = larves +NAME = 28b_28c #CC = gcc CXX = c++ @@ -24,11 +24,13 @@ INCLUDES = -I$(D_HEADERS) D_SRCS = srcs SRCS = main.cpp \ Bureaucrat.cpp \ - Form.cpp + AForm.cpp \ + ShrubberyCreationForm.cpp D_HEADERS = headers HEADERS = Bureaucrat.hpp \ - Form.hpp + AForm.hpp \ + ShrubberyCreationForm.hpp D_OBJS = builds OBJS = $(SRCS:%.$(EXT)=$(D_OBJS)/%.o) diff --git a/d05/ex02/Shrubbery.txt b/d05/ex02/Shrubbery.txt new file mode 100644 index 0000000..5a1591f --- /dev/null +++ b/d05/ex02/Shrubbery.txt @@ -0,0 +1,17 @@ + * * + * * * + * * * * * + * * * * * + * * * * * * * + * * * * * .# * * + * * * #. .# * * + * "#. #: #" * * * + * * * "#. ##" * + * "### + "## + ##. + .##: + :### + ;### + ,####. + /\/\/\/\/\/.######.\/\/\/\/\ diff --git a/d05/ex02/headers/Form.hpp b/d05/ex02/headers/AForm.hpp similarity index 52% rename from d05/ex02/headers/Form.hpp rename to d05/ex02/headers/AForm.hpp index ea436b2..00646af 100644 --- a/d05/ex02/headers/Form.hpp +++ b/d05/ex02/headers/AForm.hpp @@ -12,35 +12,43 @@ class AForm { public: - Form( std::string name, int signedGrade, int executeGrade ); - Form( Form const & src ); - virtual ~Form() = 0; - Form & operator=( Form const & rhs ); + 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; - virtual void beSigned( Bureaucrat const & b ) = 0; + void beSigned( Bureaucrat const & b ); + void execute(Bureaucrat const & executor) const; + virtual void formAction() const = 0; private: - Form(); + 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, Form const & rhs); +std::ostream & operator<<(std::ostream & o, AForm const & rhs); #endif diff --git a/d05/ex02/headers/Bureaucrat.hpp b/d05/ex02/headers/Bureaucrat.hpp index 01d4e9f..8bafb48 100644 --- a/d05/ex02/headers/Bureaucrat.hpp +++ b/d05/ex02/headers/Bureaucrat.hpp @@ -6,8 +6,8 @@ # include # include -class Form; -# include "Form.hpp" +class AForm; +# include "AForm.hpp" class Bureaucrat { @@ -24,7 +24,8 @@ public: void gradeUp(); void gradeDown(); - void signForm( Form & f ); + void signForm( AForm & f ); + void executeForm( AForm const & form ); protected: diff --git a/d05/ex02/headers/ShrubberyCreationForm.hpp b/d05/ex02/headers/ShrubberyCreationForm.hpp index c4d1ab8..5b5c84e 100644 --- a/d05/ex02/headers/ShrubberyCreationForm.hpp +++ b/d05/ex02/headers/ShrubberyCreationForm.hpp @@ -4,6 +4,7 @@ # include "color.h" # include # include +# include # include "AForm.hpp" @@ -11,24 +12,21 @@ class ShrubberyCreationForm : public AForm { public: - ShrubberyCreationForm(); + ShrubberyCreationForm( std::string target ); ShrubberyCreationForm( ShrubberyCreationForm const & src ); ~ShrubberyCreationForm(); ShrubberyCreationForm & operator=( ShrubberyCreationForm const & rhs ); -// std::string getFoo() const; + void formAction() const; -protected: - -// std::string const _foo; +// std::string getTarget() const; private: -// static std::string const ShrubberyCreationForm::_bar; + ShrubberyCreationForm(); +// std::string const _target; }; -//std::ostream & operator<<(std::ostream & o, ShrubberyCreationForm const & rhs); - #endif diff --git a/d05/ex02/larves b/d05/ex02/larves deleted file mode 100755 index 922ce63..0000000 Binary files a/d05/ex02/larves and /dev/null differ diff --git a/d05/ex02/main.cpp b/d05/ex02/main.cpp index 2747ad5..07d22ec 100644 --- a/d05/ex02/main.cpp +++ b/d05/ex02/main.cpp @@ -1,64 +1,82 @@ #include "Bureaucrat.hpp" +#include "AForm.hpp" +#include "ShrubberyCreationForm.hpp" + #define N_TEST "5" int main() { + int i = 0; - std::cout << B_YELLOW "\n[1/" N_TEST "] test too high :" RESET "\n"; + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " + << "Shrubbery ok :" RESET "\n"; { - try { - Bureaucrat b("clarence", 151); - } - catch (std::exception& e) { - std::cout << e.what() << "\n"; - } + Bureaucrat b("natasha", 50); + 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[2/" N_TEST "] test signe ko :" RESET "\n"; + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " + << "Shrubbery too low signe and execute :" RESET "\n"; { - { - Bureaucrat b("mikel", 50); - Form f("serpa_hell", 30, 100); + Bureaucrat b("jordan", 150); + ShrubberyCreationForm s("chemney"); - 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 << 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[3/" N_TEST "] test signe ok :" RESET "\n"; + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " + << "Shrubbery too low execute only :" RESET "\n"; { - { - Bureaucrat b("coran", 50); - Form f("serpa_death", 70, 100); + Bureaucrat b("bernadette", 140); + ShrubberyCreationForm s("rutabaga"); - 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 << 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[4/" N_TEST "] test form too high :" RESET "\n"; + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " + << "Bureaucrat copy test :" RESET "\n"; { - try { - Form f("serpa_horror", 155, 100); - } - catch (std::exception& e) { - std::cout << e.what() << "\n"; - } + Bureaucrat b1("pantoufle", 14); + Bureaucrat b2(b1); + + std::cout << b1 << "\n"; + std::cout << b2 << "\n"; } - std::cout << B_YELLOW "\n[5/" N_TEST "] test form too low :" RESET "\n"; + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " + << "Shrubbery copy test :" RESET "\n"; + { + ShrubberyCreationForm s1("rutabaga"); + ShrubberyCreationForm s2(s1); + + std::cout << s1 << "\n"; + std::cout << s2 << "\n"; + } + + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " + << "Robotomy :" RESET "\n"; + { + } + + std::cout << B_YELLOW "\n[" << ++i << "/" N_TEST "] " + << "Presidential :" RESET "\n"; { - try { - Form f("serpa_starvation", 70, 0); - } - catch (std::exception& e) { - std::cout << e.what() << "\n"; - } } std::cout << "\n"; diff --git a/d05/ex02/srcs/AForm.cpp b/d05/ex02/srcs/AForm.cpp new file mode 100644 index 0000000..0d6fa3b --- /dev/null +++ b/d05/ex02/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/ex02/srcs/Bureaucrat.cpp b/d05/ex02/srcs/Bureaucrat.cpp index 8332551..83c2b2c 100644 --- a/d05/ex02/srcs/Bureaucrat.cpp +++ b/d05/ex02/srcs/Bureaucrat.cpp @@ -17,7 +17,8 @@ Bureaucrat::Bureaucrat( std::string name, int grade ) return; } -Bureaucrat::Bureaucrat( Bureaucrat const & src ) { +Bureaucrat::Bureaucrat( Bureaucrat const & src ) +: _name(src.getName()) { std::cout << COPLIEN_COLOR "Bureaucrat copy constructor" RESET "\n"; *this = src; return; @@ -75,13 +76,31 @@ void Bureaucrat::gradeDown() { throw GradeTooLowException(); } -void Bureaucrat::signForm( Form & f) { +void Bureaucrat::signForm( AForm & f) { try { f.beSigned( *this ); - std::cout << _name << " signed " << f.getName() << "\n"; + 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"; } } diff --git a/d05/ex02/srcs/Form.cpp b/d05/ex02/srcs/Form.cpp deleted file mode 100644 index e29a7f8..0000000 --- a/d05/ex02/srcs/Form.cpp +++ /dev/null @@ -1,90 +0,0 @@ -#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); -} diff --git a/d05/ex02/srcs/ShrubberyCreationForm.cpp b/d05/ex02/srcs/ShrubberyCreationForm.cpp index 82fd33e..576083d 100644 --- a/d05/ex02/srcs/ShrubberyCreationForm.cpp +++ b/d05/ex02/srcs/ShrubberyCreationForm.cpp @@ -6,13 +6,14 @@ * CONSTRUCTORS *********************************************/ -ShrubberyCreationForm::ShrubberyCreationForm( std::string foo = ShrubberyCreationForm::_bar ) -: _foo(foo) { +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 ) { +ShrubberyCreationForm::ShrubberyCreationForm( ShrubberyCreationForm const & src ) +: AForm("shrubbery_creation", this->getTarget(), 145, 137) { std::cout << COPLIEN_COLOR "ShrubberyCreationForm copy constructor" RESET "\n"; *this = src; return; @@ -32,42 +33,41 @@ ShrubberyCreationForm::~ShrubberyCreationForm() { *********************************************/ ShrubberyCreationForm & ShrubberyCreationForm::operator=( ShrubberyCreationForm const & rhs ) { -// Base::operator=(rhs); - if ( this != &rhs ) - { -// _foo = rhs.getFoo(); - } + AForm::operator=(rhs); return *this; } -//std::ostream & operator<<(std::ostream & o, ShrubberyCreationForm const & rhs) -//{ -// o << rhs.getFoo(); -// return (o); -//} - -/********************************************* - * ACCESSORS - *********************************************/ - -//std::string ShrubberyCreationForm::getFoo() const {return _foo;} - /********************************************* * PUBLIC MEMBER FUNCTIONS *********************************************/ -//void ShrubberyCreationForm::function(const std::string & foo) {} +void ShrubberyCreationForm::formAction() const { + std::ofstream ofs("Shrubbery.txt", std::ofstream::out); -/********************************************* - * NESTED CLASS - *********************************************/ + if (!ofs) + { + std::cout << "opening Shrubbery.txt file failed\n"; + return ; + } -//void ShrubberyCreationForm::Class::function() {} - -/********************************************* - * STATICS - *********************************************/ - -//std::string const ShrubberyCreationForm::_bar = "bar"; + 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(); +}