Skip to content

Commit

Permalink
Make member getters return builtin types by value
Browse files Browse the repository at this point in the history
  • Loading branch information
tmadlener committed Nov 24, 2023
1 parent 32ef1b2 commit 03b3ee4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
19 changes: 19 additions & 0 deletions python/podio_gen/generator_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,25 @@ def getter_name(self, get_syntax):
return self.name
return _prefix_name(self.name, "get")

def getter_return_type(self, for_array=False):
"""Get the return type for a getter function for a variable
All types that are builtin will be returned by value, the rest will be
returned as const&
Args:
for_array (bool, optional): Whether the type should be for an indexed
array access
"""
if for_array:
if self.is_builtin_array:
return self.array_type
return f"const {self.array_type}&"
if self.is_builtin:
return self.full_type
# everything else will just be by const referene
return f"const {self.full_type}&"

def setter_name(self, get_syntax, is_relation=False):
"""Get the setter name of the variable"""
if is_relation:
Expand Down
6 changes: 3 additions & 3 deletions python/templates/macros/declarations.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
{% macro member_getters(members, get_syntax) %}
{%for member in members %}
/// Access the {{ member.docstring }}
const {{ member.full_type }}& {{ member.getter_name(get_syntax) }}() const;
{{ member.getter_return_type() }} {{ member.getter_name(get_syntax) }}() const;
{% if member.is_array %}
/// Access item i of the {{ member.docstring }}
const {{ member.array_type }}& {{ member.getter_name(get_syntax) }}(size_t i) const;
{{ member.getter_return_type(True) }} {{ member.getter_name(get_syntax) }}(size_t i) const;
{%- endif %}
{% if member.sub_members %}
{% for sub_member in member.sub_members %}
/// Access the member of {{ member.docstring }}
const {{ sub_member.full_type }}& {{ sub_member.getter_name(get_sytnax) }}() const;
{{ sub_member.getter_return_type() }} {{ sub_member.getter_name(get_sytnax) }}() const;
{% endfor %}
{% endif %}

Expand Down
6 changes: 3 additions & 3 deletions python/templates/macros/implementations.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ Mutable{{ type }} {{ full_type }}::clone() const {
{% macro member_getters(class, members, get_syntax, prefix='') %}
{% set class_type = prefix + class.bare_type %}
{% for member in members %}
const {{ member.full_type }}& {{ class_type }}::{{ member.getter_name(get_syntax) }}() const { return m_obj->data.{{ member.name }}; }
{{ member.getter_return_type() }} {{ class_type }}::{{ member.getter_name(get_syntax) }}() const { return m_obj->data.{{ member.name }}; }
{% if member.is_array %}
const {{ member.array_type }}& {{ class_type }}::{{ member.getter_name(get_syntax) }}(size_t i) const { return m_obj->data.{{ member.name }}.at(i); }
{{ member.getter_return_type(True) }} {{ class_type }}::{{ member.getter_name(get_syntax) }}(size_t i) const { return m_obj->data.{{ member.name }}.at(i); }
{% endif %}
{% if member.sub_members %}
{% for sub_member in member.sub_members %}
const {{ sub_member.full_type }}& {{ class_type }}::{{ sub_member.getter_name(get_syntax) }}() const { return m_obj->data.{{ member.name }}.{{ sub_member.name }}; }
{{ sub_member.getter_return_type() }} {{ class_type }}::{{ sub_member.getter_name(get_syntax) }}() const { return m_obj->data.{{ member.name }}.{{ sub_member.name }}; }
{% endfor %}
{%- endif %}
{% endfor %}
Expand Down

0 comments on commit 03b3ee4

Please sign in to comment.