Mostly bugfix update + empty components support and remove EntityID from Event structure

-empty components now take no memory, so flag components is now far better
-added test for critical bug
-fixed critical bug with adding/removing entities form inside events
-fixed small bug with TestRunner
-improve basic tests
-fixed betterC compilation on DMD
-remove EntityID form Event structure
-added "return" attribute to some functions
-moved some code from Tempalte side to actual implementation
-fixed bug with EntityTemplate copying
-commented out some possibliy unused code
-use code formatter
This commit is contained in:
Mergul 2020-05-27 17:03:44 +02:00
parent 15cd57dbcb
commit 6929f5a748
16 changed files with 988 additions and 544 deletions

View file

@ -7,45 +7,50 @@ License: BSD 3-clause, see LICENSE file in project root folder.
*/
module bubel.ecs.std;
version(Emscripten)version = ECSEmscripten;
version (Emscripten) version = ECSEmscripten;
import std.traits;
version(ECSEmscripten)
version (ECSEmscripten)
{
extern(C) struct pthread_mutex_t
{
union
{
int[6] __i;
void[6] *__p;
extern (C) struct pthread_mutex_t
{
union
{
int[6] __i;
void[6]* __p;
}
}
extern(C) struct pthread_mutexattr_t
{
uint __attr;
}
extern(C) int memcmp (const void *s1, const void *s2, size_t size);
extern(C) void exit (int status) nothrow @nogc;
extern(C) void __assert(const(char)* msg, const(char)* file, uint line) { exit(-20);}
extern(C) void free(void*) @nogc nothrow @system;
extern(C) void* malloc(size_t size) @nogc nothrow @system;
extern(C) void* realloc(void*, size_t size) @nogc nothrow @system;
extern(C) void* memcpy(return void*, scope const void*, size_t size) @nogc nothrow @system;
extern(C) void* memset(void*, int val, size_t size) @nogc nothrow @system;
extern(C) int posix_memalign(void**, size_t, size_t) @nogc nothrow @system;
extern(C) void qsort(void* base, size_t num, size_t size, int function(const void*,const void*) compar) @nogc nothrow @system;
extern (C) struct pthread_mutexattr_t
{
uint __attr;
}
extern(C) int pthread_mutex_lock(pthread_mutex_t *mutex) @nogc nothrow;
extern(C) int pthread_mutex_trylock(pthread_mutex_t *mutex) @nogc nothrow;
extern(C) int pthread_mutex_unlock(pthread_mutex_t *mutex) @nogc nothrow;
extern(C) void pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type) @nogc nothrow;
extern(C) void pthread_mutexattr_destroy(pthread_mutexattr_t *attr) @nogc nothrow;
extern(C) int pthread_mutexattr_init(pthread_mutexattr_t *attr) @nogc nothrow;
extern(C) int pthread_mutex_destroy(pthread_mutex_t *mutex) @nogc nothrow;
extern(C) int pthread_mutex_init(pthread_mutex_t* mutex, const pthread_mutexattr_t* attr) @nogc nothrow;
extern (C) int memcmp(const void* s1, const void* s2, size_t size);
extern (C) void exit(int status) nothrow @nogc;
extern (C) void __assert(const(char)* msg, const(char)* file, uint line)
{
exit(-20);
}
extern (C) void free(void*) @nogc nothrow @system;
extern (C) void* malloc(size_t size) @nogc nothrow @system;
extern (C) void* realloc(void*, size_t size) @nogc nothrow @system;
extern (C) void* memcpy(return void*, scope const void*, size_t size) @nogc nothrow @system;
extern (C) void* memset(void*, int val, size_t size) @nogc nothrow @system;
extern (C) int posix_memalign(void**, size_t, size_t) @nogc nothrow @system;
extern (C) void qsort(void* base, size_t num, size_t size,
int function(const void*, const void*) compar) @nogc nothrow @system;
extern (C) int pthread_mutex_lock(pthread_mutex_t* mutex) @nogc nothrow;
extern (C) int pthread_mutex_trylock(pthread_mutex_t* mutex) @nogc nothrow;
extern (C) int pthread_mutex_unlock(pthread_mutex_t* mutex) @nogc nothrow;
extern (C) void pthread_mutexattr_settype(pthread_mutexattr_t* attr, int type) @nogc nothrow;
extern (C) void pthread_mutexattr_destroy(pthread_mutexattr_t* attr) @nogc nothrow;
extern (C) int pthread_mutexattr_init(pthread_mutexattr_t* attr) @nogc nothrow;
extern (C) int pthread_mutex_destroy(pthread_mutex_t* mutex) @nogc nothrow;
extern (C) int pthread_mutex_init(pthread_mutex_t* mutex, const pthread_mutexattr_t* attr) @nogc nothrow;
}
else
@ -55,23 +60,24 @@ else
public import core.stdc.stdlib : qsort;
}
version(ECSEmscripten)
version (ECSEmscripten)
{
}
else version (Windows)
{
import core.sys.windows.windows;
extern(Windows) void* _aligned_malloc(size_t size,size_t alignment) @nogc nothrow @system;
extern(Windows) void _aligned_free(void* ptr) @nogc nothrow @system;
version(LDC)
extern (Windows) void* _aligned_malloc(size_t size, size_t alignment) @nogc nothrow @system;
extern (Windows) void _aligned_free(void* ptr) @nogc nothrow @system;
version (LDC)
{
/*extern(Windows) void* __alloca(size_t size) @nogc nothrow @system;
alias alloca = __alloca;*/
extern(Windows) void ___chkstk_ms() @nogc nothrow @system;
extern(Windows) void __chkstk()
extern (Windows) void ___chkstk_ms() @nogc nothrow @system;
extern (Windows) void __chkstk()
{
___chkstk_ms();
}
@ -80,17 +86,18 @@ else version (Windows)
else version (Posix)
{
import core.sys.posix.pthread;
import core.sys.posix.stdlib;
import core.sys.posix.stdlib : posix_memalign;
}
version(ECSEmscripten)
version (ECSEmscripten)
{
private const uint max_alloca = 10000;
private const uint max_alloca = 10000;
private __gshared byte[max_alloca] alloca_array;
private __gshared uint alloca_pos = 0;
export extern(C) void* alloca(size_t length) @nogc nothrow
export extern (C) void* alloca(size_t length) @nogc nothrow
{
if(alloca_pos + length > max_alloca)alloca_pos = 0;
if (alloca_pos + length > max_alloca)
alloca_pos = 0;
void* ret = &alloca_array[alloca_pos];
alloca_pos += length;
return ret;
@ -101,28 +108,44 @@ version(ECSEmscripten)
return null;
}*/
}
else version(D_BetterC)
else version (D_BetterC)
{
private const uint max_alloca = 10000;
private const uint max_alloca = 10000;
private __gshared byte[max_alloca] alloca_array;
private uint alloca_pos = 0;
export extern(C) void* alloca(size_t length) @nogc nothrow
export extern (C) void* __alloca(size_t length) @nogc nothrow
{
if(alloca_pos + length > max_alloca)alloca_pos = 0;
if (alloca_pos + length > max_alloca)
alloca_pos = 0;
void* ret = &alloca_array[alloca_pos];
alloca_pos += length;
return ret;
}
version(GNU)
alias alloca = __alloca;
version (DigitalMars)
{
extern(C) void __gdc_personality_v0()
export extern (C) float* _memsetFloat(float* p, float value, size_t count) @nogc nothrow
{
float* pstart = p;
float* ptop;
for (ptop = &p[count]; p < ptop; p++)
*p = value;
return pstart;
}
}
version (GNU)
{
extern (C) void __gdc_personality_v0()
{
}
}
}
else
else
{
public import core.stdc.stdlib : alloca;
}
@ -131,13 +154,13 @@ static struct Mallocator
{
static T[] makeArray(T)(size_t length) nothrow @nogc
{
T[] ret = (cast(T*)malloc(T.sizeof * length))[0 .. length];
T[] ret = (cast(T*) malloc(T.sizeof * length))[0 .. length];
static if(__traits(isPOD, T))
static if (__traits(isPOD, T))
{
static immutable T init = T.init;
foreach(i;0..ret.length)
foreach (i; 0 .. ret.length)
{
memcpy(&ret[i], &init, T.sizeof);
}
@ -145,7 +168,8 @@ static struct Mallocator
else
{
static import std.conv;
foreach(i;0..ret.length)
foreach (i; 0 .. ret.length)
{
std.conv.emplace(&ret[i]);
}
@ -155,78 +179,94 @@ static struct Mallocator
static T[] makeArray(T)(size_t length, T initializer) nothrow @nogc
{
T[] ret = (cast(T*)malloc(T.sizeof * length))[0 .. length];
foreach(ref v; ret)v = initializer;
T[] ret = (cast(T*) malloc(T.sizeof * length))[0 .. length];
foreach (ref v; ret)
v = initializer;
return ret;
}
static T[] expandArray(T)(T[] array, size_t length) nothrow @nogc
{
size_t new_length = array.length + length;
return (cast(T*)realloc(array.ptr, T.sizeof * new_length))[0 .. new_length];
return (cast(T*) realloc(array.ptr, T.sizeof * new_length))[0 .. new_length];
}
static T[] makeArray(T)(T[] array) nothrow @nogc
{
T[] ret = (cast(T*)malloc(T.sizeof * array.length))[0 .. array.length];//Mallocator.makeArray!(T)(array.length);
foreach(i, ref v;ret)v = array[i];
T[] ret = (cast(T*) malloc(T.sizeof * array.length))[0 .. array.length]; //Mallocator.makeArray!(T)(array.length);
foreach (i, ref v; ret)
v = array[i];
return ret;
}
static T* make(T, Args...)(Args args)
static T* make(T, Args...)(Args args)
{
T* ret = cast(T*)malloc(T.sizeof);
T* ret = cast(T*) malloc(T.sizeof);
static import std.conv;
static if(__traits(isPOD, T))
static if (__traits(isPOD, T))
{
static immutable T init = T.init;
memcpy(ret, &init, T.sizeof);
}
else static if(is(T == struct))std.conv.emplace(ret, args);
else static if (is(T == struct))
std.conv.emplace(ret, args);
return ret;
}
static void* alignAlloc(size_t length, size_t alignment) nothrow @nogc
{
void* ret;
version(Posix)posix_memalign(&ret, alignment, length);//ret = aligned_alloc(alignment, length);
else version(Windows)ret = _aligned_malloc(length, alignment);
else version(ECSEmscripten)posix_memalign(&ret, alignment, length);//malloc(length);
else static assert(0, "Unimplemented platform!");
version (Posix)
posix_memalign(&ret, alignment, length); //ret = aligned_alloc(alignment, length);
else version (Windows)
ret = _aligned_malloc(length, alignment);
else version (ECSEmscripten)
posix_memalign(&ret, alignment, length); //malloc(length);
else
static assert(0, "Unimplemented platform!");
return ret;
}
static void dispose(T)(T object) nothrow @nogc
{
static if(__traits(hasMember, T, "__xdtor"))object.__xdtor();
else static if(__traits(hasMember, T, "__dtor"))object.__dtor();
free(cast(void*)object);
static if (__traits(hasMember, T, "__xdtor"))
object.__xdtor();
else static if (__traits(hasMember, T, "__dtor"))
object.__dtor();
free(cast(void*) object);
}
static void alignDispose(T)(T object)
{
static if(__traits(hasMember, T, "__xdtor"))object.__xdtor();
else static if(__traits(hasMember, T, "__dtor"))object.__dtor();
version(Posix)free(cast(void*)object);
else version(Windows)_aligned_free(cast(void*)object);
else version(ECSEmscripten)free(cast(void*)object);
else static assert(0, "Unimplemented platform!");
static if (__traits(hasMember, T, "__xdtor"))
object.__xdtor();
else static if (__traits(hasMember, T, "__dtor"))
object.__dtor();
version (Posix)
free(cast(void*) object);
else version (Windows)
_aligned_free(cast(void*) object);
else version (ECSEmscripten)
free(cast(void*) object);
else
static assert(0, "Unimplemented platform!");
}
}
struct Mutex
{
version(ECSEmscripten)
version (ECSEmscripten)
{
void initialize() nothrow @nogc
{
pthread_mutexattr_t attr = void;
//pthread_mutexattr_init(&attr);
//pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(cast(pthread_mutex_t*) &m_handle, &attr);
pthread_mutex_init(cast(pthread_mutex_t*)&m_handle, &attr);
//pthread_mutexattr_destroy(&attr);
}
@ -257,7 +297,7 @@ struct Mutex
{
void initialize() nothrow @nogc
{
InitializeCriticalSection(cast(CRITICAL_SECTION*) &m_handle);
InitializeCriticalSection(cast(CRITICAL_SECTION*)&m_handle);
}
void destroy() nothrow @nogc
@ -280,7 +320,7 @@ struct Mutex
return TryEnterCriticalSection(&m_handle) != 0;
}
CRITICAL_SECTION m_handle;
CRITICAL_SECTION m_handle;
}
else version (Posix)
{
@ -289,9 +329,9 @@ struct Mutex
pthread_mutexattr_t attr = void;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(cast(pthread_mutex_t*) &m_handle, &attr);
pthread_mutex_init(cast(pthread_mutex_t*)&m_handle, &attr);
pthread_mutexattr_destroy(&attr);
}
@ -318,5 +358,6 @@ struct Mutex
private pthread_mutex_t m_handle;
}
else static assert(0, "unsupported platform!");
}
else
static assert(0, "unsupported platform!");
}