Files
42_SIDE_exam_05_cpp/cpp_module_02/SpellBook.hpp
2022-12-12 22:34:11 +01:00

62 lines
1.1 KiB
C++

#ifndef SPELLBOOK_HPP
#define SPELLBOOK_HPP
#include <iostream>
#include <string>
#include <map>
#include "ASpell.hpp"
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;
for (it = arr.begin(); it != arr.end(); ++it)
delete it->second;
this->arr.clear();
}
void learnSpell(ASpell * aspell)
{
if (aspell)
{
arr.insert(std::pair<std::string, ASpell*>
(
aspell->getName(),
aspell->clone()
));
}
}
void forgetSpell(std::string const & spell_name)
{
std::map<std::string, ASpell*>::iterator it;
it = arr.find(spell_name);
if (it != arr.end())
{
delete it->second;
arr.erase(spell_name);
}
}
ASpell * createSpell(std::string const & spell_name)
{
std::map<std::string, ASpell*>::iterator it;
it = arr.find(spell_name);
if (it != arr.end())
return arr[spell_name];
return NULL;
}
};
#endif