-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathCollection.h.jinja2
More file actions
279 lines (225 loc) · 9.54 KB
/
Copy pathCollection.h.jinja2
File metadata and controls
279 lines (225 loc) · 9.54 KB
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
{% import "macros/utils.jinja2" as utils %}
{% import "macros/workarounds.jinja2" as workarounds %}
{% from "macros/iterator.jinja2" import iterator_declaration %}
// AUTOMATICALLY GENERATED FILE - DO NOT EDIT
#ifndef {{ package_name.upper() }}_{{ class.bare_type }}Collection_H
#define {{ package_name.upper() }}_{{ class.bare_type }}Collection_H
// datamodel specific includes
#include "{{ incfolder }}{{ class.bare_type }}.h"
#include "{{ incfolder }}Mutable{{ class.bare_type }}.h"
#include "{{ incfolder }}{{ class.bare_type }}Obj.h"
#include "{{ incfolder }}{{ class.bare_type }}CollectionData.h"
// podio specific includes
#include "podio/ICollectionProvider.h"
#include "podio/CollectionBase.h"
#include "podio/detail/Pythonizations.h"
#include "podio/utilities/TypeHelpers.h"
#if defined(PODIO_JSON_OUTPUT) && !defined(__CLING__)
#include "nlohmann/json_fwd.hpp"
#endif
#include <string_view>
#include <vector>
#include <algorithm>
#include <ostream>
#include <mutex>
#include <memory>
#include <cstddef>
namespace podio {
struct RelationNames;
}
{{ utils.namespace_open(class.namespace) }}
{{ iterator_declaration(class) }}
{{ iterator_declaration(class, prefix='Mutable') }}
/**
A Collection is identified by an ID.
*/
class {{ class.bare_type }}Collection : public podio::CollectionBase {
public:
using value_type = {{ class.bare_type }};
using mutable_type = Mutable{{ class.bare_type }};
using const_iterator = {{ class.bare_type }}CollectionIterator;
using iterator = {{ class.bare_type }}MutableCollectionIterator;
using difference_type = ptrdiff_t;
using size_type = size_t;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
using reverse_iterator = std::reverse_iterator<iterator>;
{{ class.bare_type }}Collection() = default;
{{ class.bare_type }}Collection({{ class.bare_type }}CollectionData&& data, bool isSubsetColl);
// This is a move-only type
{{ class.bare_type }}Collection(const {{ class.bare_type}}Collection& ) = delete;
{{ class.bare_type }}Collection& operator=(const {{ class.bare_type}}Collection& ) = delete;
{{ class.bare_type }}Collection({{ class.bare_type }}Collection&&) = default;
{{ class.bare_type }}Collection& operator=({{ class.bare_type }}Collection&&) = default;
~{{ class.bare_type }}Collection() override;
#if defined(__cpp_lib_containers_ranges)
template<podio::detail::RangeConvertibleTo<value_type> R>
{{ class.bare_type }}Collection(std::from_range_t, R&& range);
#endif
/// Construct a {{ class.bare_type }} collection from a range of mutable or
/// immutable handles
template<podio::detail::RangeConvertibleTo<value_type> R>
static {{ class.bare_type }}Collection from(R&& range);
constexpr static std::string_view typeName = "{{ (class | string ).strip(':') + "Collection" }}";
constexpr static std::string_view valueTypeName = "{{ (class | string ).strip(':') }}";
constexpr static std::string_view dataTypeName = "{{ ( class | string ).strip(':') }}Data";
void clear() final;
/// Cppyy protocol to setup the pythonizations for this class. Not to be called directly.
static void __cppyy_pythonize__(PyObject* klass, const std::string& name){
podio::detail::pythonizations::pythonize_subscript(klass, name);
}
/// Print this collection to the passed stream
void print(std::ostream& os=std::cout, bool flush=true) const final;
/// Append a new object to the collection, and return this object.
Mutable{{ class.bare_type }} create();
/// Append a new object to the collection, and return this object.
/// Initialized with the parameters given
template<typename... Args>
Mutable{{ class.bare_type }} create(Args&&... args);
/// number of elements in the collection
std::size_t size() const final;
/// maximal number of elements in the collection
std::size_t max_size() const final;
/// Is the collection empty
bool empty() const final;
/// fully qualified type name
const std::string_view getTypeName() const final { return typeName; }
/// fully qualified type name of elements - with namespace
const std::string_view getValueTypeName() const final { return valueTypeName; }
/// fully qualified type name of stored POD elements - with namespace
const std::string_view getDataTypeName() const final { return dataTypeName; }
/// schema version
podio::SchemaVersionT getSchemaVersion() const final;
bool isSubsetCollection() const final {
return m_isSubsetColl;
}
void setSubsetCollection(bool setSubset=true) final;
/// Returns the const object of given index
{{ class.bare_type }} operator[](std::size_t index) const;
/// Returns the object of a given index
Mutable{{ class.bare_type }} operator[](std::size_t index);
/// Returns the const object of given index
{{ class.bare_type }} at(std::size_t index) const;
/// Returns the object of given index
Mutable{{ class.bare_type }} at(std::size_t index);
/// Append object to the collection
void push_back(const Mutable{{class.bare_type}}& object);
/// Append an object to the (subset) collection
void push_back(const {{ class.bare_type }}& object);
void prepareForWrite() const final;
void prepareAfterRead() final;
bool setReferences(const podio::ICollectionProvider* collectionProvider) final;
/// Get the collection buffers for this collection
podio::CollectionWriteBuffers getBuffers() final;
void setID(uint32_t ID) final {
m_collectionID = ID;
if (!m_isSubsetColl) {
std::for_each(m_storage.entries.begin(), m_storage.entries.end(),
[ID] ({{ class.bare_type }}Obj* obj) { obj->id = {obj->id.index, static_cast<uint32_t>(ID)}; }
);
}
}
uint32_t getID() const final {
return m_collectionID;
}
/// check if the collection has a valid ID
bool hasID() const final {
return getID() != static_cast<uint32_t>(podio::ObjectID::untracked) &&
getID() != static_cast<uint32_t>(podio::ObjectID::invalid);
}
size_t getDatamodelRegistryIndex() const final;
// support for the iterator protocol
iterator begin() {
return iterator(0, &m_storage.entries);
}
const_iterator begin() const {
return const_iterator(0, &m_storage.entries);
}
const_iterator cbegin() const {
return begin();
}
iterator end() {
return iterator(m_storage.entries.size(), &m_storage.entries);
}
const_iterator end() const {
return const_iterator(m_storage.entries.size(), &m_storage.entries);
}
const_iterator cend() const {
return end();
}
// reverse iterators
reverse_iterator rbegin() {
return reverse_iterator(end());
}
const_reverse_iterator rbegin() const {
return const_reverse_iterator(end());
}
const_reverse_iterator crbegin() const {
return rbegin();
}
reverse_iterator rend() {
return reverse_iterator(begin());
}
const_reverse_iterator rend() const {
return const_reverse_iterator(begin());
}
const_reverse_iterator crend() const {
return rend();
}
{% for member in Members %}
std::vector<{{ member.full_type }}> {{ member.name }}(const size_t nElem = 0) const;
{% endfor %}
private:
// For setReferences, we need to give our own CollectionData access to our
// private entries. Otherwise we would need to expose a public member function
// that gives access to the Obj* which is definitely not what we want
friend class {{ class.bare_type }}CollectionData;
mutable bool m_isPrepared{false};
bool m_isSubsetColl{false};
uint32_t m_collectionID{static_cast<uint32_t>(podio::ObjectID::untracked)};
mutable std::unique_ptr<std::mutex> m_storageMtx{std::make_unique<std::mutex>()};
mutable {{ class.bare_type }}CollectionData m_storage{};
};
std::ostream& operator<<(std::ostream& o, const {{ class.bare_type }}Collection& v);
template<typename... Args>
Mutable{{ class.bare_type }} {{ class.bare_type }}Collection::create(Args&&... args) {
if (m_isSubsetColl) {
throw std::logic_error("Cannot create new elements on a subset collection");
}
auto obj = new {{ class.bare_type }}Obj({static_cast<int>(m_storage.entries.size()), m_collectionID}, {std::forward<Args>(args)...});
m_storage.entries.push_back(obj);
{% if OneToManyRelations or VectorMembers %}
// Need to initialize the relation vectors manually for the {ObjectID, {{class.bare_type}}Data} constructor
{% for relation in OneToManyRelations + VectorMembers %}
obj->m_{{ relation.name }} = new std::vector<{{ relation.full_type }}>();
{% endfor %}
m_storage.createRelations(obj);
{% endif %}
return Mutable{{ class.bare_type }}(podio::utils::MaybeSharedPtr(obj));
}
#if defined(__cpp_lib_containers_ranges)
template<podio::detail::RangeConvertibleTo<{{ class.bare_type }}Collection::value_type> R>
{{ class.bare_type}}Collection::{{ class.bare_type }}Collection(std::from_range_t, R&& range)
: {{ class.bare_type }}Collection() {
setSubsetCollection(podio::detail::RangeOf<R, value_type>);
for (auto&& elem : range) {
push_back(std::forward<decltype(elem)>(elem));
}
}
#endif
template<podio::detail::RangeConvertibleTo<{{ class.bare_type }}Collection::value_type> R>
{{ class.bare_type }}Collection {{ class.bare_type }}Collection::from(R&& range) {
{{ class.bare_type }}Collection coll;
if constexpr (podio::detail::RangeOf<R, value_type>) {
coll.setSubsetCollection();
}
for (auto&& elem : range) {
coll.push_back(std::forward<decltype(elem)>(elem));
}
return coll;
}
#if defined(PODIO_JSON_OUTPUT) && !defined(__CLING__)
void to_json(nlohmann::json& j, const {{ class.bare_type }}Collection& collection);
#endif
{{ utils.namespace_close(class.namespace) }}
{{ workarounds.ld_library_path(class, "Collection", ["valueTypeName", "dataTypeName"]) }}
#endif