From d67d6a483136887ac9c59e7a303703d9c3a15091 Mon Sep 17 00:00:00 2001 From: IntelOrca Date: Mon, 4 Jan 2016 17:24:12 +0000 Subject: [PATCH] add more core classes --- openrct2.vcxproj | 3 ++ openrct2.vcxproj.filters | 3 ++ src/audio/mixer.cpp | 1 + src/core/Diagnostics.hpp | 22 ++++++++++++ src/core/Guard.hpp | 22 ++++++++++++ src/core/List.hpp | 76 ++++++++++++++++++++++++++++++++++++++++ src/core/Memory.hpp | 44 +++++++++++++++-------- 7 files changed, 157 insertions(+), 14 deletions(-) create mode 100644 src/core/Diagnostics.hpp create mode 100644 src/core/Guard.hpp create mode 100644 src/core/List.hpp diff --git a/openrct2.vcxproj b/openrct2.vcxproj index 5ea5b3b28f..2269f23310 100644 --- a/openrct2.vcxproj +++ b/openrct2.vcxproj @@ -200,10 +200,13 @@ + + + diff --git a/openrct2.vcxproj.filters b/openrct2.vcxproj.filters index 9e8e13016e..acc6194d01 100644 --- a/openrct2.vcxproj.filters +++ b/openrct2.vcxproj.filters @@ -887,5 +887,8 @@ Source\Core + + + \ No newline at end of file diff --git a/src/audio/mixer.cpp b/src/audio/mixer.cpp index 7f23802a26..44cefadf38 100644 --- a/src/audio/mixer.cpp +++ b/src/audio/mixer.cpp @@ -19,6 +19,7 @@ *****************************************************************************/ extern "C" { + #include "../addresses.h" #include "../config.h" #include "../platform/platform.h" #include "../localisation/localisation.h" diff --git a/src/core/Diagnostics.hpp b/src/core/Diagnostics.hpp new file mode 100644 index 0000000000..5019b8c0d8 --- /dev/null +++ b/src/core/Diagnostics.hpp @@ -0,0 +1,22 @@ +#pragma once + +#if _WIN32 +#include +#endif + +/** + * Utility methods for asserting and logging. + */ +namespace Debug +{ + void Break() + { +#if DEBUG +#if _WIN32 + if (IsDebuggerPresent()) { + DebugBreak(); + } +#endif +#endif + } +} diff --git a/src/core/Guard.hpp b/src/core/Guard.hpp new file mode 100644 index 0000000000..1f8cff8033 --- /dev/null +++ b/src/core/Guard.hpp @@ -0,0 +1,22 @@ +#pragma once + +#include +#include + +#include "Diagnostics.hpp" + +/** + * Utility methods for asserting function parameters. + */ +namespace Guard +{ + template + void ArgumentInRange(T argument, T min, T max, const char *message = nullptr) + { + Debug::Break(); + + fputs(message, stderr); + fputc('\n', stderr); + assert(argument < min || argument > max); + } +}; diff --git a/src/core/List.hpp b/src/core/List.hpp new file mode 100644 index 0000000000..0d8385238e --- /dev/null +++ b/src/core/List.hpp @@ -0,0 +1,76 @@ +#pragma once + +#include + +#include "../common.h" +#include "Guard.hpp" +#include "Memory.hpp" + +/** + * A container that stores elements in contiguous memory. Automatically reallocates memory when required. Equivalent to + * std::vector. + */ +template +class List : public std::vector +{ +public: + size_t GetCapacity() const { return this->capacity(); } + size_t GetCount() const { return this->size(); } + const T * GetItems() const { return this->data(); } + + List() : std::vector() { } + + List(size_t capacity) : std::vector(capacity) { } + + List(const T * items, size_t count) : std::vector(items, items + count) { } + + void EnsureCapacity(size_t capacity) + { + this->reserve(capacity); + } + + void ShrinkToLength() + { + this->shrink_to_fit(); + } + + void Clear() + { + this->clear(); + } + + void Add(T item) + { + this->push_back(item); + } + + void Insert(T item, size_t index) + { + Guard::ArgumentInRange(index, (size_t)0, this->size()); + this->insert() + } + + bool Remove(T item) + { + for (size_t i = 0; i < this->size(); i++) + { + if (_items[i] == item) + { + RemoveAt(i); + return true; + } + } + return false; + } + + void RemoveAt(size_t index) + { + Guard::ArgumentInRange(index, (size_t)0, this->size() - 1); + this->erase(this->begin() + index); + } + + const T * ToArray() const + { + return Memory::DuplicateArray(this->data(), this->size()); + } +}; diff --git a/src/core/Memory.hpp b/src/core/Memory.hpp index fcab3ac554..50d9dfbf6a 100644 --- a/src/core/Memory.hpp +++ b/src/core/Memory.hpp @@ -67,13 +67,6 @@ namespace Memory return (T*)memcpy((void*)dst, (const void*)src, size); } - template - T * CopyArray(T *dst, const T * src, size_t count) - { - if (count == 0) return (T*)dst; - return (T*)memcpy((void*)dst, (const void*)src, count * sizeof(T)); - } - template T * Duplicate(const T * src, size_t size) { @@ -81,16 +74,39 @@ namespace Memory return Copy(result, src, size); } - template - T * DuplicateArray(const T * src, size_t count) - { - T *result = AllocateArray(count); - return CopyArray(result, src, count); - } - template T * Set(T * dst, uint8 value, size_t size) { return (T*)memset((void*)dst, (int)value, size); } + + template + T * CopyArray(T * dst, const T * src, size_t count) + { + // Use a loop so that copy constructors are called + // compiler should optimise to memcpy if possible + T * result = dst; + for (; count > 0; count--) + { + *dst++ = *src++; + } + return result; + } + + template + T * DuplicateArray(const T * src, size_t count) + { + T * result = AllocateArray(count); + return CopyArray(result, src, count); + } + + template + void FreeArray(T * ptr, size_t count) + { + for (size_t i = 0; i < count; i++) + { + ptr[i]::~T(); + } + Free(ptr); + } }