DynArray, DynString, wgl extensions string
This commit is contained in:
63
src/DynArray.h
Normal file
63
src/DynArray.h
Normal file
@@ -0,0 +1,63 @@
|
||||
#ifndef DYN_ARRAY_H_
|
||||
#define DYN_ARRAY_H_
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "DynString.h"
|
||||
|
||||
#define DYN_ARRAY_EXPANDER(macro, args) macro args
|
||||
|
||||
#define DYN_ARRAY_IMPL(type) \
|
||||
DYN_ARRAY_ALLOC(type, DynArray_##type, DynArray_##type##_alloc) \
|
||||
DYN_ARRAY_PUSH_BACK(type, DynArray_##type, DynArray_##type##_push_back) \
|
||||
|
||||
#define DYN_ARRAY_SIGS(type) \
|
||||
DYN_ARRAY_ALLOC_SIG(DynArray_##type, DynArray_##type##_alloc); \
|
||||
DYN_ARRAY_PUSH_BACK_SIG(type, DynArray_##type, DynArray_##type##_push_back); \
|
||||
|
||||
#define DYN_ARRAY_STRUCT(type) \
|
||||
typedef struct { \
|
||||
type* data; \
|
||||
size_t count; \
|
||||
size_t capacity; \
|
||||
} DynArray_##type
|
||||
|
||||
// DYN_ARRAY_STRUCT(DynString);
|
||||
|
||||
#define DYN_ARRAY_ALLOC_SIG(type_name, func_name) int func_name(type_name* darr, const size_t capacity)
|
||||
|
||||
#define DYN_ARRAY_ALLOC(type, type_name, func_name) \
|
||||
DYN_ARRAY_ALLOC_SIG(type_name, func_name) { \
|
||||
darr->data = (type*) calloc(capacity, sizeof(type)); \
|
||||
if (darr->data == NULL) { \
|
||||
return -1; \
|
||||
} \
|
||||
darr->count = 0; \
|
||||
darr->capacity = capacity; \
|
||||
return 1; \
|
||||
}
|
||||
|
||||
#define DYN_ARRAY_PUSH_BACK_SIG(type, type_name, func_name) int func_name(type_name* darr, const type* new_elem)
|
||||
|
||||
#define DYN_ARRAY_PUSH_BACK(type, type_name, func_name) \
|
||||
DYN_ARRAY_PUSH_BACK_SIG(type, type_name, func_name) { \
|
||||
if (darr->count + 1 > darr->capacity) { \
|
||||
darr->capacity *= 2; \
|
||||
darr->data = (type*) realloc(darr->data, darr->capacity * sizeof(type)); \
|
||||
if (darr->data == NULL) { \
|
||||
return -1; \
|
||||
} \
|
||||
} \
|
||||
memcpy(darr->data + darr->count, new_elem, sizeof(type)); \
|
||||
darr->count++; \
|
||||
return 1; \
|
||||
}
|
||||
|
||||
// DYN_ARRAY_PUSH_BACK(DynString, DynArray_DynString, DynArray_DynString_push_back);
|
||||
|
||||
DYN_ARRAY_STRUCT(DynString);
|
||||
DYN_ARRAY_SIGS(DynString);
|
||||
|
||||
#endif // DYN_ARRAY_H_
|
||||
Reference in New Issue
Block a user