Skip to content

Commit fcbc75c

Browse files
committed
fix: cpp_generator._build_type_info -> generator_utils.qualified_for_modules
1 parent 88c80f1 commit fcbc75c

File tree

3 files changed

+42
-61
lines changed

3 files changed

+42
-61
lines changed

python/podio_gen/cpp_generator.py

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -622,22 +622,6 @@ def _write_all_collections_header(self):
622622
),
623623
)
624624

625-
@staticmethod
626-
def _build_type_info(full_name):
627-
"""Extract namespace and bare type from a full type name.
628-
629-
Args:
630-
full_name: Fully qualified type name (e.g., 'nsp::EnergyInNamespace' or 'ExampleHit')
631-
632-
Returns:
633-
Dictionary with 'full', 'ns', and 'bare' keys
634-
"""
635-
if "::" in full_name:
636-
ns, bare = full_name.rsplit("::", 1)
637-
else:
638-
ns, bare = "", full_name
639-
return {"full": full_name, "ns": ns, "bare": bare}
640-
641625
def _write_datamodel_module(self):
642626
"""Write a C++20 module interface file that exports all datamodel types"""
643627
if not self.enable_modules:
@@ -647,10 +631,10 @@ def _write_datamodel_module(self):
647631
print(f"Generating C++20 module interface for {self.package_name}")
648632

649633
# Prepare structured type information with namespace details
650-
datatypes = [self._build_type_info(name) for name in self.datamodel.datatypes.keys()]
651-
components = [self._build_type_info(name) for name in self.datamodel.components.keys()]
652-
interfaces = [self._build_type_info(name) for name in self.datamodel.interfaces.keys()]
653-
links = [self._build_type_info(name) for name in self.datamodel.links.keys()]
634+
datatypes = [DataType(name) for name in self.datamodel.datatypes.keys()]
635+
components = [DataType(name) for name in self.datamodel.components.keys()]
636+
interfaces = [DataType(name) for name in self.datamodel.interfaces.keys()]
637+
links = [DataType(name) for name in self.datamodel.links.keys()]
654638

655639
context = {
656640
"package_name": self.package_name,

python/podio_gen/generator_utils.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,28 @@ def __str__(self):
147147
def __repr__(self):
148148
return f"DataType: {self.__str__()}"
149149

150+
def qualified_for_modules(self, prefix="", suffix=""):
151+
"""Return the qualified name for C++ using declarations in modules.
152+
153+
Args:
154+
prefix: Optional prefix to add before the type name (e.g., "Mutable")
155+
suffix: Optional suffix to add after the type name (e.g., "Collection")
156+
157+
Returns:
158+
For namespaced types: 'namespace::PrefixTypeSuffix' (no leading ::)
159+
For global types: '::PrefixTypeSuffix' (with leading ::)
160+
161+
Examples:
162+
DataType("ExampleHit").qualified_for_modules() # Returns "::ExampleHit"
163+
DataType("ExampleHit").qualified_for_modules(prefix="Mutable") # Returns "::MutableExampleHit"
164+
DataType("ex42::Type").qualified_for_modules(suffix="Collection") # Returns "ex42::TypeCollection"
165+
"""
166+
type_name = f"{prefix}{self.bare_type}{suffix}"
167+
if self.namespace:
168+
return f"{self.namespace}::{type_name}"
169+
else:
170+
return f"::{type_name}"
171+
150172
def _to_json(self):
151173
"""Return a string representation that can be parsed again"""
152174
return self.full_type

python/templates/datamodel_module.ixx.jinja2

Lines changed: 16 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -33,27 +33,24 @@ module;
3333

3434
// Include all component headers
3535
{% for comp in components %}
36-
#include "{{ incfolder }}{{ comp.bare }}.h"
36+
#include "{{ incfolder }}{{ comp.bare_type }}.h"
3737
{% endfor %}
3838

3939
// Include all datatype headers (Object, MutableObject, Collection, etc.)
4040
{% for dt in datatypes %}
41-
#include "{{ incfolder }}{{ dt.bare }}.h"
42-
#include "{{ incfolder }}Mutable{{ dt.bare }}.h"
43-
#include "{{ incfolder }}{{ dt.bare }}Obj.h"
44-
#include "{{ incfolder }}{{ dt.bare }}Data.h"
45-
#include "{{ incfolder }}{{ dt.bare }}Collection.h"
46-
#include "{{ incfolder }}{{ dt.bare }}CollectionData.h"
41+
#include "{{ incfolder }}{{ dt.bare_type }}.h"
42+
#include "{{ incfolder }}Mutable{{ dt.bare_type }}.h"
43+
#include "{{ incfolder }}{{ dt.bare_type }}Collection.h"
4744
{% endfor %}
4845

4946
// Include all interface headers
5047
{% for iface in interfaces %}
51-
#include "{{ incfolder }}{{ iface.bare }}.h"
48+
#include "{{ incfolder }}{{ iface.bare_type }}.h"
5249
{% endfor %}
5350

5451
// Include all link headers
5552
{% for link in links %}
56-
#include "{{ incfolder }}{{ link.bare }}Collection.h"
53+
#include "{{ incfolder }}{{ link.bare_type }}Collection.h"
5754
{% endfor %}
5855

5956
export module {{ package_name }}.datamodel;
@@ -70,48 +67,30 @@ export namespace podio {
7067
{% if components %}
7168
export namespace {{ package_name }} {
7269
{% for comp in components %}
73-
{% if comp.ns %}
74-
using {{ comp.ns }}::{{ comp.bare }};
75-
{% else %}
76-
using ::{{ comp.bare }};
77-
{% endif %}
70+
using {{ comp.qualified_for_modules() }};
7871
{% endfor %}
7972
}
8073
{% endif %}
8174

8275
// Export all datatypes (Object, MutableObject, Collection)
8376
{% for dt in datatypes %}
8477
export namespace {{ package_name }} {
85-
// {{ dt.full }} datatype
86-
{% if dt.ns %}
87-
using {{ dt.ns }}::{{ dt.bare }};
88-
using {{ dt.ns }}::Mutable{{ dt.bare }};
89-
using {{ dt.ns }}::{{ dt.bare }}Collection;
90-
using {{ dt.ns }}::{{ dt.bare }}CollectionIterator;
91-
using {{ dt.ns }}::{{ dt.bare }}MutableCollectionIterator; // Note: pattern is {Type}MutableCollectionIterator
92-
// Note: {{ dt.bare }}Obj, {{ dt.bare }}Data, and {{ dt.bare }}CollectionData are internal implementation
93-
// details and are not exported. They are only accessible through the public handle and collection APIs.
94-
{% else %}
95-
using ::{{ dt.bare }};
96-
using ::Mutable{{ dt.bare }};
97-
using ::{{ dt.bare }}Collection;
98-
using ::{{ dt.bare }}CollectionIterator;
99-
using ::{{ dt.bare }}MutableCollectionIterator; // Note: pattern is {Type}MutableCollectionIterator
100-
// Note: {{ dt.bare }}Obj, {{ dt.bare }}Data, and {{ dt.bare }}CollectionData are internal implementation
78+
// {{ dt.full_type }} datatype
79+
using {{ dt.qualified_for_modules() }};
80+
using {{ dt.qualified_for_modules(prefix="Mutable") }};
81+
using {{ dt.qualified_for_modules(suffix="Collection") }};
82+
using {{ dt.qualified_for_modules(suffix="CollectionIterator") }};
83+
using {{ dt.qualified_for_modules(suffix="MutableCollectionIterator") }}; // Note: pattern is {Type}MutableCollectionIterator
84+
// Note: {{ dt.bare_type }}Obj, {{ dt.bare_type }}Data, and {{ dt.bare_type }}CollectionData are internal implementation
10185
// details and are not exported. They are only accessible through the public handle and collection APIs.
102-
{% endif %}
10386
}
10487
{% endfor %}
10588

10689
// Export all interfaces
10790
{% if interfaces %}
10891
export namespace {{ package_name }} {
10992
{% for iface in interfaces %}
110-
{% if iface.ns %}
111-
using {{ iface.ns }}::{{ iface.bare }};
112-
{% else %}
113-
using ::{{ iface.bare }};
114-
{% endif %}
93+
using {{ iface.qualified_for_modules() }};
11594
{% endfor %}
11695
}
11796
{% endif %}
@@ -120,11 +99,7 @@ export namespace {{ package_name }} {
12099
{% if links %}
121100
export namespace {{ package_name }} {
122101
{% for link in links %}
123-
{% if link.ns %}
124-
using {{ link.ns }}::{{ link.bare }}Collection;
125-
{% else %}
126-
using ::{{ link.bare }}Collection;
127-
{% endif %}
102+
using {{ link.qualified_for_modules(suffix="Collection") }};
128103
{% endfor %}
129104
}
130105
{% endif %}

0 commit comments

Comments
 (0)