d05 ex01 form ok

This commit is contained in:
Hugo LAMY
2022-02-28 17:56:20 +01:00
parent ce1bde6d2c
commit 4946eb3b8c
11 changed files with 452 additions and 11 deletions

73
d05/ex01/Makefile Normal file
View File

@@ -0,0 +1,73 @@
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# . name = value \ . += append to a variable #
# VARIABLES . value . != set result of command #
# . name is case sensitive . ?= set if not already set #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
NAME = larves
#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 \
Form.cpp
D_HEADERS = headers
HEADERS = Bureaucrat.hpp \
Form.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)
clean:
$(RM_OBJS)
fclean: clean
rm -f $(NAME)
re: fclean all
.PHONY : all clean fclean re

View File

@@ -0,0 +1,47 @@
#ifndef BUREAUCRAT_HPP
# define BUREAUCRAT_HPP
# include "color.h"
# include <iostream>
# include <string>
# include <stdexcept>
class Form;
# include "Form.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( Form & f );
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

46
d05/ex01/headers/Form.hpp Normal file
View File

@@ -0,0 +1,46 @@
#ifndef FORM_HPP
# define FORM_HPP
# include "color.h"
# include <iostream>
# include <string>
class Bureaucrat;
# include "Bureaucrat.hpp"
class Form {
public:
Form( std::string name, int signedGrade, int executeGrade );
Form( Form const & src );
~Form();
Form & operator=( Form const & rhs );
std::string getName() const;
bool getSigned() const;
int getSignedGrade() const;
int getExecuteGrade() const;
void beSigned( Bureaucrat const & b );
private:
Form();
class GradeTooHighException : public std::exception {
const char * what() const throw();};
class GradeTooLowException : public std::exception {
const char * what() const throw();};
std::string const _name;
bool _signed;
int const _signedGrade;
int const _executeGrade;
};
std::ostream & operator<<(std::ostream & o, Form const & rhs);
#endif

25
d05/ex01/headers/color.h Normal file
View 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

BIN
d05/ex01/larves Executable file

Binary file not shown.

66
d05/ex01/main.cpp Normal file
View File

@@ -0,0 +1,66 @@
#include "Bureaucrat.hpp"
#define N_TEST "5"
int main() {
std::cout << B_YELLOW "\n[1/" N_TEST "] test too high :" RESET "\n";
{
try {
Bureaucrat b("clarence", 151);
}
catch (std::exception& e) {
std::cout << e.what() << "\n";
}
}
std::cout << B_YELLOW "\n[2/" N_TEST "] test signe ko :" RESET "\n";
{
{
Bureaucrat b("mikel", 50);
Form f("serpa_hell", 30, 100);
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 << B_YELLOW "\n[3/" N_TEST "] test signe ok :" RESET "\n";
{
{
Bureaucrat b("coran", 50);
Form f("serpa_death", 70, 100);
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 << B_YELLOW "\n[4/" N_TEST "] test form too high :" RESET "\n";
{
try {
Form f("serpa_horror", 155, 100);
}
catch (std::exception& e) {
std::cout << e.what() << "\n";
}
}
std::cout << B_YELLOW "\n[5/" N_TEST "] test form too low :" RESET "\n";
{
try {
Form f("serpa_starvation", 70, 0);
}
catch (std::exception& e) {
std::cout << e.what() << "\n";
}
}
std::cout << "\n";
return 0;
}

View File

@@ -0,0 +1,98 @@
#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 ) {
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( Form & f) {
try {
f.beSigned( *this );
std::cout << _name << " signed " << f.getName() << "\n";
}
catch (std::exception & e) {
std::cout << _name << " couldn't sign " << f.getName()
<< " 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);
}

90
d05/ex01/srcs/Form.cpp Normal file
View File

@@ -0,0 +1,90 @@
#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);
}