Skip to content

Commit ed134bd

Browse files
committed
Improve C++ compatibility a bit
1 parent cd41af1 commit ed134bd

3 files changed

Lines changed: 32 additions & 18 deletions

File tree

nob.h

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,12 @@ NOBDEF bool nob_delete_file(const char *path);
196196
#define NOB_DA_INIT_CAP 256
197197
#endif
198198

199+
#ifdef __cplusplus
200+
#define NOB_DECLTYPE_CAST(T) (decltype(T))
201+
#else
202+
#define NOB_DECLTYPE_CAST(T)
203+
#endif // __cplusplus
204+
199205
#define nob_da_reserve(da, expected_capacity) \
200206
do { \
201207
if ((expected_capacity) > (da)->capacity) { \
@@ -205,7 +211,7 @@ NOBDEF bool nob_delete_file(const char *path);
205211
while ((expected_capacity) > (da)->capacity) { \
206212
(da)->capacity *= 2; \
207213
} \
208-
(da)->items = NOB_REALLOC((da)->items, (da)->capacity * sizeof(*(da)->items)); \
214+
(da)->items = NOB_DECLTYPE_CAST((da)->items)NOB_REALLOC((da)->items, (da)->capacity * sizeof(*(da)->items)); \
209215
NOB_ASSERT((da)->items != NULL && "Buy more RAM lol"); \
210216
} \
211217
} while (0)
@@ -340,7 +346,7 @@ typedef struct {
340346

341347
NOBDEF bool nob_cmd_run_opt(Nob_Cmd *cmd, Nob_Cmd_Opt opt);
342348

343-
#define nob_cmd_run(cmd, ...) nob_cmd_run_opt((cmd), (Nob_Cmd_Opt){0, __VA_ARGS__ })
349+
#define nob_cmd_run(cmd, ...) nob_cmd_run_opt((cmd), (Nob_Cmd_Opt){0, __VA_ARGS__})
344350

345351
// DEPRECATED:
346352
//
@@ -381,7 +387,6 @@ typedef struct {
381387
// use it as a C string.
382388
NOBDEF void nob_cmd_render(Nob_Cmd cmd, Nob_String_Builder *render);
383389

384-
// TODO: implement C++ support for nob.h
385390
#define nob_cmd_append(cmd, ...) \
386391
nob_da_append_many(cmd, \
387392
((const char*[]){__VA_ARGS__}), \
@@ -836,7 +841,7 @@ NOBDEF bool nob_copy_file(const char *src_path, const char *dst_path)
836841
int src_fd = -1;
837842
int dst_fd = -1;
838843
size_t buf_size = 32*1024;
839-
char *buf = NOB_REALLOC(NULL, buf_size);
844+
char *buf = (char*)NOB_REALLOC(NULL, buf_size);
840845
NOB_ASSERT(buf != NULL && "Buy more RAM lol!!");
841846
bool result = true;
842847

@@ -1326,6 +1331,7 @@ NOBDEF bool nob_read_entire_dir(const char *parent, Nob_File_Paths *children)
13261331
{
13271332
bool result = true;
13281333
DIR *dir = NULL;
1334+
struct dirent *ent = NULL;
13291335

13301336
dir = opendir(parent);
13311337
if (dir == NULL) {
@@ -1338,7 +1344,7 @@ NOBDEF bool nob_read_entire_dir(const char *parent, Nob_File_Paths *children)
13381344
}
13391345

13401346
errno = 0;
1341-
struct dirent *ent = readdir(dir);
1347+
ent = readdir(dir);
13421348
while (ent != NULL) {
13431349
nob_da_append(children, nob_temp_strdup(ent->d_name));
13441350
ent = readdir(dir);
@@ -1362,6 +1368,7 @@ NOBDEF bool nob_write_entire_file(const char *path, const void *data, size_t siz
13621368
{
13631369
bool result = true;
13641370

1371+
const char *buf = NULL;
13651372
FILE *f = fopen(path, "wb");
13661373
if (f == NULL) {
13671374
nob_log(NOB_ERROR, "Could not open file %s for writing: %s\n", path, strerror(errno));
@@ -1374,7 +1381,7 @@ NOBDEF bool nob_write_entire_file(const char *path, const void *data, size_t siz
13741381
// ^
13751382
// data
13761383

1377-
const char *buf = data;
1384+
buf = (const char*)data;
13781385
while (size > 0) {
13791386
size_t n = fwrite(buf, 1, size, f);
13801387
if (ferror(f)) {
@@ -1406,7 +1413,7 @@ NOBDEF Nob_File_Type nob_get_file_type(const char *path)
14061413
struct stat statbuf;
14071414
if (lstat(path, &statbuf) < 0) {
14081415
nob_log(NOB_ERROR, "Could not get stat of %s: %s", path, strerror(errno));
1409-
return -1;
1416+
return (Nob_File_Type)(-1);
14101417
}
14111418

14121419
if (S_ISREG(statbuf.st_mode)) return NOB_FILE_REGULAR;
@@ -1501,7 +1508,7 @@ NOBDEF bool nob_copy_directory_recursively(const char *src_path, const char *dst
15011508
NOBDEF char *nob_temp_strdup(const char *cstr)
15021509
{
15031510
size_t n = strlen(cstr);
1504-
char *result = nob_temp_alloc(n + 1);
1511+
char *result = (char*)nob_temp_alloc(n + 1);
15051512
NOB_ASSERT(result != NULL && "Increase NOB_TEMP_CAPACITY");
15061513
memcpy(result, cstr, n);
15071514
result[n] = '\0';
@@ -1526,7 +1533,7 @@ NOBDEF char *nob_temp_sprintf(const char *format, ...)
15261533
va_end(args);
15271534

15281535
NOB_ASSERT(n >= 0);
1529-
char *result = nob_temp_alloc(n + 1);
1536+
char *result = (char*)nob_temp_alloc(n + 1);
15301537
NOB_ASSERT(result != NULL && "Extend the size of the temporary allocator");
15311538
// TODO: use proper arenas for the temporary allocator;
15321539
va_start(args, format);
@@ -1553,7 +1560,7 @@ NOBDEF void nob_temp_rewind(size_t checkpoint)
15531560

15541561
NOBDEF const char *nob_temp_sv_to_cstr(Nob_String_View sv)
15551562
{
1556-
char *result = nob_temp_alloc(sv.count + 1);
1563+
char *result = (char*)nob_temp_alloc(sv.count + 1);
15571564
NOB_ASSERT(result != NULL && "Extend the size of the temporary allocator");
15581565
memcpy(result, sv.data, sv.count);
15591566
result[sv.count] = '\0';
@@ -1668,19 +1675,21 @@ NOBDEF bool nob_read_entire_file(const char *path, Nob_String_Builder *sb)
16681675
bool result = true;
16691676

16701677
FILE *f = fopen(path, "rb");
1678+
size_t new_count = 0;
1679+
long long m = 0;
16711680
if (f == NULL) nob_return_defer(false);
16721681
if (fseek(f, 0, SEEK_END) < 0) nob_return_defer(false);
16731682
#ifndef _WIN32
1674-
long m = ftell(f);
1683+
m = ftell(f);
16751684
#else
1676-
long long m = _ftelli64(f);
1685+
m = _ftelli64(f);
16771686
#endif
16781687
if (m < 0) nob_return_defer(false);
16791688
if (fseek(f, 0, SEEK_SET) < 0) nob_return_defer(false);
16801689

1681-
size_t new_count = sb->count + m;
1690+
new_count = sb->count + m;
16821691
if (new_count > sb->capacity) {
1683-
sb->items = NOB_REALLOC(sb->items, new_count);
1692+
sb->items = NOB_DECLTYPE_CAST(sb->items)NOB_REALLOC(sb->items, new_count);
16841693
NOB_ASSERT(sb->items != NULL && "Buy more RAM lool!!");
16851694
sb->capacity = new_count;
16861695
}

shared.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#define BUILD_FOLDER "build/"
99
#define TESTS_FOLDER "tests/"
1010

11+
// TODO: we should test on C++ compilers too
12+
1113
#if defined(_MSC_VER)
1214
# define nob_cc_flags(cmd) cmd_append(cmd, "/W4", "/nologo", "/D_CRT_SECURE_NO_WARNINGS", "-I.")
1315
#elif defined(__APPLE__) || defined(__MACH__)

tests/cmd_redirect.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ int main(void)
99
Cmd cmd = {0};
1010
Fd fdout = INVALID_FD;
1111
String_Builder sb = {0};
12+
String_View actual_message = {0};
13+
const char *message = NULL;
14+
const char *message_file_path = NULL;
1215

1316
const char *echo_src =
1417
"#include <assert.h>\n"
@@ -33,8 +36,8 @@ int main(void)
3336
nob_cc_inputs(&cmd, "./echo.c");
3437
if (!cmd_run(&cmd)) return_defer(1);
3538

36-
const char *message = "Hello, World";
37-
const char *message_file_path = "./echo_message.txt";
39+
message = "Hello, World";
40+
message_file_path = "./echo_message.txt";
3841

3942
fdout = fd_open_for_write(message_file_path);
4043
if (fdout == INVALID_FD) return_defer(1);
@@ -43,11 +46,11 @@ int main(void)
4346
if (!cmd_run(&cmd, .fdout = &fdout)) return_defer(1);
4447

4548
if (!read_entire_file(message_file_path, &sb)) return_defer(1);
46-
String_View actual_message = sb_to_sv(sb);
49+
actual_message = sb_to_sv(sb);
4750
if (!sv_eq(sv_trim(actual_message), sv_from_cstr(message))) {
4851
nob_log(ERROR, "Unexpected message");
4952
nob_log(ERROR, "Expected: %s", message);
50-
nob_log(ERROR, "Actual: "SV_Fmt, SV_Arg(actual_message));
53+
nob_log(ERROR, "Actual: " SV_Fmt, SV_Arg(actual_message));
5154
return_defer(1);
5255
}
5356

0 commit comments

Comments
 (0)