d05 ex02 ok pour Shrubbery form
This commit is contained in:
103
d05/ex02/srcs/AForm.cpp
Normal file
103
d05/ex02/srcs/AForm.cpp
Normal 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);
|
||||
}
|
||||
@@ -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";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user