Skip to content

Commit

Permalink
cpp_generator: use std:array for fixed-size arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
no92 committed May 26, 2024
1 parent 5cd3b08 commit 3ee0651
Showing 1 changed file with 25 additions and 18 deletions.
43 changes: 25 additions & 18 deletions bragi/cpp_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ def allocator_argument(self):
return ''
def allocator_parameter(self):
return ''
def array(self):
return 'std::array'
def vector(self):
return 'std::vector'
def string(self):
return 'std::string'
def assert_func(self):
return 'assert'
def includes(self):
return ['<stdint.h>', '<stddef.h>', '<vector>', '<cassert>', '<optional>', '<string>']
return ['<stdint.h>', '<stddef.h>', '<array>', '<vector>', '<cassert>', '<optional>', '<string>']

class FriggTraits:
def needs_allocator(self):
Expand All @@ -24,14 +26,16 @@ def allocator_argument(self):
return 'Allocator allocator = Allocator()'
def allocator_parameter(self):
return 'allocator'
def array(self):
return 'std::array'
def vector(self):
return 'frg::vector'
def string(self):
return 'frg::string'
def assert_func(self):
return 'FRG_ASSERT'
def includes(self):
return ['<stdint.h>', '<stddef.h>', '<frg/vector.hpp>', '<frg/macros.hpp>', '<frg/optional.hpp>', '<frg/string.hpp>']
return ['<stdint.h>', '<stddef.h>', '<array>', '<frg/vector.hpp>', '<frg/macros.hpp>', '<frg/optional.hpp>', '<frg/string.hpp>']

flatten = lambda l: [item for sublist in l for item in sublist]

Expand Down Expand Up @@ -186,6 +190,9 @@ def generate_using(self, using):
return out

def check_needs_allocator(self, t):
if t.identity is TypeIdentity.ARRAY and t.n_elements is not None:
return False

return (t.identity in [TypeIdentity.STRING, TypeIdentity.ARRAY, TypeIdentity.STRUCT]
or t.dynamic) and self.stdlib_traits.needs_allocator()

Expand All @@ -201,7 +208,10 @@ def generate_type(self, t):
elif t.identity is TypeIdentity.STRING:
return f'{self.stdlib_traits.string()}{"<Allocator>" if self.stdlib_traits.needs_allocator() else ""}'
elif t.identity is TypeIdentity.ARRAY:
return f'{self.stdlib_traits.vector()}<{self.generate_type(t.subtype)}{", Allocator" if self.stdlib_traits.needs_allocator() else ""}>'
if t.n_elements is None:
return f'{self.stdlib_traits.vector()}<{self.generate_type(t.subtype)}{", Allocator" if self.stdlib_traits.needs_allocator() else ""}>'
else:
return f'{self.stdlib_traits.array()}<{self.generate_type(t.subtype)}, {t.n_elements}>'
elif t.identity is TypeIdentity.STRUCT:
return f'{t.name}{"<Allocator>" if self.stdlib_traits.needs_allocator() else ""}'
else:
Expand Down Expand Up @@ -562,11 +572,6 @@ def emit_decode_fixed_internal(self, expr, expr_type, array_depth):

out = ''

if self.parent.check_needs_allocator(expr_type.subtype):
out = f'{self.parent.indent}{expr}.resize({expr_type.n_elements}, allocator);\n'
else:
out = f'{self.parent.indent}{expr}.resize({expr_type.n_elements});\n'

out += f'{self.parent.indent}for (size_t i{array_depth} = 0; i{array_depth} < {expr_type.n_elements}; i{array_depth}++) {{\n'
self.parent.enter_indent()
out += self.emit_decode_fixed_internal(f'{expr}[i{array_depth}]', expr_type.subtype, array_depth + 1)
Expand Down Expand Up @@ -642,10 +647,11 @@ def emit_decode_dynamic_internal(self, expr, expr_type, array_depth):
if expr_type.identity is TypeIdentity.ARRAY and expr_type.n_elements:
target_size = expr_type.n_elements

if self.parent.check_needs_allocator(expr_type.subtype):
out += f'{self.parent.indent}{expr}.resize({target_size}, allocator);\n'
else:
out += f'{self.parent.indent}{expr}.resize({target_size});\n'
if not (expr_type.identity is TypeIdentity.ARRAY and expr_type.n_elements is not None):
if self.parent.check_needs_allocator(expr_type.subtype):
out += f'{self.parent.indent}{expr}.resize({target_size}, allocator);\n'
else:
out += f'{self.parent.indent}{expr}.resize({target_size});\n'
out += f'{self.parent.indent}for (size_t i{array_depth} = 0; i{array_depth} < {target_size}; i{array_depth}++)\n'
if target_size != 'size':
self.parent.enter_indent()
Expand Down Expand Up @@ -816,12 +822,13 @@ def emit_accessors(self, members):
self.leave_indent()
out += f'{self.indent}}}\n\n'

out += f'{self.indent}void add_{m.name}({self.generate_type(m.type.subtype)} v) {{\n'
self.enter_indent()
out += f'{self.indent}p_{m.name} = true;\n'
out += f'{self.indent}m_{m.name}.push_back(v);\n'
self.leave_indent()
out += f'{self.indent}}}\n\n'
if m.type.n_elements is None:
out += f'{self.indent}void add_{m.name}({self.generate_type(m.type.subtype)} v) {{\n'
self.enter_indent()
out += f'{self.indent}p_{m.name} = true;\n'
out += f'{self.indent}m_{m.name}.push_back(v);\n'
self.leave_indent()
out += f'{self.indent}}}\n\n'

return out

Expand Down

0 comments on commit 3ee0651

Please sign in to comment.