CPP04 1.0
読み取り中…
検索中…
一致する文字列を見つけられません
DroppedMateriaManager.cpp
[詳解]
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* DroppedMateriaManager.cpp :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: kamitsui <kamitsui@student.42tokyo.jp> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2025/06/02 00:30:53 by kamitsui #+# #+# */
9/* Updated: 2025/06/06 12:35:01 by kamitsui ### ########.fr */
10/* */
11/* ************************************************************************** */
12
23#include "Logger.hpp"
24#include <sstream>
25
31 LOG_INFO("DroppedMateriaManager: Default constructor called.");
32 for (int i = 0; i < MAX_DROPPED_MATERIAS; ++i) {
33 droppedMaterias[i] = NULL;
34 }
35}
36
44 LOG_INFO("DroppedMateriaManager: Copy constructor called (creates empty list).");
45 for (int i = 0; i < MAX_DROPPED_MATERIAS; ++i) {
46 droppedMaterias[i] = NULL; // Initialize with empty slots
47 }
48 (void)other; // Avoid unused parameter warning
49}
50
59 LOG_INFO("DroppedMateriaManager: Copy assignment operator called (resets to empty list).");
60 if (this != &other) {
61 // Deallocate all currently managed materias to prevent leaks
62 for (int i = 0; i < MAX_DROPPED_MATERIAS; ++i) {
63 if (droppedMaterias[i] != NULL) {
64 delete droppedMaterias[i];
65 droppedMaterias[i] = NULL;
66 }
67 }
68 count = 0;
69 }
70 (void)other; // Avoid unused parameter warning
71 return *this;
72}
73
79 LOG_INFO("DroppedMateriaManager: Destructor called. Clearing all dropped materias.");
80 clearAll(); // Ensure all managed materias are deleted
81}
82
90 if (materia == NULL) {
91 LOG_INFO("DroppedMateriaManager: Cannot add NULL materia.");
92 return;
93 }
95 if (count < MAX_DROPPED_MATERIAS) {
96 for (int i = 0; i < MAX_DROPPED_MATERIAS; ++i) {
97 if (droppedMaterias[i] == NULL) {
98 droppedMaterias[i] = materia;
99 count++;
100 oss << "DroppedMateriaManager: Added " << materia->getType() << " at slot " << i;
101 LOG_INFO(oss.str());
102 return;
103 }
104 }
105 }
106 // If the list is full
107 oss << "DroppedMateriaManager: Dropped materia list is full, cannot add " << materia->getType() << ". Deleting it.";
108 LOG_INFO(oss.str());
109 delete materia; // Delete the passed materia as it cannot be stored
110}
111
120 if (index >= 0 && index < MAX_DROPPED_MATERIAS && droppedMaterias[index] != NULL) {
121 return droppedMaterias[index];
122 }
123 return NULL;
124}
125
133 if (index >= 0 && index < MAX_DROPPED_MATERIAS && droppedMaterias[index] != NULL) {
134 std::cout << "DroppedMateriaManager: Removing " << droppedMaterias[index]->getType() << " from slot " << index
135 << " (ownership transferred)." << std::endl;
136 droppedMaterias[index] = NULL;
137 count--;
138 }
139}
140
145int DroppedMateriaManager::getCount() const { return count; }
146
152 for (int i = 0; i < MAX_DROPPED_MATERIAS; ++i) {
153 if (droppedMaterias[i] != NULL) {
154 std::cout << "DroppedMateriaManager: Deleting remaining dropped materia " << droppedMaterias[i]->getType()
155 << " at slot " << i << std::endl;
156 delete droppedMaterias[i];
157 droppedMaterias[i] = NULL;
158 }
159 }
160 count = 0;
161}
162
167 if (count == 0) {
168 LOG_INFO("DroppedMateriaManager: No materias currently dropped.");
169 return;
170 }
172 oss << "DroppedMateriaManager: Currently dropped materias (" << count << "):" << std::endl;
173 for (int i = 0; i < MAX_DROPPED_MATERIAS; ++i) {
174 if (droppedMaterias[i] != NULL) {
175 oss << ">> debug >> " << count << "[" << droppedMaterias[i] << "]" << std::endl;
176 oss << " - Slot " << i << ": " << droppedMaterias[i]->getType() << std::endl;
177 }
178 }
179 LOG_INFO(oss.str());
180}
Declares the DroppedMateriaManager class.
#define MAX_DROPPED_MATERIAS
Defines the maximum number of Materias that can be managed at once.
Defines the Logger class for console-based logging.
#define LOG_INFO(msg)
Macro for logging informational messages.
Definition Logger.hpp:70
Represents an abstract base class for all magical materias.
Definition AMateria.hpp:42
std::string const & getType() const
Gets the type of the materia.
Definition AMateria.cpp:78
Manages AMateria objects that are temporarily "dropped" in the game.
~DroppedMateriaManager()
Destructor for DroppedMateriaManager. Automatically clears all remaining dropped materias by dealloca...
void clearAll()
Clears all currently managed dropped materias by deallocating their memory. This method is typically ...
void removeMateria(int index)
Removes a materia from a specific index in the dropped list without deleting it. This implies ownersh...
void displayDroppedMaterias() const
Displays the types and slots of all currently dropped materias for debugging.
DroppedMateriaManager()
Default constructor for DroppedMateriaManager. Initializes the manager with an empty list of dropped ...
DroppedMateriaManager & operator=(const DroppedMateriaManager &other)
Copy assignment operator for DroppedMateriaManager. Resets the current manager's list to empty,...
void addMateria(AMateria *materia)
Adds a materia to the list of dropped materias. Takes ownership of the passed AMateria pointer....
int getCount() const
Gets the current count of dropped materias being managed.
AMateria * getMateria(int index)
Gets a pointer to a materia at a specific index in the dropped list. This method transfers ownership ...
T endl(T... args)
T str(T... args)