From 1c3134218e4ce91bdbcc07596d12b377f6bb0c18 Mon Sep 17 00:00:00 2001 From: Ed_ Date: Thu, 5 Dec 2024 02:53:14 -0500 Subject: [PATCH] preogress on getting dependencies compilable in C-library --- gen_c_library/c_library.cpp | 40 +++++++--------- .../components/containers.hashtable.hpp | 9 +++- gen_c_library/components/misc.hpp | 6 +-- project/dependencies/basic_types.hpp | 4 ++ project/dependencies/containers.hpp | 44 +++++++++-------- project/dependencies/debug.cpp | 2 + project/dependencies/filesystem.cpp | 2 + project/dependencies/filesystem.hpp | 47 ++++++++++++------- project/dependencies/hashing.cpp | 2 + project/dependencies/hashing.hpp | 2 - project/dependencies/macros.hpp | 20 +++++++- project/dependencies/memory.cpp | 2 - project/dependencies/parsing.cpp | 3 +- project/dependencies/parsing.hpp | 2 + project/dependencies/platform.hpp | 8 ++-- project/dependencies/printing.cpp | 6 ++- project/dependencies/printing.hpp | 3 +- project/dependencies/string_ops.cpp | 2 + project/dependencies/strings.cpp | 4 ++ project/dependencies/strings.hpp | 43 +++++++++++------ project/dependencies/timing.cpp | 2 + project/dependencies/timing.hpp | 4 ++ project/gen.cpp | 38 +++++++-------- .../helpers/push_container_defines.inline.hpp | 45 ++++++++---------- 24 files changed, 206 insertions(+), 134 deletions(-) diff --git a/gen_c_library/c_library.cpp b/gen_c_library/c_library.cpp index 698b298..c46537a 100644 --- a/gen_c_library/c_library.cpp +++ b/gen_c_library/c_library.cpp @@ -26,7 +26,7 @@ GEN_NS_END using namespace gen; constexpr char const* generation_notice = -"// This file was generated automatially by gencpp's c_library.cpp" +"// This file was generated automatially by gencpp's c_library.cpp " "(See: https://github.com/Ed94/gencpp)\n\n"; constexpr StrC roll_own_dependencies_guard_start = txt(R"( @@ -198,7 +198,7 @@ int gen_main() (body_entry->Type) { case CT_Preprocess_If: { - ignore_preprocess_cond_block(txt("GEN_SUPPORT_CPP_MEMBER_FEATURES"), body_entry, body ); + ignore_preprocess_cond_block(txt("GEN_SUPPORT_CPP_MEMBER_FEATURES"), body_entry, body, new_body ); } break; @@ -213,10 +213,10 @@ int gen_main() break; case CT_Preprocess_If: { - b32 found = ignore_preprocess_cond_block(txt("GEN_SUPPORT_CPP_MEMBER_FEATURES"), entry, parsed_memory ); + b32 found = ignore_preprocess_cond_block(txt("GEN_SUPPORT_CPP_MEMBER_FEATURES"), entry, parsed_memory, memory ); if (found) break; - found = ignore_preprocess_cond_block(txt("GEN_SUPPORT_CPP_REFERENCES"), entry, parsed_memory ); + found = ignore_preprocess_cond_block(txt("GEN_SUPPORT_CPP_REFERENCES"), entry, parsed_memory, memory ); if (found) break; memory.append(entry); @@ -224,7 +224,7 @@ int gen_main() break; case CT_Preprocess_IfDef: { - b32 found = ignore_preprocess_cond_block(txt("GEN_INTELLISENSE_DIRECTIVES"), entry, parsed_memory ); + b32 found = ignore_preprocess_cond_block(txt("GEN_INTELLISENSE_DIRECTIVES"), entry, parsed_memory, memory ); if (found) break; memory.append(entry); @@ -251,14 +251,10 @@ int gen_main() } } - // CodeBody selectors = generate_generic_selectors(needs_selectors); - // memory.append_at(selectors, 0) - // memory.append(fmt_newline); - header.print( dump_to_scratch_and_retireve(memory) ); Code string_ops = scan_file( project_dir "dependencies/string_ops.hpp" ); - // header.print( string_ops ); + header.print( string_ops ); CodeBody printing_parsed = parse_file( project_dir "dependencies/printing.hpp" ); CodeBody printing = def_body(CT_Global_Body); @@ -268,7 +264,7 @@ int gen_main() { case CT_Preprocess_IfDef: { - b32 found = ignore_preprocess_cond_block(txt("GEN_INTELLISENSE_DIRECTIVES"), entry, printing_parsed ); + b32 found = ignore_preprocess_cond_block(txt("GEN_INTELLISENSE_DIRECTIVES"), entry, printing_parsed, printing ); if (found) break; printing.append(entry); @@ -290,7 +286,7 @@ int gen_main() break; } } - // header.print(dump_to_scratch_and_retireve(printing)); + header.print(dump_to_scratch_and_retireve(printing)); CodeBody containers = def_body(CT_Global_Body); { @@ -301,11 +297,11 @@ int gen_main() containers.append( def_pragma(code(endregion Containers))); } - // header.print(fmt_newline); - // header.print(dump_to_scratch_and_retireve(containers)); + header.print(fmt_newline); + header.print(dump_to_scratch_and_retireve(containers)); Code hashing = scan_file( project_dir "dependencies/hashing.hpp" ); - // header.print( hashing ); + header.print( hashing ); CodeBody parsed_strings = parse_file( project_dir "dependencies/strings.hpp" ); CodeBody strings = def_body(CT_Global_Body); @@ -318,8 +314,8 @@ int gen_main() CodePreprocessCond cond = cast(CodePreprocessCond, entry); if (cond->Content.starts_with(txt("GEN_COMPILER_C || ! GEN_SUPPORT_CPP_MEMBER_FEATURES"))) { - ++ entry; - strings.append(entry); // typedef + for (; entry != end(parsed_strings) && entry->Type != CT_Typedef; ++ entry) {} + strings.append(entry); strings.append(fmt_newline); for (; entry != end(parsed_strings) && entry->Type != CT_Preprocess_EndIf; ++ entry) {} @@ -327,16 +323,16 @@ int gen_main() break; } - bool found = ignore_preprocess_cond_block(txt("! GEN_COMPILER_C"), entry, parsed_strings); + bool found = ignore_preprocess_cond_block(txt("GEN_COMPILER_CPP"), entry, parsed_strings, strings); if (found) break; - found = ignore_preprocess_cond_block(txt("GEN_SUPPORT_CPP_REFERENCES"), entry, parsed_strings); + found = ignore_preprocess_cond_block(txt("GEN_SUPPORT_CPP_REFERENCES"), entry, parsed_strings, strings ); } break; case CT_Preprocess_IfDef: { - ignore_preprocess_cond_block(txt("GEN_INTELLISENSE_DIRECTIVES"), entry, parsed_strings ); + ignore_preprocess_cond_block(txt("GEN_INTELLISENSE_DIRECTIVES"), entry, parsed_strings, strings ); } break; @@ -362,7 +358,7 @@ int gen_main() (body_entry->Type) { case CT_Preprocess_If: { - b32 found = ignore_preprocess_cond_block(txt("! GEN_COMPILER_C"), body_entry, body ); + b32 found = ignore_preprocess_cond_block(txt("GEN_COMPILER_CPP"), body_entry, body, new_body ); if (found) break; new_body.append(body_entry); @@ -382,7 +378,7 @@ int gen_main() break; } } - // header.print(dump_to_scratch_and_retireve(strings)); + header.print(dump_to_scratch_and_retireve(strings)); Code filesystem = scan_file( project_dir "dependencies/filesystem.hpp" ); Code timing = scan_file( project_dir "dependencies/timing.hpp" ); diff --git a/gen_c_library/components/containers.hashtable.hpp b/gen_c_library/components/containers.hashtable.hpp index fe3ecb9..77670b1 100644 --- a/gen_c_library/components/containers.hashtable.hpp +++ b/gen_c_library/components/containers.hashtable.hpp @@ -7,7 +7,7 @@ using namespace gen; CodeBody gen_hashtable_base() { - return parse_global_body( code( + CodeBody struct_def = parse_global_body( code( typedef struct HT_FindResult HT_FindResult; struct HT_FindResult { @@ -16,6 +16,13 @@ CodeBody gen_hashtable_base() ssize EntryIndex; }; )); + + Code define_type = untyped_str(txt( +R"(#define Hashtable(_type) HTE_##_type +)" + )); + + return def_global_body(args(struct_def, define_type)); } CodeBody gen_hashtable( StrC type, StrC hashtable_name ) diff --git a/gen_c_library/components/misc.hpp b/gen_c_library/components/misc.hpp index 3916b23..ea6691d 100644 --- a/gen_c_library/components/misc.hpp +++ b/gen_c_library/components/misc.hpp @@ -5,7 +5,7 @@ -b32 ignore_preprocess_cond_block( StrC cond_sig, Code& entry_iter, CodeBody& body ) +b32 ignore_preprocess_cond_block( StrC cond_sig, Code& entry_iter, CodeBody& parsed_body, CodeBody& body ) { b32 found = false; CodePreprocessCond cond = cast(CodePreprocessCond, entry_iter); @@ -16,7 +16,7 @@ b32 ignore_preprocess_cond_block( StrC cond_sig, Code& entry_iter, CodeBody& bod s32 depth = 1; ++ entry_iter; - for(b32 continue_for = true; continue_for && entry_iter != body.end(); ) switch + for(b32 continue_for = true; continue_for && entry_iter != parsed_body.end(); ) switch (entry_iter->Type) { case CT_Preprocess_If: case CT_Preprocess_IfDef: @@ -27,7 +27,7 @@ b32 ignore_preprocess_cond_block( StrC cond_sig, Code& entry_iter, CodeBody& bod case CT_Preprocess_Else: ++ entry_iter; - for(; continue_for && entry_iter != body.end(); ++ entry_iter) + for(; continue_for && entry_iter != parsed_body.end(); ++ entry_iter) { if (entry_iter->Type == CT_Preprocess_EndIf) { diff --git a/project/dependencies/basic_types.hpp b/project/dependencies/basic_types.hpp index bdf8570..abef4b7 100644 --- a/project/dependencies/basic_types.hpp +++ b/project/dependencies/basic_types.hpp @@ -5,6 +5,7 @@ #endif #pragma region Basic Types +GEN_API_C_BEGIN #define GEN_U8_MIN 0u #define GEN_U8_MAX 0xffu @@ -127,11 +128,13 @@ typedef void* mem_ptr; typedef void const* mem_ptr_const ; #if ! GEN_COMPILER_C +GEN_API_C_END template uptr to_uptr( Type* ptr ) { return (uptr)ptr; } template sptr to_sptr( Type* ptr ) { return (sptr)ptr; } template mem_ptr to_mem_ptr ( Type ptr ) { return (mem_ptr) ptr; } template mem_ptr_const to_mem_ptr_const( Type ptr ) { return (mem_ptr_const)ptr; } +GEN_API_C_BEGIN #else #define to_uptr( ptr ) ((uptr)(ptr)) #define to_sptr( ptr ) ((sptr)(ptr)) @@ -140,4 +143,5 @@ template mem_ptr_const to_mem_ptr_const( Type ptr ) { return (mem #define to_mem_ptr_const( ptr) ((mem_ptr)ptr) #endif +GEN_API_C_END #pragma endregion Basic Types diff --git a/project/dependencies/containers.hpp b/project/dependencies/containers.hpp index cbf6232..e78058e 100644 --- a/project/dependencies/containers.hpp +++ b/project/dependencies/containers.hpp @@ -394,8 +394,8 @@ bool array_set_capacity(Array* array, usize new_capacity) // They are undefined in gen.hpp and gen.cpp at the end of the files. // We cpp library expects the user to use the regular calls as they can resolve the type fine. -#define array_init(type, allocator) array_init (allocator ) -#define array_init_reserve(type, allocator, cap) array_init_reserve (allocator, cap) +#define array_init(type, allocator) array_init (allocator ) +#define array_init_reserve(type, allocator, cap) array_init_reserve (allocator, cap) #define array_append_array(array, other) array_append < get_array_underlying_type(array) > (& array, other ) #define array_append_value(array, value) array_append < get_array_underlying_type(array) > (& array, value ) #define array_append_items(array, items, item_num) array_append < get_array_underlying_type(array) > (& array, items, item_num ) @@ -423,6 +423,10 @@ bool array_set_capacity(Array* array, usize new_capacity) template struct HashTable; +#ifndef get_hashtable_underlying_type +#define get_hashtable_underlying_type(table) typename TRemovePtr:: DataType +#endif + struct HashTableFindResult { ssize HashIndex; ssize PrevIndex; @@ -483,6 +487,8 @@ struct HashTable forceinline void map_mut(void (*proc)(u64, Type*)) { GEN_NS map_mut(*this, proc); } #pragma endregion Member Mapping #endif + + using DataType = Type; }; #if GEN_SUPPORT_CPP_REFERENCES @@ -708,23 +714,23 @@ bool hashtable_full(HashTable table) { return result; } -#define hashtable_init(type, allocator) hashtable_init (allocator) -#define hashtable_init_reserve(type, allocator, num) hashtable_init_reserve(allocator, num) -#define hashtable_clear(table) hashtable_clear < TStripPtr >(table) -#define hashtable_destroy(table) hashtable_destroy < TStripPtr
>(& table) -#define hashtable_get(table, key) hashtable_get < TStripPtr
>(table, key) -#define hashtable_grow(table) hashtable_grow < TStripPtr
>(& table) -#define hashtable_rehash(table, new_num) hashtable_rehash < TStripPtr
>(& table, new_num) -#define hashtable_rehash_fast(table) hashtable_rehash_fast < TStripPtr
>(table) -#define hashtable_remove(table, key) hashtable_remove < TStripPtr
>(table, key) -#define hashtable_remove_entry(table, idx) hashtable_remove_entry< TStripPtr
>(table, idx) -#define hashtable_set(table, key, value) hashtable_set < TStripPtr
>(& table, key, value) -#define hashtable_slot(table, key) hashtable_slot < TStripPtr
>(table, key) -#define hashtable_add_entry(table, key) hashtable_add_entry < TStripPtr
>(& table, key) -#define hashtable_find(table, key) hashtable_find < TStripPtr
>(table, key) -#define hashtable_full(table) hashtable_full < TStripPtr
>(table) -#define hashtable_map(table, map_proc) hashtable_map < TStripPtr
>(table, map_proc) -#define hashtable_map_mut(table, map_proc) hashtable_map_mut < TStripPtr
>(table, map_proc) +#define hashtable_init(type, allocator) hashtable_init (allocator) +#define hashtable_init_reserve(type, allocator, num) hashtable_init_reserve(allocator, num) +#define hashtable_clear(table) hashtable_clear < get_hashtable_underlying_type(table) >(table) +#define hashtable_destroy(table) hashtable_destroy < get_hashtable_underlying_type(table) >(& table) +#define hashtable_get(table, key) hashtable_get < get_hashtable_underlying_type(table) >(table, key) +#define hashtable_grow(table) hashtable_grow < get_hashtable_underlying_type(table) >(& table) +#define hashtable_rehash(table, new_num) hashtable_rehash < get_hashtable_underlying_type(table) >(& table, new_num) +#define hashtable_rehash_fast(table) hashtable_rehash_fast < get_hashtable_underlying_type(table) >(table) +#define hashtable_remove(table, key) hashtable_remove < get_hashtable_underlying_type(table) >(table, key) +#define hashtable_remove_entry(table, idx) hashtable_remove_entry< get_hashtable_underlying_type(table) >(table, idx) +#define hashtable_set(table, key, value) hashtable_set < get_hashtable_underlying_type(table) >(& table, key, value) +#define hashtable_slot(table, key) hashtable_slot < get_hashtable_underlying_type(table) >(table, key) +#define hashtable_add_entry(table, key) hashtable_add_entry < get_hashtable_underlying_type(table) >(& table, key) +#define hashtable_find(table, key) hashtable_find < get_hashtable_underlying_type(table) >(table, key) +#define hashtable_full(table) hashtable_full < get_hashtable_underlying_type(table) >(table) +#define hashtable_map(table, map_proc) hashtable_map < get_hashtable_underlying_type(table) >(table, map_proc) +#define hashtable_map_mut(table, map_proc) hashtable_map_mut < get_hashtable_underlying_type(table) >(table, map_proc) #pragma endregion HashTable diff --git a/project/dependencies/debug.cpp b/project/dependencies/debug.cpp index c5aaf59..5b66730 100644 --- a/project/dependencies/debug.cpp +++ b/project/dependencies/debug.cpp @@ -6,6 +6,7 @@ #endif #pragma region Debug +GEN_API_C_BEGIN void assert_handler( char const* condition, char const* file, s32 line, char const* msg, ... ) { @@ -45,4 +46,5 @@ s32 assert_crash( char const* condition ) } #endif +GEN_API_C_END #pragma endregion Debug diff --git a/project/dependencies/filesystem.cpp b/project/dependencies/filesystem.cpp index aa1d41d..d0e585d 100644 --- a/project/dependencies/filesystem.cpp +++ b/project/dependencies/filesystem.cpp @@ -4,6 +4,7 @@ #endif #pragma region File Handling +GEN_API_C_BEGIN #if defined( GEN_SYSTEM_WINDOWS ) || defined( GEN_SYSTEM_CYGWIN ) @@ -655,4 +656,5 @@ GEN_FILE_CLOSE_PROC( _memory_file_close ) FileOperations const memory_file_operations = { _memory_file_read, _memory_file_write, _memory_file_seek, _memory_file_close }; +GEN_API_C_END #pragma endregion File Handling diff --git a/project/dependencies/filesystem.hpp b/project/dependencies/filesystem.hpp index c788ab6..8151151 100644 --- a/project/dependencies/filesystem.hpp +++ b/project/dependencies/filesystem.hpp @@ -4,10 +4,11 @@ #endif #pragma region File Handling +GEN_API_C_BEGIN typedef u32 FileMode; -enum FileModeFlag +enum FileModeFlag_Def { EFileMode_READ = bit( 0 ), EFileMode_WRITE = bit( 1 ), @@ -15,16 +16,18 @@ enum FileModeFlag EFileMode_RW = bit( 3 ), GEN_FILE_MODES = EFileMode_READ | EFileMode_WRITE | EFileMode_APPEND | EFileMode_RW, }; +typedef enum FileModeFlag_Def FileModeFlag; // NOTE: Only used internally and for the file operations -enum SeekWhenceType +enum SeekWhenceType_Def { ESeekWhence_BEGIN = 0, ESeekWhence_CURRENT = 1, ESeekWhence_END = 2, }; +typedef enum SeekWhenceType_Def SeekWhenceType; -enum FileError +enum FileError_Def { EFileError_NONE, EFileError_INVALID, @@ -37,15 +40,17 @@ enum FileError EFileError_NAME_TOO_LONG, EFileError_UNKNOWN, }; +typedef enum FileError_Def FileError; -union FileDescriptor +union FileDescriptor_Def { void* p; sptr i; uptr u; }; +typedef union FileDescriptor_Def FileDescriptor; -typedef struct FileOperations FileOperations; +typedef struct FileOperations_Def FileOperations; #define GEN_FILE_OPEN_PROC( name ) FileError name( FileDescriptor* fd, FileOperations* ops, FileMode mode, char const* filename ) #define GEN_FILE_READ_AT_PROC( name ) b32 name( FileDescriptor fd, void* buffer, ssize size, s64 offset, ssize* bytes_read, b32 stop_at_newline ) @@ -59,35 +64,39 @@ typedef GEN_FILE_WRITE_AT_PROC( FileWriteProc ); typedef GEN_FILE_SEEK_PROC( FileSeekProc ); typedef GEN_FILE_CLOSE_PROC( FileCloseProc ); -struct FileOperations +struct FileOperations_Def { FileReadProc* read_at; FileWriteProc* write_at; FileSeekProc* seek; FileCloseProc* close; }; +typedef struct FileOperations_Def FileOperations; extern FileOperations const default_file_operations; typedef u64 FileTime; -enum DirType +enum DirType_Def { GEN_DIR_TYPE_FILE, GEN_DIR_TYPE_FOLDER, GEN_DIR_TYPE_UNKNOWN, }; +typedef enum DirType_Def DirType; -struct DirInfo; +struct DirInfo_Def; +typedef struct DirInfo_Def DirInfo; -struct DirEntry +struct DirEntry_Def { - char const* filename; - struct DirInfo* dir_info; - u8 type; + char const* filename; + DirInfo* dir_info; + u8 type; }; +typedef struct DirEntry_Def DirEntry; -struct DirInfo +struct DirInfo_Def { char const* fullpath; DirEntry* entries; // zpl_array @@ -97,7 +106,7 @@ struct DirInfo String buf; }; -struct FileInfo +struct FileInfo_Def { FileOperations ops; FileDescriptor fd; @@ -107,8 +116,9 @@ struct FileInfo FileTime last_write_time; DirEntry* dir; }; +typedef struct FileInfo_Def FileInfo; -enum FileStandardType +enum FileStandardType_Def { EFileStandard_INPUT, EFileStandard_OUTPUT, @@ -116,6 +126,7 @@ enum FileStandardType EFileStandard_COUNT, }; +typedef enum FileStandardType_Def FileStandardType; /** * Get standard file I/O. @@ -257,7 +268,7 @@ b32 file_write_at( FileInfo* file, void const* buffer, ssize size, s64 offset ); */ b32 file_write_at_check( FileInfo* file, void const* buffer, ssize size, s64 offset, ssize* bytes_written ); -enum FileStreamFlags : u32 +enum FileStreamFlags_Def enum_underlying(u32) { /* Allows us to write to the buffer directly. Beware: you can not append a new data! */ EFileStream_WRITABLE = bit( 0 ), @@ -265,7 +276,10 @@ enum FileStreamFlags : u32 /* Clones the input buffer so you can write (zpl_file_write*) data into it. */ /* Since we work with a clone, the buffer size can dynamically grow as well. */ EFileStream_CLONE_WRITABLE = bit( 1 ), + + EFileStream_UNDERLYING = GEN_U32_MAX, }; +typedef enum FileStreamFlags_Def FileStreamFlags; /** * Opens a new memory stream @@ -381,4 +395,5 @@ b32 file_write_at_check( FileInfo* f, void const* buffer, ssize size, s64 offset return f->ops.write_at( f->fd, buffer, size, offset, bytes_written ); } +GEN_API_C_END #pragma endregion File Handling diff --git a/project/dependencies/hashing.cpp b/project/dependencies/hashing.cpp index 6ac5358..d11b9c7 100644 --- a/project/dependencies/hashing.cpp +++ b/project/dependencies/hashing.cpp @@ -4,6 +4,7 @@ #endif #pragma region Hashing +GEN_API_C_BEGIN global u32 const _crc32_table[ 256 ] = { 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, @@ -87,4 +88,5 @@ u64 crc64( void const* data, ssize len ) return result; } +GEN_API_C_END #pragma endregion Hashing diff --git a/project/dependencies/hashing.hpp b/project/dependencies/hashing.hpp index 57642a6..53dffcd 100644 --- a/project/dependencies/hashing.hpp +++ b/project/dependencies/hashing.hpp @@ -4,12 +4,10 @@ #endif #pragma region Hashing - GEN_API_C_BEGIN u32 crc32( void const* data, ssize len ); u64 crc64( void const* data, ssize len ); GEN_API_C_END - #pragma endregion Hashing diff --git a/project/dependencies/macros.hpp b/project/dependencies/macros.hpp index df7132a..4a92349 100644 --- a/project/dependencies/macros.hpp +++ b/project/dependencies/macros.hpp @@ -183,6 +183,18 @@ # endif #endif +#if GEN_COMPILER_CPP +// Already Defined +#elif GEN_COMPILER_C && __STDC_VERSION__ >= 201112L +# define thread_local _Thread_local +#elif GEN_COMPILER_MSVC +# define thread_local __declspec(thread) +#elif GEN_COMPILER_CLANG +# define thread_local __thread +#else +# error "No thread local support" +#endif + #if !defined(GEN_SUPPORT_CPP_REFERENCES) # define GEN_SUPPORT_CPP_REFERENCES 1 #endif @@ -203,7 +215,7 @@ # if ! GEN_COMPILER_C # define typeof decltype # elif defined(_MSC_VER) -# define typeof(x) __typeof(x) +# define typeof(x) __typeof__(x) # elif defined(__GNUC__) || defined(__clang__) # define typeof(x) __typeof__(x) # else @@ -247,4 +259,10 @@ # define GEN_PARAM_DEFAULT #endif +#if GEN_COMPILER_CPP + #define struct_init(type, value) {value} +#else + #define struct_init(type, value) {value} +#endif + #pragma endregion Macros diff --git a/project/dependencies/memory.cpp b/project/dependencies/memory.cpp index 33a7cf3..f53c8fc 100644 --- a/project/dependencies/memory.cpp +++ b/project/dependencies/memory.cpp @@ -4,7 +4,6 @@ #endif #pragma region Memory - GEN_API_C_BEGIN void* mem_copy( void* dest, void const* source, ssize n ) @@ -520,5 +519,4 @@ void pool_clear(Pool* pool) } GEN_API_C_END - #pragma endregion Memory diff --git a/project/dependencies/parsing.cpp b/project/dependencies/parsing.cpp index eb3338a..c05249c 100644 --- a/project/dependencies/parsing.cpp +++ b/project/dependencies/parsing.cpp @@ -3,6 +3,7 @@ #endif #pragma region ADT +GEN_API_C_BEGIN #define _adt_fprintf( s_, fmt_, ... ) \ do \ @@ -1110,5 +1111,5 @@ String csv_write_string_delimiter( AllocatorInfo a, CSV_Object* obj, char delimi return output; } +GEN_API_C_END #pragma endregion CSV - diff --git a/project/dependencies/parsing.hpp b/project/dependencies/parsing.hpp index b0a721d..01527ad 100644 --- a/project/dependencies/parsing.hpp +++ b/project/dependencies/parsing.hpp @@ -3,6 +3,7 @@ #endif #pragma region ADT +GEN_API_C_BEGIN enum ADT_Type : u32 { @@ -429,4 +430,5 @@ String csv_write_string( AllocatorInfo a, CSV_Object* obj ) return csv_write_string_delimiter( a, obj, ',' ); } +GEN_API_C_END #pragma endregion CSV diff --git a/project/dependencies/platform.hpp b/project/dependencies/platform.hpp index 1c3c5df..76f0a77 100644 --- a/project/dependencies/platform.hpp +++ b/project/dependencies/platform.hpp @@ -158,7 +158,7 @@ # define GEN_NS_PARSER_END } # define GEN_USING_NS_PARSER using namespace parser # define GEN_NS_PARSER parser:: -# define GEN_NS :: +# define GEN_NS :: # define GEN_NS_BEGIN # define GEN_NS_END # endif @@ -167,7 +167,7 @@ # define GEN_NS_PARSER_END } # define GEN_NS_PARSER parser:: # define GEN_USING_NS_PARSER using namespace parser -# define GEN_NS gen:: -# define GEN_NS_BEGIN namespace gen { -# define GEN_NS_END } +# define GEN_NS gen:: +# define GEN_NS_BEGIN namespace gen { +# define GEN_NS_END } #endif diff --git a/project/dependencies/printing.cpp b/project/dependencies/printing.cpp index 3bb495a..5939106 100644 --- a/project/dependencies/printing.cpp +++ b/project/dependencies/printing.cpp @@ -6,6 +6,7 @@ #endif #pragma region Printing +GEN_API_C_BEGIN enum { @@ -549,7 +550,7 @@ char* str_fmt_buf( char const* fmt, ... ) return str; } -ssize str_fmt_file_va( struct FileInfo* f, char const* fmt, va_list va ) +ssize str_fmt_file_va( FileInfo* f, char const* fmt, va_list va ) { local_persist thread_local char buf[ GEN_PRINTF_MAXLEN ]; ssize len = str_fmt_va( buf, size_of( buf ), fmt, va ); @@ -557,7 +558,7 @@ ssize str_fmt_file_va( struct FileInfo* f, char const* fmt, va_list va ) return res ? len : -1; } -ssize str_fmt_file( struct FileInfo* f, char const* fmt, ... ) +ssize str_fmt_file( FileInfo* f, char const* fmt, ... ) { ssize res; va_list va; @@ -597,4 +598,5 @@ ssize str_fmt_out_err( char const* fmt, ... ) return res; } +GEN_API_C_END #pragma endregion Printing diff --git a/project/dependencies/printing.hpp b/project/dependencies/printing.hpp index e5cecf2..2f3148f 100644 --- a/project/dependencies/printing.hpp +++ b/project/dependencies/printing.hpp @@ -7,11 +7,12 @@ GEN_API_C_BEGIN -struct FileInfo; +typedef struct FileInfo_Def FileInfo; #ifndef GEN_PRINTF_MAXLEN # define GEN_PRINTF_MAXLEN kilobytes(128) #endif +typedef char PrintF_Buffer[GEN_PRINTF_MAXLEN]; // NOTE: A locally persisting buffer is used internally char* str_fmt_buf ( char const* fmt, ... ); diff --git a/project/dependencies/string_ops.cpp b/project/dependencies/string_ops.cpp index 954d40a..8edfc2f 100644 --- a/project/dependencies/string_ops.cpp +++ b/project/dependencies/string_ops.cpp @@ -5,6 +5,7 @@ #endif #pragma region String Ops +GEN_API_C_BEGIN internal ssize _scan_zpl_i64( const char* text, s32 base, s64* value ) @@ -212,4 +213,5 @@ f64 str_to_f64( const char* str, char** end_ptr ) return result; } +GEN_API_C_END #pragma endregion String Ops diff --git a/project/dependencies/strings.cpp b/project/dependencies/strings.cpp index 209ee47..40e70cd 100644 --- a/project/dependencies/strings.cpp +++ b/project/dependencies/strings.cpp @@ -4,6 +4,8 @@ #endif #pragma region String +GEN_API_C_BEGIN + String string_make_length( AllocatorInfo allocator, char const* str, ssize length ) { constexpr ssize header_size = sizeof( StringHeader ); @@ -51,4 +53,6 @@ String string_make_reserve( AllocatorInfo allocator, ssize capacity ) String result = { rcast(char*, allocation) + header_size }; return result; } + +GEN_API_C_END #pragma endregion String diff --git a/project/dependencies/strings.hpp b/project/dependencies/strings.hpp index e8d7332..a9deb41 100644 --- a/project/dependencies/strings.hpp +++ b/project/dependencies/strings.hpp @@ -7,7 +7,8 @@ GEN_API_C_BEGIN -struct StrC; +struct StrC_Def; +typedef struct StrC_Def StrC; bool strc_are_equal (StrC lhs, StrC rhs); char const* strc_back (StrC str); @@ -20,7 +21,7 @@ StrC strc_visualize_whitespace(StrC str, AllocatorInfo allocator); GEN_API_C_END // Constant string with length. -struct StrC +struct StrC_Def { ssize Len; char const* Ptr; @@ -41,7 +42,14 @@ struct StrC }; #define cast_to_strc( str ) * rcast( StrC*, (str) - sizeof(ssize) ) -#define txt( text ) StrC { sizeof( text ) - 1, ( text ) } + +#ifndef txt +# if GEN_COMPILER_CPP +# define txt( text ) StrC { sizeof( text ) - 1, ( text ) } +# else +# define txt( text ) (StrC){ sizeof( text ) - 1, ( text ) } +# endif +#endif GEN_API_C_BEGIN forceinline char const* strc_begin(StrC str) { return str.Ptr; } @@ -64,7 +72,7 @@ bool strc_are_equal(StrC lhs, StrC rhs) return false; for (ssize idx = 0; idx < lhs.Len; ++idx) - if (lhs[idx] != rhs[idx]) + if (lhs.Ptr[idx] != rhs.Ptr[idx]) return false; return true; @@ -102,7 +110,8 @@ b32 strc_starts_with(StrC str, StrC substring) { inline StrC to_strc_from_c_str( char const* bad_str ) { - return { str_len( bad_str ), bad_str }; + StrC result = { str_len( bad_str ), bad_str }; + return result; } GEN_API_C_END @@ -111,7 +120,8 @@ GEN_API_C_END // They used a header pattern // I kept it for simplicty of porting but its not necessary to keep it that way. #pragma region String -struct StringHeader; + struct StringHeader_Def; +typedef struct StringHeader_Def StringHeader; #if GEN_COMPILER_C || ! GEN_SUPPORT_CPP_MEMBER_FEATURES typedef char* String; @@ -160,7 +170,7 @@ String string_visualize_whitespace(String const str); GEN_API_C_END -struct StringHeader { +struct StringHeader_Def { AllocatorInfo Allocator; ssize Capacity; ssize Length; @@ -324,7 +334,7 @@ inline String string_fmt_buf(AllocatorInfo allocator, char const* fmt, ...) { local_persist thread_local - char buf[GEN_PRINTF_MAXLEN] = { 0 }; + PrintF_Buffer buf = struct_init(PrintF_Buffer, {0}); va_list va; va_start(va, fmt); @@ -366,7 +376,7 @@ inline bool string_append_c_str_len(String* str, char const* str_to_append, ssize append_length) { GEN_ASSERT(str != nullptr); - if (sptr(str_to_append) > 0) + if ( rcast(sptr, str_to_append) > 0) { ssize curr_len = string_length(* str); @@ -521,8 +531,8 @@ StringHeader* string_get_header(String str) { forceinline ssize string_length(String const str) { - StringHeader const& header = *rcast(StringHeader const*, scast(char const*, str) - sizeof(StringHeader)); - return header.Length; + StringHeader const* header = rcast(StringHeader const*, scast(char const*, str) - sizeof(StringHeader)); + return header->Length; } inline @@ -627,7 +637,8 @@ void strip_space(String str) forceinline StrC string_to_strc(String str) { - return { string_length(str), (char const*)str }; + StrC result = { string_length(str), (char const*)str }; + return result; } inline @@ -695,15 +706,17 @@ String visualize_whitespace(String const str) } #pragma endregion String +#if GEN_COMPILER_CPP struct String_POD { char* Data; }; static_assert( sizeof( String_POD ) == sizeof( String ), "String is not a POD" ); +#endif forceinline StrC strc_duplicate(StrC str, AllocatorInfo allocator) { - String result = string_make_length(allocator, str.Ptr, str.Len); - return { string_get_header(result)->Length, result }; + StrC result = string_to_strc( string_make_length(allocator, str.Ptr, str.Len)); + return result; } inline @@ -745,5 +758,5 @@ typedef StrC StringCached; GEN_API_C_END // Implements basic string interning. Data structure is based off the ZPL Hashtable. -typedef HashTable StringTable; +typedef HashTable(StringCached) StringTable; #pragma endregion Strings diff --git a/project/dependencies/timing.cpp b/project/dependencies/timing.cpp index 5258045..91840c6 100644 --- a/project/dependencies/timing.cpp +++ b/project/dependencies/timing.cpp @@ -4,6 +4,7 @@ #endif #pragma region Timing +GEN_API_C_BEGIN #ifdef GEN_BENCHMARK #if defined( GEN_COMPILER_MSVC ) && ! defined( __clang__ ) @@ -164,4 +165,5 @@ } #endif +GEN_API_C_END #pragma endregion Timing diff --git a/project/dependencies/timing.hpp b/project/dependencies/timing.hpp index e4b9575..d9ac5ca 100644 --- a/project/dependencies/timing.hpp +++ b/project/dependencies/timing.hpp @@ -5,6 +5,8 @@ #pragma region Timing +GEN_API_C_BEGIN + #ifdef GEN_BENCHMARK //! Return CPU timestamp. u64 read_cpu_time_stamp_counter( void ); @@ -16,4 +18,6 @@ f64 time_rel( void ); u64 time_rel_ms( void ); #endif +GEN_API_C_END + #pragma endregion Timing diff --git a/project/gen.cpp b/project/gen.cpp index 4e60a3b..2933c8d 100644 --- a/project/gen.cpp +++ b/project/gen.cpp @@ -13,8 +13,8 @@ // They are undefined in gen.hpp and gen.cpp at the end of the files. // We cpp library expects the user to use the regular calls as they can resolve the type fine. -#define array_init(type, allocator) array_init (allocator ) -#define array_init_reserve(type, allocator, cap) array_init_reserve (allocator, cap) +#define array_init(type, allocator) array_init (allocator ) +#define array_init_reserve(type, allocator, cap) array_init_reserve (allocator, cap) #define array_append_array(array, other) array_append < get_array_underlying_type(array) > (& array, other ) #define array_append_value(array, value) array_append < get_array_underlying_type(array) > (& array, value ) #define array_append_items(array, items, item_num) array_append < get_array_underlying_type(array) > (& array, items, item_num ) @@ -33,23 +33,23 @@ #define arary_set_capacity(new_capacity) arary_set_capacity < get_array_underlying_type(array) > (& array, new_capacity ) #define arary_get_header(array) arary_get_header < get_array_underlying_type(array) > (array ) -#define hashtable_init(type, allocator) hashtable_init (allocator) -#define hashtable_init_reserve(type, allocator, num) hashtable_init_reserve(allocator, num) -#define hashtable_clear(table) hashtable_clear (table) -#define hashtable_destroy(table) hashtable_destroy (& table) -#define hashtable_get(table, key) hashtable_get (table, key) -#define hashtable_grow(table) hashtable_grow (& table) -#define hashtable_rehash(table, new_num) hashtable_rehash (& table, new_num) -#define hashtable_rehash_fast(table) hashtable_rehash_fast (table) -#define hashtable_remove(table, key) hashtable_remove (table, key) -#define hashtable_remove_entry(table, idx) hashtable_remove_entry(table, idx) -#define hashtable_set(table, key, value) hashtable_set (& table, key, value) -#define hashtable_slot(table, key) hashtable_slot (table, key) -#define hashtable_add_entry(table, key) hashtable_add_entry (& table, key) -#define hashtable_find(table, key) hashtable_find (table, key) -#define hashtable_full(table) hashtable_full (table) -#define hashtable_map(table, map_proc) hashtable_map (table, map_proc) -#define hashtable_map_mut(table, map_proc) hashtable_map_mut (table, map_proc) +#define hashtable_init(type, allocator) hashtable_init (allocator) +#define hashtable_init_reserve(type, allocator, num) hashtable_init_reserve(allocator, num) +#define hashtable_clear(table) hashtable_clear < get_hashtable_underlying_type(table) >(table) +#define hashtable_destroy(table) hashtable_destroy < get_hashtable_underlying_type(table) >(& table) +#define hashtable_get(table, key) hashtable_get < get_hashtable_underlying_type(table) >(table, key) +#define hashtable_grow(table) hashtable_grow < get_hashtable_underlying_type(table) >(& table) +#define hashtable_rehash(table, new_num) hashtable_rehash < get_hashtable_underlying_type(table) >(& table, new_num) +#define hashtable_rehash_fast(table) hashtable_rehash_fast < get_hashtable_underlying_type(table) >(table) +#define hashtable_remove(table, key) hashtable_remove < get_hashtable_underlying_type(table) >(table, key) +#define hashtable_remove_entry(table, idx) hashtable_remove_entry< get_hashtable_underlying_type(table) >(table, idx) +#define hashtable_set(table, key, value) hashtable_set < get_hashtable_underlying_type(table) >(& table, key, value) +#define hashtable_slot(table, key) hashtable_slot < get_hashtable_underlying_type(table) >(table, key) +#define hashtable_add_entry(table, key) hashtable_add_entry < get_hashtable_underlying_type(table) >(& table, key) +#define hashtable_find(table, key) hashtable_find < get_hashtable_underlying_type(table) >(table, key) +#define hashtable_full(table) hashtable_full < get_hashtable_underlying_type(table) >(table) +#define hashtable_map(table, map_proc) hashtable_map < get_hashtable_underlying_type(table) >(table, map_proc) +#define hashtable_map_mut(table, map_proc) hashtable_map_mut < get_hashtable_underlying_type(table) >(table, map_proc) //! If its desired to roll your own dependencies, define GEN_ROLL_OWN_DEPENDENCIES before including this file. //! Dependencies are derived from the c-zpl library: https://github.com/zpl-c/zpl diff --git a/project/helpers/push_container_defines.inline.hpp b/project/helpers/push_container_defines.inline.hpp index 3ca8d4c..825b456 100644 --- a/project/helpers/push_container_defines.inline.hpp +++ b/project/helpers/push_container_defines.inline.hpp @@ -1,26 +1,19 @@ -#if GEN_SUPPORT_CPP_MEMBER_FEATURES -// These are intended for use in the base library of gencpp and the C-variant of the library -// It provides a interoperability between the C++ and C implementation of arrays. (not letting these do any crazy substiution though) -// They are undefined in gen.hpp and gen.cpp at the end of the files. -// We cpp library expects the user to use the regular calls as they can resolve the type fine. - -#define array_init(type, allocator) array_init (type, allocator ) -#define array_init_reserve(type, allocator, cap) array_init_reserve(type, allocator, cap) -#define array_append_array(type, array, other) array_append (type, & array, other ) -#define array_append_value(type, array, value) array_append (type, & array, value ) -#define array_append_items(type, array, items, item_num) array_append (type, & array, items, item_num ) -#define array_append_at(type, array, item, idx ) array_append_at (type, & array, item, idx ) -#define array_append_at(type, array, items, item_num, idx) array_append_at (type, & items, item_num, idx ) -#define array_back(type, array ) array_back (type, array ) -#define array_clear(type, array ) array_clear (type, array ) -#define array_fill(type, array, begin, end, value) array_fill (type, array, begin, end, value ) -#define array_free(type, array) array_free (type, & array ) -#define arary_grow(type, array, min_capacity) arary_grow (type, & array, min_capacity) -#define array_num(type, array) array_num (type, array ) -#define arary_pop(type, array) arary_pop (type, array ) -#define arary_remove_at(type, array, idx) arary_remove_at (type, idx) -#define arary_reserve(type, array, new_capacity) arary_reserve (type, & array, new_capacity ) -#define arary_resize(type, array, num) arary_resize (type, & array, num) -#define arary_set_capacity(array, new_capacity) arary_set_capacity(type, & array, new_capacity ) -#define arary_get_header(array) arary_get_header (type, array ) -#endif +#define array_init(type, allocator) array_init (allocator ) +#define array_init_reserve(type, allocator, cap) array_init_reserve (allocator, cap) +#define array_append_array(array, other) array_append < get_array_underlying_type(array) > (& array, other ) +#define array_append_value(array, value) array_append < get_array_underlying_type(array) > (& array, value ) +#define array_append_items(array, items, item_num) array_append < get_array_underlying_type(array) > (& array, items, item_num ) +#define array_append_at(array, item, idx ) array_append_at < get_array_underlying_type(array) > (& array, item, idx ) +#define array_append_at_items(array, items, item_num, idx) array_append_at_items< get_array_underlying_type(array) > (& items, item_num, idx ) +#define array_back(array) array_back < get_array_underlying_type(array) > (array ) +#define array_clear(array) array_clear < get_array_underlying_type(array) > (array ) +#define array_fill(array, begin, end, value) array_fill < get_array_underlying_type(array) > (array, begin, end, value ) +#define array_free(array) array_free < get_array_underlying_type(array) > (& array ) +#define arary_grow(array, min_capacity) arary_grow < get_array_underlying_type(array) > (& array, min_capacity) +#define array_num(array) array_num < get_array_underlying_type(array) > (array ) +#define arary_pop(array) arary_pop < get_array_underlying_type(array) > (array ) +#define arary_remove_at(array, idx) arary_remove_at < get_array_underlying_type(array) > (idx) +#define arary_reserve(array, new_capacity) arary_reserve < get_array_underlying_type(array) > (& array, new_capacity ) +#define arary_resize(array, num) arary_resize < get_array_underlying_type(array) > (& array, num) +#define arary_set_capacity(new_capacity) arary_set_capacity < get_array_underlying_type(array) > (& array, new_capacity ) +#define arary_get_header(array) arary_get_header < get_array_underlying_type(array) > (array )