2023-06-30 12:11:49 -07:00
|
|
|
#pragma once
|
|
|
|
|
2023-07-18 21:13:12 -07:00
|
|
|
#if GEN_TIME
|
2023-06-30 12:11:49 -07:00
|
|
|
#include "gen.hpp"
|
2023-07-11 22:33:11 -07:00
|
|
|
#include "Buffer.Upfront.hpp"
|
2023-06-30 12:11:49 -07:00
|
|
|
|
|
|
|
using namespace gen;
|
|
|
|
|
2023-07-11 22:33:11 -07:00
|
|
|
Code gen__ring( StrC type )
|
2023-06-30 12:11:49 -07:00
|
|
|
{
|
2023-07-15 13:13:44 -07:00
|
|
|
static CodeType t_allocator_info = def_type( name(AllocatorInfo) );
|
2023-06-30 12:11:49 -07:00
|
|
|
|
|
|
|
String name;
|
|
|
|
{
|
|
|
|
char const* name_str = str_fmt_buf( "Ring_%s\0", type.Ptr );
|
|
|
|
s32 name_len = str_len( name_str );
|
|
|
|
|
|
|
|
name = get_cached_string({ name_len, name_str });
|
|
|
|
};
|
|
|
|
|
2023-07-15 13:13:44 -07:00
|
|
|
CodeType t_ring_type = def_type( name );
|
|
|
|
CodeType t_ring_type_ptr = def_type( name, __, spec_ptr );
|
2023-06-30 12:11:49 -07:00
|
|
|
|
2023-07-15 13:13:44 -07:00
|
|
|
CodeType t_type = def_type( type );
|
|
|
|
CodeType t_type_ptr = def_type( type, __, spec_ptr );
|
|
|
|
CodeType t_type_ref = def_type( type, __, spec_ref );
|
2023-06-30 12:11:49 -07:00
|
|
|
|
2023-07-15 13:13:44 -07:00
|
|
|
CodeType t_buffer_type;
|
2023-06-30 12:11:49 -07:00
|
|
|
{
|
|
|
|
char const* name_str = str_fmt_buf( "Buffer_%s\0", type.Ptr );
|
|
|
|
s32 len = str_len( name_str );
|
|
|
|
|
|
|
|
t_buffer_type = def_type( { len, name_str } );
|
|
|
|
}
|
|
|
|
|
2023-07-15 13:13:44 -07:00
|
|
|
CodeStruct ring = {0};
|
2023-06-30 12:11:49 -07:00
|
|
|
{
|
2023-07-15 13:13:44 -07:00
|
|
|
CodeUsing using_type = def_using( name(Type), t_type );
|
2023-06-30 21:23:40 -07:00
|
|
|
|
2023-07-15 13:13:44 -07:00
|
|
|
CodeVar backing = def_variable( t_allocator_info, name(Backing) );
|
|
|
|
CodeVar capacity = def_variable( t_uw, name(Capacity) );
|
|
|
|
CodeVar head = def_variable( t_uw, name(Head) );
|
|
|
|
CodeVar tail = def_variable( t_uw, name(Tail) );
|
|
|
|
CodeVar buffer = def_variable( t_buffer_type, name(Buffer) );
|
2023-06-30 12:11:49 -07:00
|
|
|
|
2023-07-15 13:13:44 -07:00
|
|
|
CodeFn init;
|
2023-06-30 12:11:49 -07:00
|
|
|
{
|
2023-07-15 13:13:44 -07:00
|
|
|
CodeParam params = def_params( args(
|
2023-07-12 12:59:47 -07:00
|
|
|
def_param( t_allocator_info, name(allocator) )
|
2023-06-30 12:11:49 -07:00
|
|
|
, def_param( t_uw, name(max_size) )
|
2023-07-12 00:41:16 -07:00
|
|
|
));
|
2023-06-30 12:11:49 -07:00
|
|
|
|
2023-07-11 22:33:11 -07:00
|
|
|
char const* tmpl = stringize(
|
2023-06-30 12:11:49 -07:00
|
|
|
<type> result = { 0 };
|
|
|
|
|
|
|
|
result.Backing = allocator;
|
|
|
|
|
|
|
|
result.Buffer = Buffer_<data_type>::init( allocator, max_size + 1 );
|
|
|
|
|
|
|
|
if ( result.Buffer == nullptr )
|
|
|
|
return { nullptr };
|
|
|
|
|
|
|
|
result.Capacity = max_size + 1;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
);
|
2023-07-11 22:33:11 -07:00
|
|
|
Code body = def_execution( token_fmt( "type", (StrC)name, "data_type", type, tmpl ));
|
2023-06-30 12:11:49 -07:00
|
|
|
|
|
|
|
init = def_function( name(init), params, t_ring_type, body, spec_static_member );
|
|
|
|
}
|
|
|
|
|
2023-07-15 13:13:44 -07:00
|
|
|
CodeFn append = def_function( name(append), def_param( t_type, name(value)), t_void
|
2023-07-08 09:21:26 -07:00
|
|
|
, def_execution( code(
|
2023-06-30 21:23:40 -07:00
|
|
|
Buffer[ Head ] = value;
|
|
|
|
Head = ( Head + 1 ) % Capacity;
|
|
|
|
|
|
|
|
if ( Head == Tail )
|
|
|
|
Tail = ( Tail + 1 ) % Capacity;
|
|
|
|
))
|
|
|
|
);
|
|
|
|
|
2023-07-15 13:13:44 -07:00
|
|
|
CodeFn appendv;
|
2023-06-30 21:23:40 -07:00
|
|
|
{
|
2023-07-15 13:13:44 -07:00
|
|
|
CodeParam params = def_params( 2
|
2023-06-30 21:23:40 -07:00
|
|
|
, def_param( t_type_ptr, name(values))
|
|
|
|
, def_param( t_sw, name(num))
|
|
|
|
);
|
|
|
|
|
2023-07-08 09:21:26 -07:00
|
|
|
Code body = def_execution( code(
|
2023-06-30 21:23:40 -07:00
|
|
|
for ( sw idx = 0; idx < num; idx++ )
|
|
|
|
append( values[ idx ] );
|
|
|
|
));
|
|
|
|
|
|
|
|
appendv = def_function( name(append), params, t_void, body, spec_inline );
|
|
|
|
}
|
|
|
|
|
2023-07-15 13:13:44 -07:00
|
|
|
CodeFn empty = def_function( name(empty), __, t_bool
|
2023-06-30 21:23:40 -07:00
|
|
|
, def_execution( code(
|
|
|
|
return Head == Tail;
|
|
|
|
))
|
|
|
|
);
|
|
|
|
|
2023-07-15 13:13:44 -07:00
|
|
|
CodeFn free = def_function( name(free), __, t_void
|
2023-06-30 21:23:40 -07:00
|
|
|
, def_execution( code(
|
|
|
|
Buffer.free();
|
|
|
|
))
|
|
|
|
);
|
|
|
|
|
2023-07-15 13:13:44 -07:00
|
|
|
CodeFn full = def_function( name(full), __, t_bool
|
2023-06-30 21:23:40 -07:00
|
|
|
, def_execution( code(
|
|
|
|
return (Head + 1) % Capacity == Tail;
|
|
|
|
))
|
|
|
|
);
|
|
|
|
|
2023-07-15 13:13:44 -07:00
|
|
|
CodeFn get = def_function( name(get), __, t_type_ref
|
2023-06-30 21:23:40 -07:00
|
|
|
, def_execution( code(
|
|
|
|
Type& data = Buffer[ Tail ];
|
|
|
|
Tail = ( Tail + 1 ) % Capacity;
|
|
|
|
|
|
|
|
return data;
|
|
|
|
))
|
|
|
|
);
|
|
|
|
|
2023-07-15 13:13:44 -07:00
|
|
|
CodeFn wipe = def_function( name(wipe), __, t_void
|
2023-06-30 21:23:40 -07:00
|
|
|
, def_execution( code(
|
|
|
|
Head = 0;
|
|
|
|
Tail = 0;
|
|
|
|
Buffer.wipe();
|
|
|
|
))
|
|
|
|
);
|
|
|
|
|
2023-07-12 00:41:16 -07:00
|
|
|
ring = def_struct( name, def_struct_body( args(
|
2023-06-30 21:23:40 -07:00
|
|
|
using_type,
|
|
|
|
|
2023-06-30 12:11:49 -07:00
|
|
|
init,
|
|
|
|
|
2023-06-30 21:23:40 -07:00
|
|
|
append,
|
|
|
|
appendv,
|
|
|
|
empty,
|
|
|
|
free,
|
|
|
|
full,
|
|
|
|
get,
|
|
|
|
wipe,
|
|
|
|
|
2023-06-30 12:11:49 -07:00
|
|
|
backing,
|
|
|
|
capacity,
|
|
|
|
head,
|
|
|
|
tail,
|
|
|
|
buffer
|
2023-07-12 00:41:16 -07:00
|
|
|
)));
|
2023-06-30 12:11:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return ring;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct GenRingRequest
|
|
|
|
{
|
|
|
|
StrC Dependency;
|
|
|
|
StrC Type;
|
|
|
|
};
|
2023-07-11 15:29:45 -07:00
|
|
|
Array<GenRingRequest> GenRingRequests;
|
2023-06-30 12:11:49 -07:00
|
|
|
|
2023-07-11 22:33:11 -07:00
|
|
|
void gen__ring_request( StrC type, StrC dep = {} )
|
2023-06-30 12:11:49 -07:00
|
|
|
{
|
|
|
|
do_once_start
|
2023-07-29 13:00:06 -07:00
|
|
|
GenRingRequests = Array<GenRingRequest>::init( GlobalAllocator );
|
2023-06-30 12:11:49 -07:00
|
|
|
do_once_end
|
|
|
|
|
|
|
|
// Make sure we don't already have a request for the type.
|
2023-07-11 15:29:45 -07:00
|
|
|
for ( sw idx = 0; idx < GenRingRequests.num(); ++idx )
|
2023-06-30 12:11:49 -07:00
|
|
|
{
|
|
|
|
StrC const reqest_type = GenRingRequests[ idx ].Type;
|
|
|
|
|
|
|
|
if ( reqest_type.Len != type.Len )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if ( str_compare( reqest_type.Ptr, type.Ptr, reqest_type.Len ) == 0 )
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-06-30 21:23:40 -07:00
|
|
|
// Ring definition depends on a array and buffer definition.
|
2023-07-11 22:33:11 -07:00
|
|
|
gen__buffer_request( type, dep );
|
2023-06-30 12:11:49 -07:00
|
|
|
|
2023-07-11 22:33:11 -07:00
|
|
|
GenRingRequest request = { dep, type };
|
2023-07-11 15:29:45 -07:00
|
|
|
GenRingRequests.append( request );
|
2023-06-30 12:11:49 -07:00
|
|
|
}
|
2023-07-11 22:33:11 -07:00
|
|
|
#define gen_ring( type ) gen__ring_request( code(type) )
|
2023-06-30 12:11:49 -07:00
|
|
|
|
|
|
|
u32 gen_ring_file()
|
|
|
|
{
|
|
|
|
Builder
|
|
|
|
gen_ring_file;
|
2023-07-11 22:33:11 -07:00
|
|
|
gen_ring_file.open( "ring.Upfront.gen.hpp" );
|
|
|
|
|
2023-07-15 19:27:38 -07:00
|
|
|
gen_ring_file.print( def_include( txt_StrC("gen.hpp")) );
|
2023-07-11 22:33:11 -07:00
|
|
|
gen_ring_file.print( def_include( txt_StrC("buffer.Upfront.gen.hpp")) );
|
2023-06-30 12:11:49 -07:00
|
|
|
|
2023-07-11 22:33:11 -07:00
|
|
|
gen_ring_file.print( def_using_namespace( name(gen)));
|
2023-06-30 12:11:49 -07:00
|
|
|
|
|
|
|
GenRingRequest* current = GenRingRequests;
|
2023-07-11 15:29:45 -07:00
|
|
|
s32 left = GenRingRequests.num();
|
2023-06-30 12:11:49 -07:00
|
|
|
while (left--)
|
|
|
|
{
|
|
|
|
GenRingRequest const& request = * current;
|
|
|
|
|
2023-07-11 22:33:11 -07:00
|
|
|
Code generated_ring = gen__ring( current->Type );
|
2023-06-30 12:11:49 -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 } );
|
|
|
|
Code include = def_include( request.Dependency );
|
|
|
|
|
|
|
|
gen_ring_file.print( cmt );
|
|
|
|
gen_ring_file.print( include );
|
|
|
|
}
|
|
|
|
|
|
|
|
gen_ring_file.print( generated_ring );
|
|
|
|
current++;
|
|
|
|
}
|
|
|
|
|
|
|
|
gen_ring_file.write();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-07-18 21:13:12 -07:00
|
|
|
#endif // GEN_TIME
|