stb_ds v0.3: fixes for compiling client code in C++
Before adoption: this commit came in with the history the project adopted, so no landing records it.
add missing _wrapper suffixes disable clang rvalue support in C++ disable unit tests in VC6 C++ other tweaks
authored by Sean Barrett · committed by Sean Barrett · parent 39c0559 · Browse files · 2 files·+49 added−38 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+49 added−38 removed
- other:+49 added−38 removed
Provenance
- Commit
f9133c3- Tree
6b20ded- Author
Sean Barrett <sean@nothings.org>- Committer
Sean Barrett <sean@nothings.org>- Parent
39c0559
2 files changed+49 added−38 removed
modified:README.md+2 added-2 removed
15 unmodified lines161718192020212223248 unmodified lines33343536373738394041118 unmodified linesExpand all15 unmodified lines**[stb_truetype.h](stb_truetype.h)** | 1.21 | graphics | 4882 | parse, decode, and rasterize characters from truetype fonts**[stb_image_write.h](stb_image_write.h)** | 1.11 | graphics | 1621 | image writing to disk: PNG, TGA, BMP**[stb_image_resize.h](stb_image_resize.h)** | 0.95 | graphics | 2627 | resize images larger/smaller with good quality**[stb_rect_pack.h](stb_rect_pack.h)** | 1.00 | graphics | 628 | simple 2D rectangle packer with decent quality**[stb_ds.h](stb_ds.h)** | 0.2 | utility | 1627 | typesafe dynamic array and hash tables for C, will compile in C++**[stb_ds.h](stb_ds.h)** | 0.3 | utility | 1638 | typesafe dynamic array and hash tables for C, will compile in C++**[stb_sprintf.h](stb_sprintf.h)** | 1.06 | utility | 1860 | fast sprintf, snprintf for C/C++**[stretchy_buffer.h](stretchy_buffer.h)** | 1.03 | utility | 262 | typesafe dynamic array for C (i.e. approximation to vector<>), doesn't compile as C++**[stb_textedit.h](stb_textedit.h)** | 1.13 | user interface | 1404 | guts of a text editor for games etc implementing them from scratch**[stb_voxel_render.h](stb_voxel_render.h)** | 0.87 | 3D graphics | 3805 | Minecraft-esque voxel rendering "engine" with many more features8 unmodified lines**[stb.h](stb.h)** | 2.33 | misc | 14471 | helper functions for C, mostly redundant in C++; basically author's personal stuff**[stb_leakcheck.h](stb_leakcheck.h)** | 0.5 | misc | 190 | quick-and-dirty malloc/free leak-checking
Total libraries: 21 Total lines of C code: 55609Total lines of C code: 55620
FAQ---118 unmodified linesExpand allmodified:stb_ds.h+47 added-36 removed
1-/* stb_ds.h - v0.2 - public domain data structures - Sean Barrett 20191+/* stb_ds.h - v0.3 - public domain data structures - Sean Barrett 20192 3 This is a single-header-file library that provides easy-to-use4 dynamic arrays and hash tables for C (also works in C++).5 395 unmodified lines401 402 #if defined(__GNUC__) || defined(__clang__)403 #define STBDS_HAS_TYPEOF404 #ifdef __cplusplus405-#define STBDS_HAS_LITERAL_ARRAY405+//#define STBDS_HAS_LITERAL_ARRAY // this is currently broken for clang406 #endif407 #endif408 409 #if !defined(__cplusplus)3 unmodified lines413 #endif414 415 // this macro takes the address of the argument, but on gcc/clang can accept rvalues416 #if defined(STBDS_HAS_LITERAL_ARRAY) && defined(STBDS_HAS_TYPEOF)417+ #if __clang__418+ #define STBDS_ADDRESSOF(typevar, value) ((__typeof__(typevar)[1]){value}) // literal array decays to pointer to value419+ #else417-#define STBDS_ADDRESSOF(typevar, value) ((typeof(typevar)[1]){value}) // literal array decays to pointer to value420+ #define STBDS_ADDRESSOF(typevar, value) ((typeof(typevar)[]){value}) // literal array decays to pointer to value421+ #endif422 #else423 #define STBDS_ADDRESSOF(typevar, value) &(value)424 #endif425 20 unmodified lines446 447 #define stbds_arrmaybegrow(a,n) ((!(a) || stbds_header(a)->length + (n) > stbds_header(a)->capacity) \448 ? (stbds_arrgrow(a,n,0),0) : 0)449 446-#define stbds_arrgrow(a,b,c) ((a) = stbds_arrgrowf((a), sizeof *(a), (b), (c)))450+#define stbds_arrgrow(a,b,c) ((a) = stbds_arrgrowf_wrapper((a), sizeof *(a), (b), (c)))451 452 #define stbds_hmput(t, k, v) \449- ((t) = stbds_hmput_key_wrapper((t), sizeof *(t), STBDS_ADDRESSOF((t)->key, (k)), sizeof (t)->key, 0), \453+ ((t) = stbds_hmput_key_wrapper((t), sizeof *(t), (void*) STBDS_ADDRESSOF((t)->key, (k)), sizeof (t)->key, 0), \454 (t)[stbds_temp((t)-1)].key = (k), \455 (t)[stbds_temp((t)-1)].value = (v))456 457 #define stbds_hmputs(t, s) \458 ((t) = stbds_hmput_key_wrapper((t), sizeof *(t), &(s).key, sizeof (s).key, STBDS_HM_BINARY), \459 (t)[stbds_temp((t)-1)] = (s))460 461 #define stbds_hmgeti(t,k) \458- ((t) = stbds_hmget_key_wrapper((t), sizeof *(t), STBDS_ADDRESSOF((t)->key, (k)), sizeof (t)->key, STBDS_HM_BINARY), \462+ ((t) = stbds_hmget_key_wrapper((t), sizeof *(t), (void*) STBDS_ADDRESSOF((t)->key, (k)), sizeof (t)->key, STBDS_HM_BINARY), \463 stbds_temp((t)-1))464 465 #define stbds_hmgetp(t, k) \466 ((void) stbds_hmgeti(t,k), &(t)[stbds_temp((t)-1)])467 468 #define stbds_hmdel(t,k) \465- (((t) = stbds_hmdel_key_wrapper((t),sizeof *(t), STBDS_ADDRESSOF((t)->key, (k)), sizeof (t)->key, STBDS_OFFSETOF((t),key), STBDS_HM_BINARY)),(t)?stbds_temp((t)-1):0)469+ (((t) = stbds_hmdel_key_wrapper((t),sizeof *(t), (void*) STBDS_ADDRESSOF((t)->key, (k)), sizeof (t)->key, STBDS_OFFSETOF((t),key), STBDS_HM_BINARY)),(t)?stbds_temp((t)-1):0)470 471 #define stbds_hmdefault(t, v) \472 ((t) = stbds_hmput_default_wrapper((t), sizeof *(t)), (t)[-1].value = (v))473 8 unmodified lines482 #define stbds_hmlen(t) (stbds_arrlen((t)-1)-1)483 #define stbds_hmlenu(t) (stbds_arrlenu((t)-1)-1)484 485 #define stbds_shput(t, k, v) \482- ((t) = stbds_hmput_key_wrapper((t), sizeof *(t), (k), sizeof (t)->key, STBDS_HM_STRING), \486+ ((t) = stbds_hmput_key_wrapper((t), sizeof *(t), (void*) (k), sizeof (t)->key, STBDS_HM_STRING), \487 (t)[stbds_temp(t-1)].value = (v))488 489 #define stbds_shputs(t, s) \486- ((t) = stbds_hmput_key_wrapper((t), sizeof *(t), (s).key, sizeof (s).key, STBDS_HM_STRING), \490+ ((t) = stbds_hmput_key_wrapper((t), sizeof *(t), (void*) (s).key, sizeof (s).key, STBDS_HM_STRING), \491 (t)[stbds_temp(t-1)] = (s))492 493 #define stbds_shgeti(t,k) \490- ((t) = stbds_hmget_key_wrapper((t), sizeof *(t), (k), sizeof (t)->key, STBDS_HM_STRING), \494+ ((t) = stbds_hmget_key_wrapper((t), sizeof *(t), (void*) (k), sizeof (t)->key, STBDS_HM_STRING), \495 stbds_temp(t))496 497 #define stbds_shgetp(t, k) \498 ((void) stbds_shgeti(t,k), &(t)[stbds_temp(t-1)])499 500 #define stbds_shdel(t,k) \497- (((t) = stbds_hmdel_key((t),sizeof *(t), (k), sizeof (t)->key, STBDS_OFFSETOF((t),key), STBDS_HM_STRING)),(t)?stbds_temp((t)-1):0)501+ (((t) = stbds_hmdel_key_wrapper((t),sizeof *(t), (void*) (k), sizeof (t)->key, STBDS_OFFSETOF((t),key), STBDS_HM_STRING)),(t)?stbds_temp((t)-1):0)502 503 #define stbds_sh_new_arena(t) \504 ((t) = stbds_shmode_func_wrapper(t, sizeof *(t), STBDS_SH_ARENA))505 #define stbds_sh_new_strdup(t) \62 unmodified lines568 template<class T> static T * stbds_hmdel_key_wrapper(T *a, size_t elemsize, void *key, size_t keysize, size_t keyoffset, int mode){569 return (T*)stbds_hmdel_key((void*)a, elemsize, key, keysize, keyoffset, mode);570 }571 template<class T> static T * stbds_shmode_func_wrapper(T *, size_t elemsize, int mode) {568- return stbds_shmode_func(elemsize, mode);572+ return (T*)stbds_shmode_func(elemsize, mode);573 }574 #else575 #define stbds_arrgrowf_wrapper stbds_arrgrowf576 #define stbds_hmget_key_wrapper stbds_hmget_key870 unmodified lines1447 }1448 1449 void stbds_unit_tests(void)1450 {1451+#if defined(_MSC_VER) && _MSC_VER <= 1200 && defined(__cplusplus)1452+ // VC6 C++ doesn't like the template<> trick on unnamed structures, so do nothing!1453+ STBDS_ASSERT(0);1454+#else1455 const int testsize = 100000;1456 int *arr=NULL;1457 struct { int key; int value; } *intmap = NULL;1458 struct { char *key; int value; } *strmap = NULL;20 unmodified lines1479 1480 for (i=0; i < 5; ++i) {1481 arrpush(arr,1); arrpush(arr,2); arrpush(arr,3); arrpush(arr,4);1482 stbds_arrins(arr,i,5);1475- assert(arr[i] == 5);1483+ STBDS_ASSERT(arr[i] == 5);1484 if (i < 4)1477- assert(arr[4] == 4);1485+ STBDS_ASSERT(arr[4] == 4);1486 arrfree(arr);1487 }1488 1489 hmdefault(intmap, -1);1482- i=1; assert(hmget(intmap, i) == -1);1490+ i=1; STBDS_ASSERT(hmget(intmap, i) == -1);1491 for (i=0; i < testsize; i+=2)1492 hmput(intmap, i, i*5);1493 for (i=0; i < testsize; i+=1)1486- if (i & 1) assert(hmget(intmap, i) == -1 );1487- else assert(hmget(intmap, i) == i*5);1494+ if (i & 1) STBDS_ASSERT(hmget(intmap, i) == -1 );1495+ else STBDS_ASSERT(hmget(intmap, i) == i*5);1496 for (i=0; i < testsize; i+=2)1497 hmput(intmap, i, i*3);1498 for (i=0; i < testsize; i+=1)1491- if (i & 1) assert(hmget(intmap, i) == -1 );1492- else assert(hmget(intmap, i) == i*3);1499+ if (i & 1) STBDS_ASSERT(hmget(intmap, i) == -1 );1500+ else STBDS_ASSERT(hmget(intmap, i) == i*3);1501 for (i=2; i < testsize; i+=4)1502 hmdel(intmap, i); // delete half the entries1503 for (i=0; i < testsize; i+=1)1496- if (i & 3) assert(hmget(intmap, i) == -1 );1497- else assert(hmget(intmap, i) == i*3);1504+ if (i & 3) STBDS_ASSERT(hmget(intmap, i) == -1 );1505+ else STBDS_ASSERT(hmget(intmap, i) == i*3);1506 for (i=0; i < testsize; i+=1)1507 hmdel(intmap, i); // delete the rest of the entries 1508 for (i=0; i < testsize; i+=1)1501- assert(hmget(intmap, i) == -1 );1509+ STBDS_ASSERT(hmget(intmap, i) == -1 );1510 hmfree(intmap);1511 for (i=0; i < testsize; i+=2)1512 hmput(intmap, i, i*3);1513 hmfree(intmap);1514 1507- #ifdef __clang__1515+ #if defined(__clang__) || defined(__GNUC__)1516+ #ifndef __cplusplus1517 intmap = NULL;1518 hmput(intmap, 15, 7);1519 hmput(intmap, 11, 3);1520 hmput(intmap, 9, 5);1512- assert(hmget(intmap, 9) == 5);1513- assert(hmget(intmap, 11) == 3);1514- assert(hmget(intmap, 15) == 7);1521+ STBDS_ASSERT(hmget(intmap, 9) == 5);1522+ STBDS_ASSERT(hmget(intmap, 11) == 3);1523+ STBDS_ASSERT(hmget(intmap, 15) == 7);1524 #endif1525+ #endif1526 1527 for (i=0; i < testsize; ++i)1528 stralloc(&sa, strkey(i));1529 strreset(&sa);6 unmodified lines1536 shdefault(strmap, -1);1537 for (i=0; i < testsize; i+=2)1538 shput(strmap, strkey(i), i*3);1539 for (i=0; i < testsize; i+=1)1530- if (i & 1) assert(shget(strmap, strkey(i)) == -1 );1531- else assert(shget(strmap, strkey(i)) == i*3);1540+ if (i & 1) STBDS_ASSERT(shget(strmap, strkey(i)) == -1 );1541+ else STBDS_ASSERT(shget(strmap, strkey(i)) == i*3);1542 for (i=2; i < testsize; i+=4)1543 shdel(strmap, strkey(i)); // delete half the entries1544 for (i=0; i < testsize; i+=1)1535- if (i & 3) assert(shget(strmap, strkey(i)) == -1 );1536- else assert(shget(strmap, strkey(i)) == i*3);1545+ if (i & 3) STBDS_ASSERT(shget(strmap, strkey(i)) == -1 );1546+ else STBDS_ASSERT(shget(strmap, strkey(i)) == i*3);1547 for (i=0; i < testsize; i+=1)1548 shdel(strmap, strkey(i)); // delete the rest of the entries 1549 for (i=0; i < testsize; i+=1)1540- assert(shget(strmap, strkey(i)) == -1 );1550+ STBDS_ASSERT(shget(strmap, strkey(i)) == -1 );1551 shfree(strmap);1552 }1553 1554 {16 unmodified lines1571 1572 for (i=0; i < testsize; i += 1) {1573 stbds_struct s = { i,i*2,i*3 ,i*4 };1574 stbds_struct t = { i,i*2,i*3+1,i*4 };1565- if (i & 1) assert(hmget(map, s) == 0);1566- else assert(hmget(map, s) == i*5);1567- assert(hmget(map, t) == 0);1575+ if (i & 1) STBDS_ASSERT(hmget(map, s) == 0);1576+ else STBDS_ASSERT(hmget(map, s) == i*5);1577+ STBDS_ASSERT(hmget(map, t) == 0);1578 }1579 1580 for (i=0; i < testsize; i += 2) {1581 stbds_struct s = { i,i*2,i*3,i*4 };3 unmodified lines1585 1586 for (i=0; i < testsize; i += 1) {1587 stbds_struct s = { i,i*2,i*3,i*4 };1588 stbds_struct t = { i,i*2,i*3,i*4 };1579- if (i & 1) assert(hmgets(map2, s.key).d == 0);1580- else assert(hmgets(map2, s.key).d == i*4);1589+ if (i & 1) STBDS_ASSERT(hmgets(map2, s.key).d == 0);1590+ else STBDS_ASSERT(hmgets(map2, s.key).d == i*4);1591 }1592 hmfree(map2);1593+#endif1594 }1595 #endif1596 1597 41 unmodified lines