Use unique pointers in scriptset.
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -2,21 +2,17 @@
|
||||
#define CREATURELIB_SCRIPTSET_HPP
|
||||
|
||||
#include <Arbutils/Collections/Dictionary.hpp>
|
||||
#include <Arbutils/Collections/List.hpp>
|
||||
#include <Arbutils/Memory/UniquePtrList.hpp>
|
||||
#include <any>
|
||||
#include "Script.hpp"
|
||||
|
||||
namespace CreatureLib::Battling {
|
||||
class ScriptSet {
|
||||
ArbUt::List<Script*> _scripts;
|
||||
ArbUt::UniquePtrList<Script> _scripts;
|
||||
ArbUt::Dictionary<uint32_t, size_t> _lookup;
|
||||
|
||||
public:
|
||||
~ScriptSet() {
|
||||
for (auto s : _scripts) {
|
||||
delete s;
|
||||
}
|
||||
}
|
||||
~ScriptSet() = default;
|
||||
|
||||
static constexpr size_t defaultCapacity = 8;
|
||||
ScriptSet() : _scripts(defaultCapacity), _lookup(defaultCapacity){};
|
||||
@@ -33,9 +29,11 @@ namespace CreatureLib::Battling {
|
||||
_lookup.Insert(script->GetName(), _scripts.Count() - 1);
|
||||
}
|
||||
|
||||
Script* Get(const ArbUt::CaseInsensitiveConstString& key) const { return Get(key.GetHash()); }
|
||||
ArbUt::BorrowedPtr<Script> Get(const ArbUt::CaseInsensitiveConstString& key) const {
|
||||
return Get(key.GetHash());
|
||||
}
|
||||
|
||||
Script* Get(uint32_t keyHash) const noexcept {
|
||||
ArbUt::BorrowedPtr<Script> Get(uint32_t keyHash) const noexcept {
|
||||
size_t v;
|
||||
if (_lookup.TryGet(keyHash, v)) {
|
||||
return _scripts[v];
|
||||
@@ -50,7 +48,6 @@ namespace CreatureLib::Battling {
|
||||
if (_lookup.TryGet(keyHash, v)) {
|
||||
auto script = _scripts[v];
|
||||
script->OnRemove();
|
||||
delete script;
|
||||
_scripts.Remove(v);
|
||||
_lookup.Remove(keyHash);
|
||||
}
|
||||
@@ -71,7 +68,7 @@ namespace CreatureLib::Battling {
|
||||
|
||||
inline size_t Count() const { return _scripts.Count(); }
|
||||
|
||||
const ArbUt::List<Script*>* GetIterator() const { return &_scripts; }
|
||||
const ArbUt::UniquePtrList<Script>& GetIterator() const { return _scripts; }
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user