Merge branch 'master' of bitbucket.org:hugogogo/piscine_cpp
This commit is contained in:
@@ -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";
|
||||
{
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
86
d05/ex03/Makefile
Normal file
86
d05/ex03/Makefile
Normal file
@@ -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
|
||||
|
||||
54
d05/ex03/headers/AForm.hpp
Normal file
54
d05/ex03/headers/AForm.hpp
Normal file
@@ -0,0 +1,54 @@
|
||||
#ifndef AFORM_HPP
|
||||
# define AFORM_HPP
|
||||
|
||||
# include "color.h"
|
||||
# include <iostream>
|
||||
# include <string>
|
||||
|
||||
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
|
||||
|
||||
48
d05/ex03/headers/Bureaucrat.hpp
Normal file
48
d05/ex03/headers/Bureaucrat.hpp
Normal file
@@ -0,0 +1,48 @@
|
||||
#ifndef BUREAUCRAT_HPP
|
||||
# define BUREAUCRAT_HPP
|
||||
|
||||
# include "color.h"
|
||||
# include <iostream>
|
||||
# include <string>
|
||||
# include <stdexcept>
|
||||
|
||||
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
|
||||
|
||||
44
d05/ex03/headers/Intern.hpp
Normal file
44
d05/ex03/headers/Intern.hpp
Normal file
@@ -0,0 +1,44 @@
|
||||
#ifndef INTERN_HPP
|
||||
# define INTERN_HPP
|
||||
|
||||
# include "colors.h"
|
||||
# include <iostream>
|
||||
# include <string>
|
||||
# include <algorithm> // trasnform
|
||||
# include <cctype> // tolower
|
||||
|
||||
# include <AForm.hpp>
|
||||
# include <ShrubberyCreationForm.hpp>
|
||||
# include <PresidentialPardonForm.hpp>
|
||||
# include <RobotomyRequestForm.hpp>
|
||||
|
||||
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);
|
||||
|
||||
bool isValidForm(std::string name, unsigned int i) const;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
28
d05/ex03/headers/PresidentialPardonForm.hpp
Normal file
28
d05/ex03/headers/PresidentialPardonForm.hpp
Normal file
@@ -0,0 +1,28 @@
|
||||
#ifndef PRESIDENTIALPARDONFORM_HPP
|
||||
# define PRESIDENTIALPARDONFORM_HPP
|
||||
|
||||
# include "color.h"
|
||||
# include <iostream>
|
||||
# include <string>
|
||||
# include <cstdlib>
|
||||
|
||||
# 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
|
||||
|
||||
28
d05/ex03/headers/RobotomyRequestForm.hpp
Normal file
28
d05/ex03/headers/RobotomyRequestForm.hpp
Normal file
@@ -0,0 +1,28 @@
|
||||
#ifndef ROBOTOMYREQUESTFORM_HPP
|
||||
# define ROBOTOMYREQUESTFORM_HPP
|
||||
|
||||
# include "color.h"
|
||||
# include <iostream>
|
||||
# include <string>
|
||||
# include <cstdlib>
|
||||
|
||||
# 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
|
||||
|
||||
28
d05/ex03/headers/ShrubberyCreationForm.hpp
Normal file
28
d05/ex03/headers/ShrubberyCreationForm.hpp
Normal file
@@ -0,0 +1,28 @@
|
||||
#ifndef SHRUBBERYCREATIONFORM_HPP
|
||||
# define SHRUBBERYCREATIONFORM_HPP
|
||||
|
||||
# include "color.h"
|
||||
# include <iostream>
|
||||
# include <string>
|
||||
# include <fstream>
|
||||
|
||||
# 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
|
||||
|
||||
25
d05/ex03/headers/color.h
Normal file
25
d05/ex03/headers/color.h
Normal file
@@ -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
|
||||
|
||||
25
d05/ex03/headers/colors.h
Normal file
25
d05/ex03/headers/colors.h
Normal file
@@ -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
|
||||
|
||||
192
d05/ex03/main.cpp
Normal file
192
d05/ex03/main.cpp
Normal file
@@ -0,0 +1,192 @@
|
||||
#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;
|
||||
AForm * f5;
|
||||
|
||||
std::cout << B_BLUE "\nintern tries to create a Shrubbery form" RESET "\n";
|
||||
f1 = i.makeForm("Shrubbery", "smoking");
|
||||
if (f1)
|
||||
std::cout << *f1 << '\n';
|
||||
else
|
||||
std::cout << "intern makes 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 makes 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 makes 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 makes an error and cannot create the form\n";
|
||||
|
||||
std::cout << B_BLUE "\nintern tries to create a \"robotomy request\" form" RESET "\n";
|
||||
f5 = i.makeForm("robotomy request", "try_hard");
|
||||
if (f5)
|
||||
std::cout << *f5 << '\n';
|
||||
else
|
||||
std::cout << "intern makes an error and cannot create the form\n";
|
||||
|
||||
std::cout << '\n';
|
||||
delete f1;
|
||||
delete f2;
|
||||
delete f3;
|
||||
delete f4;
|
||||
delete f5;
|
||||
}
|
||||
|
||||
std::cout << "\n";
|
||||
return 0;
|
||||
}
|
||||
103
d05/ex03/srcs/AForm.cpp
Normal file
103
d05/ex03/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);
|
||||
}
|
||||
117
d05/ex03/srcs/Bureaucrat.cpp
Normal file
117
d05/ex03/srcs/Bureaucrat.cpp
Normal file
@@ -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);
|
||||
}
|
||||
84
d05/ex03/srcs/Intern.cpp
Normal file
84
d05/ex03/srcs/Intern.cpp
Normal file
@@ -0,0 +1,84 @@
|
||||
#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;
|
||||
}
|
||||
|
||||
/*********************************************
|
||||
* 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 (isValidForm(formName, i))
|
||||
{
|
||||
std::cout << "Intern creates " << formName << "\n";
|
||||
return _chooseForm[i].create(formTarget);
|
||||
}
|
||||
std::cout << "Intern cannot create " << formName << "\n";
|
||||
return NULL;
|
||||
}
|
||||
|
||||
bool Intern::isValidForm(std::string name, unsigned int i) const {
|
||||
// to lowercase :
|
||||
// https://stackoverflow.com/questions/313970/how-to-convert-an-instance-of-stdstring-to-lower-case
|
||||
std::transform(name.begin(), name.end(), name.begin(), ::tolower);
|
||||
|
||||
if (name.find(_chooseForm[i].name) == std::string::npos)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
/*********************************************
|
||||
* 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);}
|
||||
|
||||
47
d05/ex03/srcs/PresidentialPardonForm.cpp
Normal file
47
d05/ex03/srcs/PresidentialPardonForm.cpp
Normal file
@@ -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";
|
||||
}
|
||||
|
||||
51
d05/ex03/srcs/RobotomyRequestForm.cpp
Normal file
51
d05/ex03/srcs/RobotomyRequestForm.cpp
Normal file
@@ -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";
|
||||
}
|
||||
|
||||
74
d05/ex03/srcs/ShrubberyCreationForm.cpp
Normal file
74
d05/ex03/srcs/ShrubberyCreationForm.cpp
Normal file
@@ -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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user