CPP05 1.0
読み取り中…
検索中…
一致する文字列を見つけられません
Intern.cpp
[詳解]
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* Intern.cpp :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: kamitsui <kamitsui@student.42tokyo.jp> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2025/06/30 13:50:37 by kamitsui #+# #+# */
9/* Updated: 2025/07/18 19:21:35 by kamitsui ### ########.fr */
10/* */
11/* ************************************************************************** */
12
13#include "Intern.hpp"
14
15// --- Default Factory for production ---
17 return new ShrubberyCreationForm(target);
18}
19AForm *DefaultFormFactory::createRobotomy(const std::string &target) const { return new RobotomyRequestForm(target); }
21 return new PresidentialPardonForm(target);
22}
23
24// --- Intern ---
25// --- Orthodox Canonical Form ---
26Intern::Intern(IFormFactory *factory) : _factory(factory), _ownsFactory(false) {
27 if (!_factory) {
28 this->_factory = new DefaultFormFactory();
29 this->_ownsFactory = true;
30 }
31}
32
33Intern::Intern() : _factory(new DefaultFormFactory()), _ownsFactory(true) {
34 std::cout << "A wild Intern appears!" << std::endl;
35}
36
38 if (_ownsFactory) {
39 delete _factory;
40 }
41 std::cout << "The Intern has vanished, probably to get coffee." << std::endl;
42}
43
44// --- Intern method ---
45AForm *Intern::makeForm(const std::string &formName, const std::string &formTarget) {
46 struct FormMap {
47 std::string name;
48 AForm *(IFormFactory::*creator)(const std::string &) const;
49 };
50 const FormMap formMap[] = {{"shrubbery creation", &IFormFactory::createShrubbery},
51 {"robotomy request", &IFormFactory::createRobotomy},
52 {"presidential pardon", &IFormFactory::createPresidential}};
53 const int numForms = sizeof(formMap) / sizeof(formMap[0]);
54
55 for (int i = 0; i < numForms; ++i) {
56 if (formName == formMap[i].name) {
57 std::cout << "Intern (via factory) creates " << formName << std::endl;
58 return (this->_factory->*(formMap[i].creator))(formTarget);
59 }
60 }
62}
63
68const char *Intern::UnknownFormException::what() const throw() {
69 return "Error: Unknown form name provided. Cannot create form.";
70}
Defines the Intern class, which can create forms.
An abstract base class for forms.
Definition AForm.hpp:34
virtual AForm * createPresidential(const std::string &target) const
Definition Intern.cpp:20
virtual AForm * createShrubbery(const std::string &target) const
Definition Intern.cpp:16
virtual AForm * createRobotomy(const std::string &target) const
Definition Intern.cpp:19
virtual AForm * createPresidential(const std::string &target) const =0
virtual AForm * createShrubbery(const std::string &target) const =0
virtual AForm * createRobotomy(const std::string &target) const =0
virtual const char * what() const
Definition Intern.cpp:68
AForm * makeForm(const std::string &formName, const std::string &formTarget)
Definition Intern.cpp:45
~Intern()
Definition Intern.cpp:37
Intern()
Definition Intern.cpp:33
A concrete form that grants a presidential pardon.
A concrete form that simulates a robotomy attempt.
A concrete form that creates a file with ASCII trees.
T endl(T... args)