-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathForm.cpp
More file actions
121 lines (102 loc) · 3.71 KB
/
Copy pathForm.cpp
File metadata and controls
121 lines (102 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Form.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dpoveda- <me@izenynn.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/17 23:13:03 by dpoveda- #+# #+# */
/* Updated: 2022/02/18 02:56:21 by dpoveda- ### ########.fr */
/* */
/* ************************************************************************** */
#include "Form.hpp"
namespace {
void checkGrade(int grade) {
if (grade < Bureaucrat::kMaxGrade) throw Bureaucrat::GradeTooHighException();
if (grade > Bureaucrat::kMinGrade) throw Bureaucrat::GradeTooLowException();
}
}
Form::Form()
: _signed(false),
_name("form"),
_target("none"),
_signGrade(150),
_executeGrade(150) {}
Form::Form(const std::string& name, const std::string& target, int signGrade, int executeGrade)
: _signed(false),
_name(name),
_target(target),
_signGrade(signGrade),
_executeGrade(executeGrade) {
checkGrade(signGrade);
checkGrade(executeGrade);
std::cout << "Form created" << std::endl;
}
Form::Form(const Form& other)
: _signed(other._signed),
_name(other._name),
_target(other._target),
_signGrade(other._signGrade),
_executeGrade(other._executeGrade) {
std::cout << "Copy of Form created" << std::endl;
}
Form::~Form() {
std::cout << "Form destroyed" << std::endl;
}
Form& Form::operator=(const Form& other) {
std::cout << "Form assignment operator" << std::endl;
if (this == &other) return *this;
return *this;
}
const std::string& Form::getName() const {
return this->_name;
}
const std::string& Form::getTarget() const {
return this->_target;
}
bool Form::getSigned() const {
return this->_signed;
}
int Form::getSignGrade() const {
return this->_signGrade;
}
int Form::getExecuteGrade() const {
return this->_executeGrade;
}
void Form::beSigned(const Bureaucrat& bureaucrat) {
if (bureaucrat.getGrade() <= this->_signGrade) {
this->_signed = true;
std::cout << bureaucrat.getName() << " has signed " << this->_name << std::endl;
} else {
std::cout << bureaucrat.getName() << " can't sign " << this->_name << std::endl;
throw Form::GradeTooLowException();
}
}
void Form::beExecuted(const Bureaucrat& bureaucrat) const {
if (this->_signed == false) {
std::cout << bureaucrat.getName() << " can't execute " << this->_name << std::endl;
throw Form::NotSigned();
}
if (bureaucrat.getGrade() > this->_executeGrade) {
std::cout << bureaucrat.getName() << " can't execute " << this->_name << std::endl;
throw Form::GradeTooLowException();
}
std::cout << bureaucrat.getName() << " has executed " << this->_name << std::endl;
this->executeAction();
}
const char* Form::GradeTooHighException::what() const throw() {
return "Exception: grade too high";
}
const char* Form::GradeTooLowException::what() const throw() {
return "Exception: grade too low";
}
const char* Form::NotSigned::what() const throw() {
return "Exception: form is not signed so it can't be executed";
}
std::ostream& operator<<(std::ostream& os, const Form& instance) {
os << "Form " << instance.getName()
<< ", status: " << (instance.getSigned() ? "signed" : "not signed")
<< ", sign grade: " << instance.getSignGrade()
<< ", execute grade: " << instance.getExecuteGrade();
return os;
}