Make all individual scripts smart pointers.
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
2020-06-02 13:43:44 +02:00
parent dba1275813
commit e4b9550efa
11 changed files with 48 additions and 58 deletions

View File

@@ -21,7 +21,7 @@ Battling::Creature::Creature(const BattleLibrary* library,
AssertNotNull(species)
AssertNotNull(variant)
_activeTalent = _library->LoadScript(ScriptCategory::Talent, GetActiveTalent());
_activeTalent = std::unique_ptr<Script>(_library->LoadScript(ScriptCategory::Talent, GetActiveTalent()));
if (_nickname.empty()) {
_nickname = species->GetName().std_str();
}
@@ -142,9 +142,8 @@ void Battling::Creature::Heal(uint32_t amount, bool canRevive) {
void Battling::Creature::OverrideActiveTalent(const ConstString& talent) {
_hasOverridenTalent = true;
_activeTalent->OnRemove();
delete _activeTalent;
_overridenTalentName = talent;
_activeTalent = this->_library->LoadScript(ScriptCategory::Talent, talent);
_activeTalent.reset(this->_library->LoadScript(ScriptCategory::Talent, talent));
}
const ArbUt::List<uint8_t>& Battling::Creature::GetTypes() const noexcept {

View File

@@ -47,7 +47,7 @@ namespace CreatureLib::Battling {
std::string _nickname = "";
CreatureLib::Library::TalentIndex _talentIndex;
Script* _activeTalent = nullptr;
std::unique_ptr<Script> _activeTalent = nullptr;
bool _hasOverridenTalent;
ArbUt::CaseInsensitiveConstString _overridenTalentName = ""_cnc;
@@ -56,7 +56,7 @@ namespace CreatureLib::Battling {
ArbUt::UniquePtrList<LearnedAttack> _attacks;
bool _allowedExperienceGain;
Script* _status = nullptr;
std::unique_ptr<Script> _status = nullptr;
ScriptSet _volatile = {};
private:
@@ -70,10 +70,7 @@ namespace CreatureLib::Battling {
const Library::TalentIndex& talent, const std::vector<LearnedAttack*>& attacks,
bool allowedExperienceGain = true);
virtual ~Creature() {
delete _activeTalent;
delete _status;
};
virtual ~Creature() = default;
virtual void Initialize() {
RecalculateFlatStats();

View File

@@ -41,24 +41,25 @@ namespace CreatureLib::Battling {
HitData* _hits;
Creature* _user;
ArbUt::BorrowedPtr<LearnedAttack> _attack;
Script* _script;
std::unique_ptr<Script> _script = nullptr;
public:
ExecutingAttack(const ArbUt::List<ArbUt::BorrowedPtr<Creature>>& targets, uint8_t numberHits, Creature* user,
const ArbUt::BorrowedPtr<LearnedAttack>& attack, Script* script)
const ArbUt::BorrowedPtr<LearnedAttack>& attack, const std::unique_ptr<Script>& script)
: _targets(targets.Count()), _numberHits(numberHits), _hits(new HitData[targets.Count() * numberHits]),
_user(user), _attack(attack), _script(script) {
_user(user), _attack(attack) {
AssertNotNull(user)
AssertNotNull(attack)
for (auto target : targets) {
_targets.Append(target);
}
// Take ownership of the script of the attack choice, and give attack choice our initial nullptr.
_script.swap(const_cast<std::unique_ptr<Script>&>(script));
}
ExecutingAttack(const ExecutingAttack&) = delete;
ExecutingAttack& operator=(const ExecutingAttack&) = delete;
virtual ~ExecutingAttack() noexcept {
delete _script;
delete[] _hits;
};