diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-11-02 15:55:58 +0100 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-11-02 15:55:58 +0100 |
commit | 5e4dd0c0197f6273c61491a5b9a030c93f796a12 (patch) | |
tree | 390fc6d980cc49927523befb43d189c608452276 /backend | |
parent | 369e3d71aa79783d95166739cfa93a480defe6ea (diff) |
add some tests
Diffstat (limited to 'backend')
-rw-r--r-- | backend/Player.cpp | 4 | ||||
-rw-r--r-- | backend/PtrList.hpp | 1 | ||||
-rw-r--r-- | backend/String.cpp | 8 | ||||
-rw-r--r-- | backend/print.cpp | 2 | ||||
-rw-r--r-- | backend/util.cpp | 38 | ||||
-rw-r--r-- | backend/util.h | 2 |
6 files changed, 31 insertions, 24 deletions
diff --git a/backend/Player.cpp b/backend/Player.cpp index 5e3e030..8a44a3a 100644 --- a/backend/Player.cpp +++ b/backend/Player.cpp @@ -7,8 +7,8 @@ Player::Player(Dungeon & dungeon) : dungeon(dungeon) { } Player::~Player() { - safe_free(this->weapon); - safe_free(this->armor); + safe_free(&this->weapon); + safe_free(&this->armor); } float Player::get_attack() const { diff --git a/backend/PtrList.hpp b/backend/PtrList.hpp index cbfd6e0..6969533 100644 --- a/backend/PtrList.hpp +++ b/backend/PtrList.hpp @@ -1,6 +1,7 @@ #pragma once #include "PtrList.h" +#include "ListIterator.h" template <typename T> PtrList<T>::~PtrList() { diff --git a/backend/String.cpp b/backend/String.cpp index e5512bf..84dc2ec 100644 --- a/backend/String.cpp +++ b/backend/String.cpp @@ -26,7 +26,7 @@ String::String(String && other) { } String & String::operator = (String && other) { if (this == &other) return *this; - safe_free(this->_data); + safe_free(&this->_data); this->_data = other._data; this->_data_len = other._data_len; other._data = nullptr; @@ -58,14 +58,14 @@ String String::va_fmt(va_list args, const char * fmt) { out._data_len = vsnprintf(NULL, 0, fmt, args_copy); va_end(args_copy); - safe_free(out._data); + safe_free(&out._data); out._data = static_cast<char *>(malloc(out._data_len + 1)); vsnprintf(out._data, out._data_len + 1, fmt, args); return out; } String::~String() { - safe_free(this->_data); + safe_free(&this->_data); } void String::set(const char * data) { @@ -73,7 +73,7 @@ void String::set(const char * data) { } void String::set(const char * data, size_t data_len) { - safe_free(this->_data); + safe_free(&this->_data); this->_data = static_cast<char *>(malloc(data_len + 1)); this->_data_len = data_len; memcpy(this->_data, data, data_len); diff --git a/backend/print.cpp b/backend/print.cpp index 1cb6a75..edf7989 100644 --- a/backend/print.cpp +++ b/backend/print.cpp @@ -31,7 +31,7 @@ SessionLog::SessionLog() { } SessionLog::~SessionLog() { - safe_free(this->file); + safe_free(&this->file); } void SessionLog::append(const String & str) const { diff --git a/backend/util.cpp b/backend/util.cpp index 557df4c..edf9539 100644 --- a/backend/util.cpp +++ b/backend/util.cpp @@ -7,35 +7,41 @@ #include "ArmorObject.h" template <> -void safe_free(void * & ptr) { - if (ptr == nullptr) return; - free(ptr); - ptr = nullptr; +void safe_free(void ** ptr) { + if (*ptr == nullptr) return; + free(*ptr); + *ptr = nullptr; } template <> -void safe_free(const char * & ptr) { - auto x = static_cast<void *>(const_cast<char *>(ptr)); - safe_free(x); +void safe_free(const char ** ptr) { + // wow c++ casts so readable + auto a = const_cast<char **>(ptr); + auto b = static_cast<void *>(a); + auto c = reinterpret_cast<void**>(b); + safe_free(c); } template <> -void safe_free(WeaponObject * & ptr) { - delete ptr; +void safe_free(WeaponObject ** ptr) { + delete *ptr; + *ptr = nullptr; } template <> -void safe_free(ArmorObject * & ptr) { - delete ptr; +void safe_free(ArmorObject ** ptr) { + delete *ptr; + *ptr = nullptr; } template <typename T> -void safe_free(T * & ptr) { - auto x = static_cast<void *>(ptr); - safe_free(x); +void safe_free(T ** ptr) { + auto a = static_cast<void *>(ptr); + auto b = reinterpret_cast<void **>(ptr); + safe_free(b); } -template void safe_free(char * &); -template void safe_free(FILE * &); +template void safe_free(char **); +template void safe_free(FILE **); diff --git a/backend/util.h b/backend/util.h index d278eb1..f1e7bab 100644 --- a/backend/util.h +++ b/backend/util.h @@ -1,7 +1,7 @@ #pragma once template <typename T> -void safe_free(T * & ptr); +void safe_free(T ** ptr); template <typename T> inline T min(const T &, const T &); |