CPP04 1.0
読み取り中…
検索中…
一致する文字列を見つけられません
MateriaSource.cpp
[詳解]
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* MateriaSource.cpp :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: kamitsui <kamitsui@student.42tokyo.jp> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2025/05/29 23:40:28 by kamitsui #+# #+# */
9/* Updated: 2025/06/02 05:06:26 by kamitsui ### ########.fr */
10/* */
11/* ************************************************************************** */
12
24#include "MateriaSource.hpp" // Includes the declaration of the MateriaSource class
25#include "Logger.hpp"
26#include <sstream>
27
33 LOG_INFO("MateriaSource constructor called");
34 for (int i = 0; i < 4; ++i)
35 this->learnedMateria[i] = NULL;
36}
37
44MateriaSource::MateriaSource(const MateriaSource &other) : learnCount(other.learnCount) {
45 LOG_INFO("MateriaSource copy constructor called");
46 for (int i = 0; i < 4; ++i) {
47 if (other.learnedMateria[i])
48 this->learnedMateria[i] = other.learnedMateria[i]->clone();
49 else
50 this->learnedMateria[i] = NULL;
51 }
52}
53
63 LOG_INFO("MateriaSource copy assignment operator called");
64 if (this != &other) {
65 this->learnCount = other.learnCount;
66 for (int i = 0; i < 4; ++i) {
67 if (this->learnedMateria[i])
68 delete this->learnedMateria[i];
69 if (other.learnedMateria[i])
70 this->learnedMateria[i] = other.learnedMateria[i]->clone();
71 else
72 this->learnedMateria[i] = NULL;
73 }
74 }
75 return *this;
76}
77
84 LOG_INFO("MateriaSource destructor called");
85 for (int i = 0; i < 4; ++i) {
86 if (this->learnedMateria[i])
87 delete this->learnedMateria[i];
88 }
89}
90
99 if (this->learnCount < 4 && materia) {
100 this->learnedMateria[this->learnCount++] = materia->clone();
101 oss << "MateriaSource learned " << materia->getType();
102 LOG_INFO(oss.str());
103 delete materia;
104 } else if (materia) {
105 oss << "MateriaSource cannot learn more materia.";
106 LOG_INFO(oss.str());
107 delete materia; // Delete the passed materia if not learned to avoid leaks
108 }
109}
110
120 for (int i = 0; i < this->learnCount; ++i) {
121 if (this->learnedMateria[i] && this->learnedMateria[i]->getType() == type) {
122 oss << "MateriaSource creates " << type << std::endl;
123 LOG_INFO(oss.str());
124 return this->learnedMateria[i]->clone();
125 }
126 }
127 oss << "MateriaSource does not know materia type " << type;
128 LOG_INFO(oss.str());
129 return 0;
130}
Defines the Logger class for console-based logging.
#define LOG_INFO(msg)
Macro for logging informational messages.
Definition Logger.hpp:70
Declares the MateriaSource class.
Represents an abstract base class for all magical materias.
Definition AMateria.hpp:42
virtual AMateria * clone() const =0
std::string const & getType() const
Gets the type of the materia.
Definition AMateria.cpp:78
Represents a source for learning and creating Materias.
MateriaSource()
Default constructor for MateriaSource. Initializes the learning count to zero and sets all learned Ma...
virtual ~MateriaSource()
Destroys the MateriaSource object. Deallocates all dynamically allocated AMateria objects that were l...
MateriaSource & operator=(const MateriaSource &other)
Copy assignment operator for MateriaSource. Assigns values from another MateriaSource object,...
virtual void learnMateria(AMateria *materia)
Learns a Materia template for later creation. The MateriaSource makes a deep copy of the passed Mater...
virtual AMateria * createMateria(std::string const &type) const
Creates a new Materia based on a learned type. Returns a new instance (a clone) of a previously learn...
T endl(T... args)
T str(T... args)