From 3ee065107e9dc734df1f02e90022c951bbb7f3c8 Mon Sep 17 00:00:00 2001 From: no92 Date: Sun, 26 May 2024 15:51:05 +0200 Subject: [PATCH] cpp_generator: use std:array for fixed-size arrays --- bragi/cpp_generator.py | 43 ++++++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/bragi/cpp_generator.py b/bragi/cpp_generator.py index 0cb30cb..85b9004 100644 --- a/bragi/cpp_generator.py +++ b/bragi/cpp_generator.py @@ -8,6 +8,8 @@ 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): @@ -15,7 +17,7 @@ def string(self): def assert_func(self): return 'assert' def includes(self): - return ['', '', '', '', '', ''] + return ['', '', '', '', '', '', ''] class FriggTraits: def needs_allocator(self): @@ -24,6 +26,8 @@ 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): @@ -31,7 +35,7 @@ def string(self): def assert_func(self): return 'FRG_ASSERT' def includes(self): - return ['', '', '', '', '', ''] + return ['', '', '', '', '', '', ''] flatten = lambda l: [item for sublist in l for item in sublist] @@ -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() @@ -201,7 +208,10 @@ def generate_type(self, t): elif t.identity is TypeIdentity.STRING: return f'{self.stdlib_traits.string()}{"" 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}{"" if self.stdlib_traits.needs_allocator() else ""}' else: @@ -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) @@ -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() @@ -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