Add stream structure and utilities
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
// vim:fileencoding=utf-8:foldmethod=marker
|
||||
|
||||
#include "stream.h"
|
||||
#include "../../common/aliases/aliases.h"
|
||||
#include "../../common/assert/assert.h"
|
||||
|
||||
void _streamValidate(const WpStream *stream, u64 item_size);
|
||||
|
||||
b8 _streamAtEnd(const WpStream *stream, u64 item_size) {
|
||||
_streamValidate(stream, item_size);
|
||||
return stream->position >= stream->count;
|
||||
}
|
||||
|
||||
b8 _streamHasNext(const WpStream *stream, u64 item_size) {
|
||||
_streamValidate(stream, item_size);
|
||||
return stream->position + 1 < stream->count;
|
||||
}
|
||||
|
||||
void *_streamPeek(const WpStream *stream, u64 item_size) {
|
||||
if (_streamHasNext(stream, item_size)) {
|
||||
u64 index = (stream->position + 1) * stream->item_size;
|
||||
return (void *)(&((u8 *)(stream->data))[index]);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void *_streamConsume(WpStream *stream, u64 item_size) {
|
||||
if (!_streamAtEnd(stream, item_size)) {
|
||||
u64 index = stream->position++ * stream->item_size;
|
||||
return (void *)(&((u8 *)(stream->data))[index]);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void _streamValidate(const WpStream *stream, u64 item_size) {
|
||||
wpDebugAssert(stream != NULL, "`stream` should not be NULL");
|
||||
wpRuntimeAssert(stream->item_size == item_size, "Invalid item type provided");
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
// vim:fileencoding=utf-8:foldmethod=marker
|
||||
|
||||
#ifndef STREAM_H
|
||||
#define STREAM_H
|
||||
|
||||
#include "../../common/aliases/aliases.h"
|
||||
#include "../../common/platform/platform.h"
|
||||
|
||||
#ifdef WP_PLATFORM_CPP
|
||||
BEGIN_C_LINKAGE
|
||||
#endif // !WP_PLATFORM_CPP
|
||||
|
||||
typedef struct {
|
||||
void *data;
|
||||
u64 count;
|
||||
u64 position;
|
||||
u64 item_size;
|
||||
} WpStream;
|
||||
|
||||
// NOTE (Abdelrahman): WpStream typedefs for readability
|
||||
typedef WpStream WpVoidPtrStream;
|
||||
typedef WpStream WpC8Stream;
|
||||
typedef WpStream WpC16Stream;
|
||||
typedef WpStream WpC32Stream;
|
||||
typedef WpStream WpU8Stream;
|
||||
typedef WpStream WpU16Stream;
|
||||
typedef WpStream WpU32Stream;
|
||||
typedef WpStream WpU64Stream;
|
||||
typedef WpStream WpB8Stream;
|
||||
typedef WpStream WpI8Stream;
|
||||
typedef WpStream WpI16Stream;
|
||||
typedef WpStream WpI32Stream;
|
||||
typedef WpStream WpI64Stream;
|
||||
typedef WpStream WpF32Stream;
|
||||
typedef WpStream WpF64Stream;
|
||||
typedef WpStream WpF128Stream;
|
||||
typedef WpStream WpUptrStream;
|
||||
typedef WpStream WpIptrStream;
|
||||
typedef WpStream WpStr8Stream;
|
||||
|
||||
#ifdef WP_PLATFORM_CPP
|
||||
#define wpStream(TYPE, DATA, SIZE) ([&]() { \
|
||||
return WpStream{ \
|
||||
DATA, \
|
||||
SIZE / sizeof(TYPE), \
|
||||
0, \
|
||||
sizeof(TYPE) \
|
||||
}; \
|
||||
}())
|
||||
#else
|
||||
#define wpStream(TYPE, DATA, SIZE) ( \
|
||||
(WpStream){ \
|
||||
.data = DATA, \
|
||||
.count = SIZE / sizeof(TYPE), \
|
||||
.position = 0, \
|
||||
.item_size = sizeof(TYPE) \
|
||||
} \
|
||||
)
|
||||
#endif
|
||||
|
||||
#define wpStreamAtEnd(TYPE, STREAM) _streamAtEnd(STREAM, sizeof(TYPE))
|
||||
#define wpStreamHasNext(TYPE, STREAM) _streamHasNext(STREAM, sizeof(TYPE))
|
||||
#define wpStreamPeek(TYPE, STREAM) ((TYPE *)_streamPeek(STREAM, sizeof(TYPE)))
|
||||
#define wpStreamConsume(TYPE, STREAM) ((TYPE *)_streamConsume(STREAM, sizeof(TYPE)))
|
||||
|
||||
b8 _streamAtEnd(const WpStream *stream, u64 item_size);
|
||||
b8 _streamHasNext(const WpStream *stream, u64 item_size);
|
||||
void *_streamPeek(const WpStream *stream, u64 item_size);
|
||||
void *_streamConsume(WpStream *stream, u64 item_size);
|
||||
|
||||
#ifdef WP_PLATFORM_CPP
|
||||
END_C_LINKAGE
|
||||
#endif // !WP_PLATFORM_CPP
|
||||
|
||||
#endif // !STREAM_H
|
||||
@@ -7,6 +7,7 @@
|
||||
#include "array/array.c"
|
||||
#include "dbl_list/dbl_list.c"
|
||||
#include "queue/queue.c"
|
||||
#include "stream/stream.c"
|
||||
#include "mem/allocator/mem_allocator.c"
|
||||
#include "mem/utils/mem_utils.c"
|
||||
#include "strings/str8/str8.c"
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#include "array/array.h"
|
||||
#include "dbl_list/dbl_list.h"
|
||||
#include "queue/queue.h"
|
||||
#include "stream/stream.h"
|
||||
#include "mem/allocator/mem_allocator.h"
|
||||
#include "mem/utils/mem_utils.h"
|
||||
#include "strings/str8/str8.h"
|
||||
|
||||
Reference in New Issue
Block a user