fix some issues with pushlist macros, some syntax, new EachEl
This commit is contained in:
51
core.h
51
core.h
@@ -7,7 +7,7 @@
|
||||
#include "time.h" // TODO(djledda): try not to depend on this one
|
||||
|
||||
// ### Misc macros ###
|
||||
#if ENABLE_ASSERT
|
||||
#if DJSTDLIB_DEBUG
|
||||
#define Assert(expression) if (!(expression)) {*(volatile int *)0 = 0;}
|
||||
#else
|
||||
#define Assert(expression)
|
||||
@@ -47,16 +47,18 @@ typedef struct string string;
|
||||
#define MemberSizeUnderlying(type, memberName) sizeof(*((type *)0)->memberName)
|
||||
|
||||
// ### Arenas ###
|
||||
typedef struct {
|
||||
typedef struct Arena Arena;
|
||||
struct Arena {
|
||||
void *memory;
|
||||
size_t capacity;
|
||||
size_t head;
|
||||
} Arena;
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
typedef struct Scratch Scratch;
|
||||
struct Scratch {
|
||||
Arena *arena;
|
||||
size_t start;
|
||||
} Scratch;
|
||||
};
|
||||
|
||||
void *pushSize(Arena *arena, size_t bytes);
|
||||
void *pushSizeFill(Arena *arena, size_t bytes, byte fill);
|
||||
@@ -76,13 +78,14 @@ void scratchEnd(Scratch scratch);
|
||||
#define PushStructZero(arena, type) (type *)pushSizeFill(arena, sizeof(type), 0)
|
||||
|
||||
// ### Vectors ###
|
||||
typedef union {
|
||||
typedef union Vec2 Vec2;
|
||||
union Vec2 {
|
||||
struct {
|
||||
real32 x;
|
||||
real32 y;
|
||||
};
|
||||
real32 vec[2];
|
||||
} Vec2;
|
||||
};
|
||||
inline function Vec2 vec2(real32 x, real32 y) {
|
||||
Vec2 result = {0};
|
||||
result.x = x;
|
||||
@@ -90,14 +93,15 @@ inline function Vec2 vec2(real32 x, real32 y) {
|
||||
return result;
|
||||
}
|
||||
|
||||
typedef union {
|
||||
typedef union Vec3 Vec3;
|
||||
union Vec3 {
|
||||
struct {
|
||||
real32 x;
|
||||
real32 y;
|
||||
real32 z;
|
||||
};
|
||||
real32 vec[3];
|
||||
} Vec3;
|
||||
};
|
||||
inline function Vec3 vec3(real32 x, real32 y, real32 z) {
|
||||
Vec3 result = {0};
|
||||
result.x = x;
|
||||
@@ -106,7 +110,8 @@ inline function Vec3 vec3(real32 x, real32 y, real32 z) {
|
||||
return result;
|
||||
}
|
||||
|
||||
typedef union {
|
||||
typedef union Vec4 Vec4;
|
||||
union Vec4 {
|
||||
struct {
|
||||
real32 r;
|
||||
real32 g;
|
||||
@@ -120,7 +125,7 @@ typedef union {
|
||||
real32 w;
|
||||
};
|
||||
real32 vec[4];
|
||||
} Vec4;
|
||||
};
|
||||
inline function Vec4 vec4(real32 x, real32 y, real32 z, real32 w) {
|
||||
Vec4 result = {0};
|
||||
result.x = x;
|
||||
@@ -132,20 +137,21 @@ inline function Vec4 vec4(real32 x, real32 y, real32 z, real32 w) {
|
||||
|
||||
// ### Lists ###
|
||||
#define DefineList(type, prefix) \
|
||||
typedef struct {\
|
||||
typedef struct prefix ## List prefix ## List;\
|
||||
struct prefix ## List {\
|
||||
type* data;\
|
||||
size_t length;\
|
||||
size_t capacity;\
|
||||
} prefix ## List;\
|
||||
};\
|
||||
typedef type prefix ## List ## _underlying
|
||||
#define ListElementSize(list) MemberSizeUnderlying(list, data)
|
||||
|
||||
DefineList(string, String);
|
||||
|
||||
#define PushList(arena, type, size) (type){ pushSize(arena, ListElementSize(type)*size), 0, size }
|
||||
#define PushListZero(arena, type, size) (type){ pushSizeFill(arena, ListElementSize(type)*size, 0), 0, size }
|
||||
#define PushFullList(arena, type, size) (type){ pushSize(arena, ListElementSize(type)*size), size, size }
|
||||
#define PushFullListZero(arena, type, size) (type){ pushSizeFill(arena, ListElementSize(type)*size, 0), size, size }
|
||||
#define PushList(arena, type, size) (type){ .data=pushSize(arena, ListElementSize(type)*(size)), .length=0, .capacity=(size) }
|
||||
#define PushListZero(arena, type, size) (type){ .data=pushSizeFill(arena, ListElementSize(type)*(size), 0), .length=0, .capacity=(size) }
|
||||
#define PushFullList(arena, type, size) (type){ .data=pushSize(arena, ListElementSize(type)*size), .length=(size), .capacity=(size) }
|
||||
#define PushFullListZero(arena, type, size) (type){ .data=pushSizeFill(arena, ListElementSize(type)*(size), 0), .length=(size), .capacity=(size) }
|
||||
|
||||
#define EmptyList() { NULL, 0, 0 }
|
||||
#define __ArrayAsList(array) { .data=(array), .length=ArrayCount(array), .capacity=ArrayCount(array) }
|
||||
@@ -199,16 +205,18 @@ StringList strSplit(Arena *arena, string splitStr, string inputStr);
|
||||
string strPrintfv(Arena *arena, const char *fmt, va_list args);
|
||||
string strPrintf(Arena *arena, const char *fmt, ...);
|
||||
|
||||
typedef struct {
|
||||
typedef struct ParsePositiveIntResult ParsePositiveIntResult;
|
||||
struct ParsePositiveIntResult {
|
||||
uint8 result;
|
||||
bool valid;
|
||||
} ParsePositiveIntResult;
|
||||
};
|
||||
ParsePositiveIntResult parsePositiveInt(string str, size_t *lengthPointer);
|
||||
|
||||
typedef struct {
|
||||
typedef struct ParsePositiveReal32Result ParsePositiveReal32Result;
|
||||
struct ParsePositiveReal32Result {
|
||||
real32 result;
|
||||
bool valid;
|
||||
} ParsePositiveReal32Result;
|
||||
};
|
||||
ParsePositiveReal32Result parsePositiveReal32(string str, size_t *lengthPointer);
|
||||
|
||||
inline function bool isNumeric(char c);
|
||||
@@ -290,6 +298,7 @@ extern void (*print)(const char *fmt, ...);
|
||||
|
||||
// ### Loops ###
|
||||
#define EachIn(list, it) size_t it = 0; it < (list).length; it++
|
||||
#define EachEl(list, type, element) type* element = (list).data; element < (list).data + (list).length; element += 1
|
||||
#define EachInReversed(list, it) size_t it = (list).length - 1; it >= 0 && it < (list).length; it--
|
||||
#define EachInArray(arr, it) size_t it = 0; it < ArrayCount(arr); ++it
|
||||
|
||||
|
||||
Reference in New Issue
Block a user