gencpp/test/SOA.hpp

115 lines
2.5 KiB
C++
Raw Normal View History

#pragma once
#ifdef gen_time
#include "gen.hpp"
using namespace gen;
2023-07-13 20:01:20 -07:00
Code gen_SOA( CodeStruct struct_def, s32 num_entries = 0 )
{
StringCached name = get_cached_string( token_fmt( "name", (StrC)struct_def->Name,
stringize( SOA_<name> )
));
Code
soa_entry = { struct_def->duplicate() };
soa_entry->Name = get_cached_string( name(Entry) );
constexpr s32 Num_Vars_Cap = 128;
local_persist Code var_memory[Num_Vars_Cap];
local_persist Arena var_arena;
do_once_start
var_arena = Arena::init_from_memory( var_memory, kilobytes(Num_Vars_Cap) );
do_once_end
2023-07-13 20:01:20 -07:00
Array<CodeVar> vars = Array<CodeVar>::init( var_arena );;
2023-07-13 20:01:20 -07:00
CodeStruct soa = def_struct( name, def_body( ECode::Struct_Body ));
{
2023-07-13 20:01:20 -07:00
for ( Code struct_mem : struct_def.body() )
{
if ( struct_mem->Type == ECode::Variable )
{
2023-07-13 20:01:20 -07:00
CodeType var_type = CodeVar(struct_mem).type();
StrC num_entries_str = to_StrC( str_fmt_buf( "%d", num_entries ) );
2023-07-13 20:01:20 -07:00
CodeVar entry_arr = { nullptr };
if ( ! num_entries)
{
entry_arr = parse_variable( token_fmt( "type", (StrC)var_type->Name, "name", (StrC)struct_mem->Name,
stringize( Array<<type>> <name>; )
));
}
else
{
entry_arr = parse_variable( token_fmt( "type", (StrC)var_type->Name, "name", (StrC)struct_mem->Name, "num", num_entries_str,
stringize( <type> <name>[<num>]; )
));
}
vars.append( entry_arr );
2023-07-13 20:01:20 -07:00
soa.body()->add_entry( entry_arr );
}
}
}
2023-07-13 20:01:20 -07:00
CodeFn make;
{
make = parse_function( token_fmt("SOA_Type", (StrC)name,
stringize(
static
<SOA_Type> make( AllocatorInfo allocator )
{
<SOA_Type> soa = {};
}
)
));
if ( ! num_entries )
{
2023-07-13 20:01:20 -07:00
for ( CodeVar member : vars )
{
Code arr_init = def_execution( token_fmt( "var_name", (StrC)member->Name, "var_type", (StrC)member->entry(0)->Name,
stringize( soa.<var_name> = <var_type>::init( allocator ); )
));
2023-07-13 20:01:20 -07:00
make.body()->add_entry( arr_init );
}
}
2023-07-13 20:01:20 -07:00
make.body()->add_entry( def_execution( code( return soa; ) ));
}
2023-07-13 20:01:20 -07:00
CodeFn get;
{
get = parse_function( code(
Entry get( s32 idx )
{
}
));
String content = String::make( Memory::GlobalAllocator, "return\n{\n" );
2023-07-13 20:01:20 -07:00
for ( CodeVar member : vars )
{
content.append_fmt( token_fmt( "var_name", (StrC)member->Name,
"<var_name>[idx],"
));
}
content.append( "};" );
2023-07-13 20:01:20 -07:00
CodeExec ret = def_execution( content );
2023-07-13 20:01:20 -07:00
get.body()->add_entry( ret );
}
2023-07-13 20:01:20 -07:00
soa.body()->add_entry( make );
soa.body()->add_entry( get );
soa.body()->validate_body();
vars.free();
return soa;
}
#endif