forked from AIDASoft/podio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CollectionData.cc.jinja2
223 lines (191 loc) · 7.49 KB
/
CollectionData.cc.jinja2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
{% import "macros/utils.jinja2" as utils %}
{% import "macros/collections.jinja2" as macros %}
// AUTOMATICALLY GENERATED FILE - DO NOT EDIT
#include "{{ incfolder }}{{ class.bare_type }}CollectionData.h"
#include "{{ incfolder }}{{ class.bare_type }}Collection.h"
{% for include in includes_coll_cc %}
{{ include }}
{% endfor %}
{{ utils.namespace_open(class.namespace) }}
{% with class_type = class.bare_type + 'CollectionData' %}
{{ class_type }}::{{ class_type }}() :
{% for relation in OneToManyRelations + OneToOneRelations %}
m_rel_{{ relation.name }}(new std::vector<{{ relation.namespace }}::{{ relation.bare_type }}>()),
{% endfor %}
{%- for member in VectorMembers %}
m_vec_{{ member.name }}(new std::vector<{{ member.full_type }}>()),
{% endfor %}
m_data(new {{ class.bare_type }}DataContainer()) {
{% for relation in OneToManyRelations + OneToOneRelations %}
m_refCollections.emplace_back(std::make_unique<std::vector<podio::ObjectID>>());
{% endfor %}
{% for member in VectorMembers %}
m_vecmem_info.emplace_back("{{ member.full_type }}", &m_vec_{{ member.name }});
{% endfor %}
}
{{ class_type }}::{{ class_type }}(podio::CollectionReadBuffers buffers, bool isSubsetColl) :
{% for relation in OneToManyRelations + OneToOneRelations %}
m_rel_{{ relation.name }}(new std::vector<{{ relation.namespace }}::{{ relation.bare_type }}>()),
{% endfor %}
m_refCollections(std::move(*buffers.references)),
m_vecmem_info(std::move(*buffers.vectorMembers)) {
// For subset collections we are done, for proper collections we still have to
// populate the data and vector members
if (!isSubsetColl) {
m_data.reset(buffers.dataAsVector<{{ class.full_type }}Data>());
{% for member in VectorMembers %}
m_vec_{{ member.name }}.reset(podio::CollectionReadBuffers::asVector<{{ member.full_type }}>(m_vecmem_info[{{ loop.index0 }}].second));
{% endfor %}
}
// Cleanup these to avoid leaking them
delete buffers.references;
delete buffers.vectorMembers;
}
void {{ class_type }}::clear(bool isSubsetColl) {
if (isSubsetColl) {
// We don't own the objects so no cleanup to do here
entries.clear();
// Clear the ObjectID I/O buffer
for (auto& pointer : m_refCollections) { pointer->clear(); }
return;
}
// Normal collections manage a bit more and have to clean up a bit more
if (m_data) m_data->clear();
{% if OneToManyRelations or OneToOneRelations %}
for (auto& pointer : m_refCollections) { pointer->clear(); }
{% endif %}
{% for relation in OneToManyRelations %}
// clear relations to {{ relation.name }}. Make sure to unlink() the reference data as they may be gone already.
for (auto& pointer : m_rel_{{ relation.name }}_tmp) {
for (auto& item : *pointer) {
item.unlink();
}
}
m_rel_{{ relation.name }}_tmp.clear();
{{ macros.clear_relation(relation) }}
{% endfor %}
{% for relation in OneToOneRelations %}
{{ macros.clear_relation(relation) }}
{% endfor %}
{% for member in VectorMembers %}
if (m_vec_{{ member.name }}) m_vec_{{ member.name }}->clear();
m_vecs_{{ member.name }}.clear();
{% endfor %}
for (auto& obj : entries) { delete obj; }
entries.clear();
}
podio::CollectionWriteBuffers {{ class_type }}::getCollectionBuffers(bool isSubsetColl) {
{% if VectorMembers %}
if (!isSubsetColl) {
// Make sure these point to the right place, even if a collection has been
// moved since it has been created
{% for member in VectorMembers %}
m_vecmem_info[{{ loop.index0 }}].second = &m_vec_{{ member.name }};
{% endfor %}
}
{% endif -%}
return {
isSubsetColl ? nullptr : (void*)&m_data,
isSubsetColl ? nullptr : (void*)m_data.get(),
&m_refCollections, // only need to store the ObjectIDs of the referenced objects
&m_vecmem_info
};
}
void {{ class_type }}::prepareForWrite(bool isSubsetColl) {
for (auto& pointer : m_refCollections) { pointer->clear(); }
// If this is a subset collection use the relation storing mechanism to
// store the ObjectIDs of all referenced objects and nothing else
if (isSubsetColl) {
for (const auto* obj : entries) {
m_refCollections[0]->emplace_back(obj->id);
}
return;
}
// Normal collections have to store the data and all the relations
m_data->reserve(entries.size());
for (auto& obj : entries) { m_data->push_back(obj->data); }
{% for relation in OneToManyRelations %}
int {{ relation.name }}_index = 0;
{% endfor %}
{% for member in VectorMembers %}
const auto {{ member.name }}_size = std::accumulate(entries.begin(), entries.end(), 0,
[](size_t sum, const {{ class.bare_type }}Obj* obj) { return sum + obj->m_{{ member.name }}->size(); });
m_vec_{{ member.name }}->reserve({{ member.name }}_size);
int {{ member.name }}_index = 0;
{% endfor %}
{%- if OneToManyRelations or VectorMembers %}
for (size_t i = 0, size = m_data->size(); i != size; ++i) {
{% for relation in OneToManyRelations %}
{{ macros.prepare_for_write_multi_relation(relation, loop.index0) }}
{% endfor %}
{% for member in VectorMembers %}
{{ macros.prepare_for_write_vector_member(member) }}
{% endfor %}
}
{%- endif %}
{% for relation in OneToOneRelations %}
{{ macros.prepare_for_write_single_relation(relation, loop.index0, OneToManyRelations | length) }}
{% endfor %}
}
void {{ class_type }}::prepareAfterRead(uint32_t collectionID) {
int index = 0;
for (auto& data : *m_data) {
auto obj = new {{ class.bare_type }}Obj({index, collectionID}, data);
{% for relation in OneToManyRelations %}
obj->m_{{ relation.name }} = m_rel_{{ relation.name }}.get();
{% endfor %}
{% for member in VectorMembers %}
obj->m_{{ member.name }} = m_vec_{{ member.name }}.get();
{% endfor %}
entries.emplace_back(obj);
++index;
}
// at this point we could clear the I/O data buffer, but we keep them intact
// because then we can save a call to prepareForWrite
}
{% if OneToManyRelations or VectorMembers %}
void {{ class_type }}::createRelations({{ class.bare_type }}Obj* obj) {
{% for relation in OneToManyRelations %}
// We take ownership of these here
m_rel_{{ relation.name }}_tmp.emplace_back(obj->m_{{ relation.name }});
{% endfor %}
{% for member in VectorMembers %}
// We take ownership of these here
m_vecs_{{ member.name }}.emplace_back(obj->m_{{ member.name }});
{% endfor %}
}
{% endif %}
bool {{ class_type }}::setReferences(const podio::ICollectionProvider* collectionProvider, bool isSubsetColl) {
if (isSubsetColl) {
for (const auto& id : *m_refCollections[0]) {
{{ macros.get_obj_ptr(class.full_type) }}
entries.push_back(obj);
}
return true; // TODO: check success, how?
}
// Normal collections have to resolve all relations
{% for relation in OneToManyRelations %}
{{ macros.set_references_multi_relation(relation, loop.index0) }}
{% endfor %}
{% for relation in OneToOneRelations %}
{{ macros.set_reference_single_relation(relation, loop.index0, OneToManyRelations | length) }}
{% endfor %}
return true; // TODO: check success, how?
}
void {{ class_type }}::makeSubsetCollection() {
// Subset collections do not need all the data buffers that normal
// collections need, so we can free them here
m_vecmem_info.clear();
m_data.reset(nullptr);
{% for relation in OneToManyRelations + OneToOneRelations %}
m_rel_{{ relation.name }}.reset(nullptr);
{% endfor %}
{% for member in VectorMembers %}
m_vec_{{ member.name }}.reset(nullptr);
{% endfor %}
// Subset collections need one vector of ObjectIDs for I/O purposes.
m_refCollections.resize(1);
m_refCollections[0] = std::make_unique<std::vector<podio::ObjectID>>();
}
{% endwith %}
{{ utils.namespace_close(class.namespace) }}