aboutsummaryrefslogtreecommitdiff
path: root/cpp05/ex00/Bureaucrat.cpp
blob: 33850893c90fce7d859266864c19aef883f3b6e9 (plain)
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
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   Bureaucrat.cpp                                     :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: charles <charles.cabergs@gmail.com>        +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/04/14 18:08:13 by charles           #+#    #+#             */
/*   Updated: 2020/12/12 11:35:48 by cacharle         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "Bureaucrat.hpp"

Bureaucrat::Bureaucrat(Bureaucrat const& other)
    : m_name(other.m_name) { *this = other; }

Bureaucrat& Bureaucrat::operator=(Bureaucrat const& other)
{
    // cannot copy other.m_name since it's constant
    m_grade = other.m_grade;
    return *this;
}

Bureaucrat::~Bureaucrat() {}

Bureaucrat::Bureaucrat(std::string const& name, int grade)
    : m_name(name), m_grade(grade)
{
    checkGrade();
}

std::string const& Bureaucrat::getName()  const { return m_name;  }
int                Bureaucrat::getGrade() const { return m_grade; }

void Bureaucrat::incrementGrade()
{
    m_grade--;
    checkGrade();
}

void Bureaucrat::decrementGrade()
{
    m_grade++;
    checkGrade();
}

void Bureaucrat::checkGrade()
{
    if (m_grade > 150)
        throw Bureaucrat::GradeTooLowException();
    if (m_grade < 1)
        throw Bureaucrat::GradeTooHighException();
}

std::ostream& operator<<(std::ostream& out, Bureaucrat const& b)
{
    std::cout << b.getName() << ", bureaucrat grade " << b.getGrade() << std::endl;
    return out;
}

///////////////////////////////////////////////////////////////////////////////
// Exception grade too high
///////////////////////////////////////////////////////////////////////////////

Bureaucrat::GradeTooHighException::GradeTooHighException() : std::exception() {}

Bureaucrat::GradeTooHighException::GradeTooHighException(GradeTooHighException const& other)
    : std::exception(other) {}

Bureaucrat::GradeTooHighException&
Bureaucrat::GradeTooHighException::operator=(GradeTooHighException const& other)
{
    std::exception::operator=(other);
    return *this;
}

Bureaucrat::GradeTooHighException::~GradeTooHighException() throw() {}

char const* Bureaucrat::GradeTooHighException::what() const throw() { return "Grade is too high"; }

///////////////////////////////////////////////////////////////////////////////
// Exception grade too low
///////////////////////////////////////////////////////////////////////////////

Bureaucrat::GradeTooLowException::GradeTooLowException() : std::exception() {}

Bureaucrat::GradeTooLowException::GradeTooLowException(GradeTooLowException const& other)
    : std::exception(other) {}

Bureaucrat::GradeTooLowException&
Bureaucrat::GradeTooLowException::operator=(GradeTooLowException const& other)
{
    std::exception::operator=(other);
    return *this;
}

Bureaucrat::GradeTooLowException::~GradeTooLowException() throw() {}

char const* Bureaucrat::GradeTooLowException::what() const throw() { return "Grade is too low"; }

Bureaucrat::Bureaucrat() {}