d05 ex02 ok pour Shrubbery form

This commit is contained in:
hugogogo
2022-03-04 20:58:15 +01:00
parent 130b228a2f
commit 0a5827f3a1
14 changed files with 297 additions and 194 deletions

View File

@@ -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 <vector>
#include <functional>
#include <iostream>
int main()
{
std::vector<std::function<void()>> 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();
}
}
*/

Binary file not shown.

BIN
d05/ex02/28b_28c Executable file

Binary file not shown.

View File

@@ -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)

17
d05/ex02/Shrubbery.txt Normal file
View File

@@ -0,0 +1,17 @@
* *
* * *
* * * * *
* * * * *
* * * * * * *
* * * * * .# * *
* * * #. .# * *
* "#. #: #" * * *
* * * "#. ##" *
* "###
"##
##.
.##:
:###
;###
,####.
/\/\/\/\/\/.######.\/\/\/\/\

View File

@@ -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

View File

@@ -6,8 +6,8 @@
# include <string>
# include <stdexcept>
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:

View File

@@ -4,6 +4,7 @@
# include "color.h"
# include <iostream>
# include <string>
# include <fstream>
# 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

Binary file not shown.

View File

@@ -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";

103
d05/ex02/srcs/AForm.cpp Normal file
View File

@@ -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);
}

View File

@@ -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";
}
}

View File

@@ -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);
}

View File

@@ -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();
}