diff options
| author | Paul Buetow <paul@buetow.org> | 2024-02-22 20:21:38 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2024-02-22 20:21:38 +0200 |
| commit | e800879cd818f51f83c3bfa86a42aa331a58af0c (patch) | |
| tree | 1dbad6420c20369ec4976bd9e8ba3b0429b7e2f2 /internal/generated/nqc.raku | |
| parent | 5e3da322f4deb438f0e7ad1b1245d524d2ec0246 (diff) | |
add sync pool to auto-generated types.go
Diffstat (limited to 'internal/generated/nqc.raku')
| -rw-r--r-- | internal/generated/nqc.raku | 72 |
1 files changed, 48 insertions, 24 deletions
diff --git a/internal/generated/nqc.raku b/internal/generated/nqc.raku index 6cb9e56..578a263 100644 --- a/internal/generated/nqc.raku +++ b/internal/generated/nqc.raku @@ -39,36 +39,42 @@ class NQCToGoActions { } method statement($/) { - make "\n" ~ $<struct>.made ~ "\n"; + make "\n" ~ $<struct>.made; } method constant($/) { push @!const-names: ~$<identifier>; my $const-type = $<identifier>.ends-with('_OP_ID') ?? ' OpId' !! ''; - make 'const ' ~ $<identifier> ~ $const-type ~ " = " ~ $<number> ~ "\n"; + + make qq:to/END/; + const {$<identifier>}$const-type = {$<number>} + END } method !constant-go-string-method returns Str { - return qq:to/END/; - type OpId uint32 - - func (o OpId) String() string \{ - \tswitch (o) \{ - \t{@!const-names.grep(/_OP_ID$/).map({ - "case $_:\n" ~ "\t\treturn \"{$_.subst('_OP_ID', '').lc}\"" - }).join("\n\t")} - default: - \tpanic(fmt.Sprintf("Unknown OpId: %d", o)) - \t\} - \} - END + qq:to/END/; + type OpId uint32 + + func (o OpId) String() string \{ + switch (o) \{ + {@!const-names.grep(/_OP_ID$/).map({ + "case $_: return \"{$_.subst('_OP_ID', '').lc}\"" + }).join('; ')} + default: panic(fmt.Sprintf("Unknown OpId: %d", o)) + \} + \} + END } method struct($/) { - make 'type ' ~ $<identifier>.made ~ " struct \{\n\t" - ~ $<member>.map(*.made).join("\n\t") - ~ "\n\}\n\n" - ~ self!struct-go-string-method($/); + make qq:to/END/; + type {$<identifier>.made} struct \{ + {$<member>.map(*.made).join('; ')} + \} + + {self!struct-go-string-method($/)} + {($<identifier>.made.ends-with('Event') ?? "\n" ~ self!struct-go-sync-pool($/) !! '')} + END } # Generate String() method on the Go struct, for pretty printing. @@ -82,11 +88,29 @@ class NQCToGoActions { ($_.<type> eq 'char' && $_.<arraysize>) ?? "string({$ref}[:])" !! $ref; }); - return qq:to/END/; - func ({$self-ref} {$<identifier>.made}) String() string \{ - return fmt.Sprintf("{@format.join(' ')}", {@args.join(', ')}) - \} - END + qq:to/END/; + func ($self-ref {$<identifier>.made}) String() string \{ + return fmt.Sprintf("{@format.join(' ')}", {@args.join(', ')}) + \} + END + } + + method !struct-go-sync-pool($/) returns Str { + my Str $identifier = $/<identifier>.made; + + qq:to/END/; + var poolOf{$identifier}s = sync.Pool\{ + New: func() interface\{\} \{ return &$identifier\{\} \}, + \} + + func {$identifier}New() *$identifier \{ + return poolOf{$identifier}s.Get().(*$identifier); + \} + + func {$identifier}Recycle(elem *$identifier) \{ + poolOf{$identifier}s.Put(elem) + \} + END } method member($/) { |
