mirror of
https://github.com/OpenRCT2/OpenRCT2.git
synced 2025-01-23 10:51:58 -05:00
add more core classes
This commit is contained in:
parent
470ac384e2
commit
d67d6a4831
7 changed files with 157 additions and 14 deletions
|
@ -200,10 +200,13 @@
|
|||
<ClInclude Include="src\common.h" />
|
||||
<ClInclude Include="src\config.h" />
|
||||
<ClInclude Include="src\core\Console.hpp" />
|
||||
<ClInclude Include="src\core\Diagnostics.hpp" />
|
||||
<ClInclude Include="src\core\Exception.hpp" />
|
||||
<ClInclude Include="src\core\FileStream.hpp" />
|
||||
<ClInclude Include="src\core\Guard.hpp" />
|
||||
<ClInclude Include="src\core\IDisposable.hpp" />
|
||||
<ClInclude Include="src\core\IStream.hpp" />
|
||||
<ClInclude Include="src\core\List.hpp" />
|
||||
<ClInclude Include="src\core\Math.hpp" />
|
||||
<ClInclude Include="src\core\Memory.hpp" />
|
||||
<ClInclude Include="src\core\Path.hpp" />
|
||||
|
|
|
@ -887,5 +887,8 @@
|
|||
<ClInclude Include="src\core\Path.hpp">
|
||||
<Filter>Source\Core</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="src\core\List.hpp" />
|
||||
<ClInclude Include="src\core\Guard.hpp" />
|
||||
<ClInclude Include="src\core\Diagnostics.hpp" />
|
||||
</ItemGroup>
|
||||
</Project>
|
|
@ -19,6 +19,7 @@
|
|||
*****************************************************************************/
|
||||
|
||||
extern "C" {
|
||||
#include "../addresses.h"
|
||||
#include "../config.h"
|
||||
#include "../platform/platform.h"
|
||||
#include "../localisation/localisation.h"
|
||||
|
|
22
src/core/Diagnostics.hpp
Normal file
22
src/core/Diagnostics.hpp
Normal file
|
@ -0,0 +1,22 @@
|
|||
#pragma once
|
||||
|
||||
#if _WIN32
|
||||
#include <debugapi.h>
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Utility methods for asserting and logging.
|
||||
*/
|
||||
namespace Debug
|
||||
{
|
||||
void Break()
|
||||
{
|
||||
#if DEBUG
|
||||
#if _WIN32
|
||||
if (IsDebuggerPresent()) {
|
||||
DebugBreak();
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
}
|
22
src/core/Guard.hpp
Normal file
22
src/core/Guard.hpp
Normal file
|
@ -0,0 +1,22 @@
|
|||
#pragma once
|
||||
|
||||
#include <cassert>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "Diagnostics.hpp"
|
||||
|
||||
/**
|
||||
* Utility methods for asserting function parameters.
|
||||
*/
|
||||
namespace Guard
|
||||
{
|
||||
template<typename T>
|
||||
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);
|
||||
}
|
||||
};
|
76
src/core/List.hpp
Normal file
76
src/core/List.hpp
Normal file
|
@ -0,0 +1,76 @@
|
|||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
|
||||
#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<typename T>
|
||||
class List : public std::vector<T>
|
||||
{
|
||||
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<T>() { }
|
||||
|
||||
List(size_t capacity) : std::vector<T>(capacity) { }
|
||||
|
||||
List(const T * items, size_t count) : std::vector<T>(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());
|
||||
}
|
||||
};
|
|
@ -67,13 +67,6 @@ namespace Memory
|
|||
return (T*)memcpy((void*)dst, (const void*)src, size);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
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<typename T>
|
||||
T * Duplicate(const T * src, size_t size)
|
||||
{
|
||||
|
@ -81,16 +74,39 @@ namespace Memory
|
|||
return Copy(result, src, size);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T * DuplicateArray(const T * src, size_t count)
|
||||
{
|
||||
T *result = AllocateArray<T>(count);
|
||||
return CopyArray(result, src, count);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T * Set(T * dst, uint8 value, size_t size)
|
||||
{
|
||||
return (T*)memset((void*)dst, (int)value, size);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
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<typename T>
|
||||
T * DuplicateArray(const T * src, size_t count)
|
||||
{
|
||||
T * result = AllocateArray<T>(count);
|
||||
return CopyArray(result, src, count);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void FreeArray(T * ptr, size_t count)
|
||||
{
|
||||
for (size_t i = 0; i < count; i++)
|
||||
{
|
||||
ptr[i]::~T();
|
||||
}
|
||||
Free(ptr);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue