Skip to content

Commit

Permalink
Memory management improvements
Browse files Browse the repository at this point in the history
- Remove all owning raw pointers
- Replace all calls to new with make_unique
- Use CRTP for generating Clone method
  • Loading branch information
Guekka committed Mar 8, 2021
1 parent 065495c commit 3e2e36b
Show file tree
Hide file tree
Showing 14 changed files with 569 additions and 874 deletions.
387 changes: 133 additions & 254 deletions include/Animation.hpp

Large diffs are not rendered by default.

22 changes: 10 additions & 12 deletions include/BasicTypes.hpp
Expand Up @@ -231,7 +231,7 @@ class StringRef {
}
};

class Ref {
class Ref : public CloneInherit<AbstractMethod<Ref>> {
protected:
int index = NIF_NPOS;

Expand All @@ -244,7 +244,7 @@ class Ref {
};

template<typename T>
class BlockRef : public Ref {
class BlockRef : public CloneInherit<BlockRef<T>, Ref> {
public:
BlockRef() {}
BlockRef(const int id) { index = id; }
Expand All @@ -254,7 +254,7 @@ class BlockRef : public Ref {
void Put(NiStream& stream) { stream << index; }
};

class RefArray {
class RefArray : public CloneInherit<AbstractMethod<RefArray>> {
protected:
int arraySize = 0;
bool keepEmptyRefs = false;
Expand All @@ -279,7 +279,7 @@ class RefArray {
};

template<typename T>
class BlockRefArray : public RefArray {
class BlockRefArray : public CloneInherit<BlockRefArray<T>, RefArray> {
protected:
std::vector<BlockRef<T>> refs;

Expand Down Expand Up @@ -389,7 +389,7 @@ class BlockRefArray : public RefArray {
};

template<typename T>
class BlockRefShortArray : public BlockRefArray<T> {
class BlockRefShortArray : public CloneInherit<BlockRefShortArray<T>, BlockRefArray<T>> {
public:
typedef BlockRefArray<T> base;
using base::arraySize;
Expand Down Expand Up @@ -423,7 +423,7 @@ class BlockRefShortArray : public BlockRefArray<T> {
}
};

class NiObject {
class NiObject : public CloneInherit<AbstractMethod<NiObject>> {
protected:
uint32_t blockSize = 0;

Expand All @@ -443,15 +443,14 @@ class NiObject {
virtual void GetChildIndices(std::vector<int>&) {}
virtual void GetPtrs(std::set<Ref*>&) {}

virtual NiObject* Clone() { return new NiObject(*this); }

template<typename T>
bool HasType() {
return dynamic_cast<const T*>(this) != nullptr;
}
};

class NiHeader : public NiObject {
class NiHeader : public CloneInherit<NiHeader, NiObject> {
/*
Minimum supported
Version: 20.2.0.7
Expand Down Expand Up @@ -542,8 +541,8 @@ class NiHeader : public NiObject {

void DeleteBlock(int blockId);
void DeleteBlockByType(const std::string& blockTypeStr, const bool orphanedOnly = false);
int AddBlock(NiObject* newBlock);
int ReplaceBlock(int oldBlockId, NiObject* newBlock);
int AddBlock(std::unique_ptr<NiObject> newBlock);
int ReplaceBlock(int oldBlockId, std::unique_ptr<NiObject> newBlock);
void SetBlockOrder(std::vector<int>& newOrder);
void FixBlockAlignment(const std::vector<NiObject*>& currentTree);

Expand Down Expand Up @@ -602,7 +601,7 @@ class NiHeader : public NiObject {
void Put(NiStream& stream) override;
};

class NiUnknown : public NiObject {
class NiUnknown : public CloneInherit<NiUnknown, NiObject> {
private:
std::vector<char> data;

Expand All @@ -613,7 +612,6 @@ class NiUnknown : public NiObject {

void Get(NiStream& stream) override;
void Put(NiStream& stream) override;
NiUnknown* Clone() override { return new NiUnknown(*this); }

std::vector<char> GetData() { return data; }
};
Expand Down

0 comments on commit 3e2e36b

Please sign in to comment.