Files
42_SIDE_exam_05_cpp/cpp_module_02/SpellBook.hpp
2022-12-01 15:47:19 +01:00

51 lines
1.1 KiB
C++

#ifndef SPELLBOOK_HPP
#define SPELLBOOK_HPP
# include <iostream>
# include <string>
# include "ASpell.hpp"
# include <map>
class SpellBook {
private:
SpellBook(SpellBook const & other);
SpellBook & operator=(SpellBook const & other);
std::map<std::string, ASpell *> arr;
public:
SpellBook() {};
~SpellBook() {
std::map<std::string, ASpell *>::iterator it_begin = this->arr.begin();
std::map<std::string, ASpell *>::iterator it_end = this->arr.end();
while (it_begin != it_end) {
delete it_begin->second;
++it_begin;
}
this->arr.clear();
};
void learnSpell(ASpell *aspell) {
if (aspell)
arr.insert(std::pair<std::string, ASpell *>(
aspell->getName(),
aspell->clone()
));
};
void forgetSpell(std::string & name) {
std::map<std::string, ASpell *>::iterator it = arr.find(name);
if (it == arr.end())
return;
delete it->second;
arr.erase(name);
};
ASpell * createSpell(std::string & name) {
std::map<std::string, ASpell *>::iterator it = arr.find(name);
if (it == arr.end())
return NULL;
return arr[name];
};
};
#endif