CPP01 1.0
読み取り中…
検索中…
一致する文字列を見つけられません
Harl.cpp
[詳解]
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* Harl.cpp :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: kamitsui <kamitsui@student.42tokyo.jp> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2025/04/19 22:03:38 by kamitsui #+# #+# */
9/* Updated: 2025/04/26 15:22:14 by kamitsui ### ########.fr */
10/* */
11/* ************************************************************************** */
12
18#include "Harl.hpp"
19
20// NG : No container and No Algorithms (cpp00~07)
21// Explicitly initialize the static map in the .cpp file
22// const std::map<std::string, Harl::ComplaintFunc> Harl::complaintsMap = Harl::initializeComplaintMap();
23
28
34Harl::Harl(const Harl &other) {
35 (void)other; // To avoid unused parameter warning
36}
37
44Harl &Harl::operator=(const Harl &other) {
45 if (this != &other) {
46 // No members to copy in this simple class
47 }
48 return *this;
49}
50
55
59void Harl::debug(void) {
60 std::cout << "[ DEBUG ]" << std::endl;
61 std::cout << "I love having extra bacon for my 7XL-double-cheese-triple-pickle-special-ketchup burger. I really do!"
62 << std::endl;
63}
64
68void Harl::info(void) {
69 std::cout << "[ INFO ]" << std::endl;
70 std::cout << "I cannot believe adding extra bacon costs more money. You didn't put enough bacon in my burger! If "
71 "you did, I wouldn't be asking for more!"
72 << std::endl;
73}
74
78void Harl::warning(void) {
79 std::cout << "[ WARNING ]" << std::endl;
80 std::cout << "I think I deserve to have some extra bacon for free. I've been coming for years, whereas you started "
81 "working here just last month."
82 << std::endl;
83}
84
88void Harl::error(void) {
89 std::cout << "[ ERROR ]" << std::endl;
90 std::cout << "This is unacceptable! I want to speak to the manager now." << std::endl;
91}
92
102 static const std::string levels[] = {"DEBUG", "INFO", "WARNING", "ERROR"};
103 static ComplaintFunc functions[] = {&Harl::debug, &Harl::info, &Harl::warning, &Harl::error};
104
105 for (int i = 0; i < 4; ++i) {
106 if (levels[i] == level) {
107 (this->*(functions[i]))();
108 return;
109 }
110 }
111}
112
113// NG : No container and No Algorithms (cpp00~07)
114// void Harl::complain(std::string level) {
115// std::map<std::string, ComplaintFunc>::const_iterator it = complaintsMap.find(level);
116// if (it != complaintsMap.end()) {
117// (this->*(it->second))();
118// }
119// }
120//
121// std::map<std::string, Harl::ComplaintFunc> Harl::initializeComplaintMap() {
122// std::map<std::string, Harl::ComplaintFunc> map;
123// map["DEBUG"] = &Harl::debug;
124// map["INFO"] = &Harl::info;
125// map["WARNING"] = &Harl::warning;
126// map["ERROR"] = &Harl::error;
127// return map;
128// }
129
130// NG : C++98 does not support, that is C++11.
131// Initialize the static member outside the class definition
132// const std::map<std::string, Harl::ComplaintFunc> Harl::complaintsMap = {
133// {"DEBUG", &Harl::debug}, {"INFO", &Harl::info}, {"WARNING", &Harl::warning}, {"ERROR", &Harl::error}};
Declares the Harl class, which can complain at different levels.
basic_ostream< _CharT, _Traits > & endl(basic_ostream< _CharT, _Traits > &__os)
ostream cout
Represents a Harl character who can complain at different levels.
Definition Harl.hpp:28
~Harl()
Destructor for the Harl class.
Definition Harl.cpp:54
Harl & operator=(const Harl &other)
Copy assignment operator for the Harl class.
Definition Harl.cpp:44
Harl()
Default constructor for the Harl class.
Definition Harl.cpp:27
void complain(std::string level)
Makes Harl complain based on the specified level.
Definition Harl.cpp:101