2023-05-06 12:49:43 -07:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#if gen_time
|
|
|
|
#include "gen.hpp"
|
|
|
|
|
|
|
|
using namespace gen;
|
|
|
|
|
|
|
|
Code gen__array_base()
|
|
|
|
{
|
2023-07-08 11:11:41 -07:00
|
|
|
return parse_global_body( code(
|
|
|
|
struct ArrayHeader
|
2023-05-06 12:49:43 -07:00
|
|
|
{
|
2023-07-08 11:11:41 -07:00
|
|
|
AllocatorInfo Allocator;
|
|
|
|
uw Capacity;
|
|
|
|
uw Num;
|
2023-05-06 12:49:43 -07:00
|
|
|
};
|
|
|
|
|
2023-07-08 11:11:41 -07:00
|
|
|
static inline uw array_grow_formula( uw value )
|
|
|
|
{
|
|
|
|
return 2 * value * 8;
|
|
|
|
}
|
|
|
|
));
|
2023-05-06 12:49:43 -07:00
|
|
|
}
|
|
|
|
|
2023-07-08 11:11:41 -07:00
|
|
|
Code gen__array( StrC type, sw type_size )
|
2023-05-06 12:49:43 -07:00
|
|
|
{
|
2023-07-08 11:11:41 -07:00
|
|
|
StrC name;
|
|
|
|
{
|
|
|
|
char const* name_str = str_fmt_buf( "Array_%s\0", type.Ptr );
|
|
|
|
s32 name_len = str_len( name_str );
|
2023-05-06 12:49:43 -07:00
|
|
|
|
2023-07-08 11:11:41 -07:00
|
|
|
name = { name_len, name_str };
|
|
|
|
};
|
2023-05-06 12:49:43 -07:00
|
|
|
|
2023-07-08 11:11:41 -07:00
|
|
|
Code array = parse_struct( token_fmt(
|
|
|
|
txt(
|
|
|
|
struct <ArrayType>
|
2023-05-06 12:49:43 -07:00
|
|
|
{
|
2023-07-08 11:11:41 -07:00
|
|
|
using Header = ArrayHeader;
|
|
|
|
using Type = <type>;
|
2023-05-06 12:49:43 -07:00
|
|
|
|
2023-07-08 11:11:41 -07:00
|
|
|
constexpr auto grow_formula = &array_glow_formula;
|
2023-05-06 12:49:43 -07:00
|
|
|
|
2023-07-08 11:11:41 -07:00
|
|
|
static
|
|
|
|
<ArrayType> init( AllocatorInfo allocator )
|
|
|
|
{
|
|
|
|
return init_reserve( allocator, grow_formula(0) );
|
|
|
|
}
|
2023-05-06 12:49:43 -07:00
|
|
|
|
2023-07-08 11:11:41 -07:00
|
|
|
static
|
|
|
|
<ArrayType> init_reserve( AllocatorInfo allocator, sw capacity )
|
|
|
|
{
|
|
|
|
Header* header = rcast( Header*, alloc( allocator, sizeof(Header) + sizeof(Type) ));
|
2023-05-06 12:49:43 -07:00
|
|
|
|
2023-07-08 11:11:41 -07:00
|
|
|
if ( header == nullptr )
|
|
|
|
return { nullptr };
|
2023-05-06 12:49:43 -07:00
|
|
|
|
2023-07-08 11:11:41 -07:00
|
|
|
header->Allocator = allocator;
|
|
|
|
header->Capacity = capacity;
|
|
|
|
header->Num = 0;
|
2023-05-06 12:49:43 -07:00
|
|
|
|
2023-07-08 11:11:41 -07:00
|
|
|
return { rcast( Type*, header + 1) };
|
|
|
|
}
|
2023-05-06 12:49:43 -07:00
|
|
|
|
2023-07-08 11:11:41 -07:00
|
|
|
bool append( Type value )
|
2023-05-06 12:49:43 -07:00
|
|
|
{
|
2023-07-08 11:11:41 -07:00
|
|
|
Header& header = get_header();
|
2023-05-06 12:49:43 -07:00
|
|
|
|
2023-07-08 11:11:41 -07:00
|
|
|
if ( header.Num == header.Capacity )
|
|
|
|
{
|
|
|
|
if ( ! grow( header.Capacity ))
|
|
|
|
return false;
|
|
|
|
}
|
2023-05-06 12:49:43 -07:00
|
|
|
|
2023-07-08 11:11:41 -07:00
|
|
|
Data[ header.Num ] = value;
|
|
|
|
header.Num++;
|
2023-05-06 12:49:43 -07:00
|
|
|
|
2023-07-08 11:11:41 -07:00
|
|
|
return true;
|
|
|
|
}
|
2023-05-06 12:49:43 -07:00
|
|
|
|
2023-07-08 11:11:41 -07:00
|
|
|
inline Type& back( void )
|
|
|
|
{
|
|
|
|
Header& header = get_header();
|
|
|
|
return Data[ header.Num - 1 ];
|
|
|
|
}
|
2023-05-06 15:35:11 -07:00
|
|
|
|
2023-07-08 11:11:41 -07:00
|
|
|
inline void clear( void )
|
|
|
|
{
|
|
|
|
Header& header = get_header();
|
|
|
|
header.Num = 0;
|
|
|
|
}
|
2023-05-06 12:49:43 -07:00
|
|
|
|
2023-07-08 11:11:41 -07:00
|
|
|
bool fill( uw begin, uw end, Type value )
|
|
|
|
{
|
|
|
|
Header& header = get_header();
|
2023-05-06 15:35:11 -07:00
|
|
|
|
2023-07-08 11:11:41 -07:00
|
|
|
if ( begin < 0 || end >= header.Num )
|
|
|
|
return false;
|
2023-05-06 15:35:11 -07:00
|
|
|
|
2023-07-08 11:11:41 -07:00
|
|
|
for ( sw idx = begin; idx < end; idx++ )
|
|
|
|
{
|
|
|
|
Data[ idx ] = value;
|
|
|
|
}
|
2023-05-06 12:49:43 -07:00
|
|
|
|
2023-07-08 11:11:41 -07:00
|
|
|
return true;
|
|
|
|
}
|
2023-05-06 12:49:43 -07:00
|
|
|
|
2023-07-08 11:11:41 -07:00
|
|
|
inline void free( void )
|
|
|
|
{
|
|
|
|
Header& header = get_header();
|
|
|
|
zpl::free( header.Allocator, &header );
|
|
|
|
}
|
2023-05-06 15:35:11 -07:00
|
|
|
|
2023-07-08 11:11:41 -07:00
|
|
|
inline Header& get_header( void )
|
|
|
|
{
|
|
|
|
return *( reinterpret_cast< Header* >( Data ) - 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
bool grow( uw min_capacity )
|
|
|
|
{
|
|
|
|
Header& header = get_header();
|
|
|
|
uw new_capacity = grow_formula( header.Capacity );
|
2023-05-06 15:35:11 -07:00
|
|
|
|
2023-07-08 11:11:41 -07:00
|
|
|
if ( new_capacity < min_capacity )
|
|
|
|
new_capacity = 8;
|
2023-05-06 15:35:11 -07:00
|
|
|
|
2023-07-08 11:11:41 -07:00
|
|
|
return set_capacity( new_capacity );
|
|
|
|
}
|
2023-05-06 15:35:11 -07:00
|
|
|
|
2023-07-08 11:11:41 -07:00
|
|
|
inline uw num( void )
|
|
|
|
{
|
|
|
|
return get_header().Num;
|
|
|
|
}
|
2023-05-06 15:35:11 -07:00
|
|
|
|
2023-07-08 11:11:41 -07:00
|
|
|
inline bool pop( void )
|
|
|
|
{
|
|
|
|
Header& header = get_header();
|
2023-05-06 15:35:11 -07:00
|
|
|
|
2023-07-08 11:11:41 -07:00
|
|
|
ZPL_ASSERT( header.Num > 0 );
|
|
|
|
header.Num--;
|
|
|
|
}
|
2023-05-06 15:35:11 -07:00
|
|
|
|
2023-07-08 11:11:41 -07:00
|
|
|
inline void remove_at( uw idx )
|
|
|
|
{
|
|
|
|
Header* header = &get_header();
|
|
|
|
ZPL_ASSERT( idx < header->Num );
|
2023-05-06 15:35:11 -07:00
|
|
|
|
2023-07-08 11:11:41 -07:00
|
|
|
mem_move( header + idx, header + idx + 1, sizeof( Type ) * ( header->Num - idx - 1 ) );
|
|
|
|
header->Num--;
|
|
|
|
}
|
2023-05-06 15:35:11 -07:00
|
|
|
|
2023-07-08 11:11:41 -07:00
|
|
|
bool reserve( uw new_capacity )
|
|
|
|
{
|
|
|
|
Header& header = get_header();
|
2023-05-06 15:35:11 -07:00
|
|
|
|
2023-07-08 11:11:41 -07:00
|
|
|
if ( header.Capacity < new_capacity )
|
|
|
|
return set_capacity( new_capacity );
|
2023-05-06 15:35:11 -07:00
|
|
|
|
2023-07-08 11:11:41 -07:00
|
|
|
return true;
|
|
|
|
}
|
2023-05-06 15:35:11 -07:00
|
|
|
|
2023-07-08 11:11:41 -07:00
|
|
|
bool resize( uw num )
|
|
|
|
{
|
|
|
|
Header& header = get_header();
|
2023-05-06 12:49:43 -07:00
|
|
|
|
2023-07-08 11:11:41 -07:00
|
|
|
if ( num > header.Capacity )
|
|
|
|
{
|
|
|
|
if ( ! grow( header.Capacity ) )
|
|
|
|
return false;
|
|
|
|
}
|
2023-05-06 15:35:11 -07:00
|
|
|
|
2023-07-08 11:11:41 -07:00
|
|
|
header.Num = num;
|
2023-05-06 15:35:11 -07:00
|
|
|
return true;
|
2023-07-08 11:11:41 -07:00
|
|
|
}
|
2023-05-06 15:35:11 -07:00
|
|
|
|
2023-07-08 11:11:41 -07:00
|
|
|
bool set_capacity( uw new_capacity )
|
|
|
|
{
|
|
|
|
Header& header = get_header();
|
2023-05-06 12:49:43 -07:00
|
|
|
|
2023-07-08 11:11:41 -07:00
|
|
|
if ( new_capacity == header.Capacity )
|
|
|
|
return true;
|
2023-05-06 12:49:43 -07:00
|
|
|
|
2023-07-08 11:11:41 -07:00
|
|
|
if ( new_capacity < header.Num )
|
|
|
|
header.Num = new_capacity;
|
2023-05-06 15:35:11 -07:00
|
|
|
|
2023-07-08 11:11:41 -07:00
|
|
|
sw size = sizeof( Header ) + sizeof( Type ) * new_capacity;
|
|
|
|
Header* new_header = reinterpret_cast< Header* >( alloc( header.Allocator, size ) );
|
2023-05-06 15:35:11 -07:00
|
|
|
|
2023-07-08 11:11:41 -07:00
|
|
|
if ( new_header == nullptr )
|
|
|
|
return false;
|
2023-05-06 12:49:43 -07:00
|
|
|
|
2023-07-08 11:11:41 -07:00
|
|
|
mem_move( new_header, &header, sizeof( Header ) + sizeof( Type ) * header.Num );
|
2023-05-06 12:49:43 -07:00
|
|
|
|
2023-07-08 11:11:41 -07:00
|
|
|
new_header->Allocator = header.Allocator;
|
|
|
|
new_header->Num = header.Num;
|
|
|
|
new_header->Capacity = new_capacity;
|
2023-05-06 15:35:11 -07:00
|
|
|
|
2023-07-08 11:11:41 -07:00
|
|
|
zpl::free( header.Allocator, &header );
|
|
|
|
|
|
|
|
Data = ( Type* )new_header + 1;
|
|
|
|
return true;
|
|
|
|
}
|
2023-05-06 12:49:43 -07:00
|
|
|
|
2023-07-08 11:11:41 -07:00
|
|
|
Type* Data;
|
|
|
|
};
|
|
|
|
),
|
|
|
|
// Tokens
|
|
|
|
2
|
|
|
|
, "ArrayType", name
|
|
|
|
, "type", type
|
|
|
|
));
|
2023-05-06 12:49:43 -07:00
|
|
|
|
2023-07-08 11:11:41 -07:00
|
|
|
Code op_ptr = untyped_str( code(
|
|
|
|
operator Type*()
|
|
|
|
{
|
|
|
|
return Data;
|
|
|
|
}
|
|
|
|
));
|
|
|
|
array.body()->add_entry( op_ptr );
|
|
|
|
|
|
|
|
return array;
|
2023-05-06 12:49:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct GenArrayRequest
|
|
|
|
{
|
2023-07-08 11:11:41 -07:00
|
|
|
StrC Dependency;
|
|
|
|
StrC Type;
|
|
|
|
sw Size;
|
2023-05-06 12:49:43 -07:00
|
|
|
};
|
|
|
|
Array(GenArrayRequest) GenArrayRequests;
|
|
|
|
|
2023-07-08 11:11:41 -07:00
|
|
|
void gen__array_request( StrC type, sw size, StrC dep = {} )
|
2023-05-06 12:49:43 -07:00
|
|
|
{
|
2023-07-08 11:11:41 -07:00
|
|
|
do_once_start
|
|
|
|
array_init( GenArrayRequests, g_allocator );
|
|
|
|
do_once_end
|
|
|
|
|
|
|
|
// Make sure we don't already have a request for the type.
|
|
|
|
for ( sw idx = 0; idx < array_count( GenArrayRequests ); ++idx )
|
|
|
|
{
|
|
|
|
StrC const reqest_type = GenArrayRequests[ idx ].Type;
|
|
|
|
|
|
|
|
if ( reqest_type.Len != type.Len )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if ( str_compare( reqest_type.Ptr, type.Ptr, reqest_type.Len ) == 0 )
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
GenArrayRequest request = { dep, type, size };
|
2023-05-06 12:49:43 -07:00
|
|
|
array_append( GenArrayRequests, request );
|
|
|
|
}
|
2023-07-08 11:11:41 -07:00
|
|
|
#define gen_array( type ) gen__array_request( { txt_n_len(type) }, sizeof(type) )
|
2023-05-06 12:49:43 -07:00
|
|
|
|
|
|
|
u32 gen_array_file()
|
|
|
|
{
|
|
|
|
Builder
|
|
|
|
gen_array_file;
|
|
|
|
gen_array_file.open( "array.gen.hpp" );
|
|
|
|
|
2023-07-08 11:11:41 -07:00
|
|
|
Code include_zpl = def_include( StrC::from("Bloat.hpp") );
|
|
|
|
gen_array_file.print( include_zpl );
|
|
|
|
|
|
|
|
Code array_base = gen__array_base();
|
|
|
|
gen_array_file.print( array_base );
|
2023-05-06 12:49:43 -07:00
|
|
|
|
|
|
|
GenArrayRequest* current = GenArrayRequests;
|
|
|
|
s32 left = array_count( GenArrayRequests );
|
|
|
|
while (left--)
|
|
|
|
{
|
|
|
|
GenArrayRequest const& request = * current;
|
|
|
|
|
2023-07-08 11:11:41 -07:00
|
|
|
Code generated_array = gen__array( request.Type, request.Size );
|
2023-05-06 12:49:43 -07:00
|
|
|
|
|
|
|
if ( request.Dependency )
|
|
|
|
{
|
|
|
|
char const* cmt_str = str_fmt_buf( "// Dependency for %s type", request.Type );
|
|
|
|
s32 cmt_len = str_len( cmt_str );
|
|
|
|
|
|
|
|
Code cmt = def_comment( { cmt_len, cmt_str } );
|
2023-07-08 11:11:41 -07:00
|
|
|
Code include = def_include( request.Dependency );
|
2023-05-06 12:49:43 -07:00
|
|
|
|
|
|
|
gen_array_file.print( cmt );
|
|
|
|
gen_array_file.print( include );
|
|
|
|
}
|
|
|
|
|
|
|
|
gen_array_file.print( generated_array );
|
|
|
|
current++;
|
|
|
|
}
|
|
|
|
|
|
|
|
gen_array_file.write();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|