PODIO v00-16-03
An Event-Data-Model Toolkit for High Energy Physics Experiments
Loading...
Searching...
No Matches
CollectionBuffers.h
Go to the documentation of this file.
1#ifndef PODIO_COLLECTIONBUFFERS_H
2#define PODIO_COLLECTIONBUFFERS_H
3
4#include "podio/ObjectID.h"
5
6#include <functional>
7#include <memory>
8#include <string>
9#include <utility>
10#include <vector>
11
12namespace podio {
13
14class CollectionBase;
15
16template <typename T>
17using UVecPtr = std::unique_ptr<std::vector<T>>;
18
19using CollRefCollection = std::vector<UVecPtr<podio::ObjectID>>;
20using VectorMembersInfo = std::vector<std::pair<std::string, void*>>;
21
22/**
23 * Simple helper struct that bundles all the potentially necessary buffers that
24 * are necessary to represent a collection for I/O purposes.
25 */
27 void* data{nullptr};
30
31 template <typename DataT>
32 std::vector<DataT>* dataAsVector() {
33 return asVector<DataT>(data);
34 }
35
36 template <typename T>
37 static std::vector<T>* asVector(void* raw) {
38 // Are we at a beach? I can almost smell the C...
39 return *static_cast<std::vector<T>**>(raw);
40 }
41};
42
44 void* data{nullptr};
47
48 using CreateFuncT = std::function<std::unique_ptr<podio::CollectionBase>(podio::CollectionReadBuffers, bool)>;
49 using RecastFuncT = std::function<void(CollectionReadBuffers&)>;
50
52 RecastFuncT&& recastFunc) :
53 data(d),
54 references(ref),
55 vectorMembers(vec),
56 createCollection(std::move(createFunc)),
57 recast(std::move(recastFunc)) {
58 }
59
63
65 data(buffers.data), references(buffers.references), vectorMembers(buffers.vectorMembers) {
66 }
67
68 template <typename DataT>
69 std::vector<DataT>* dataAsVector() {
70 return asVector<DataT>(data);
71 }
72
73 template <typename T>
74 static std::vector<T>* asVector(void* raw) {
75 // Are we at a beach? I can almost smell the C...
76 return static_cast<std::vector<T>*>(raw);
77 }
78
80
81 // This is a hacky workaround for the ROOT backend at the moment. There is
82 // probably a better solution, but I haven't found it yet. The problem is the
83 // following:
84 //
85 // When creating a pointer to a vector<T>, either via new or via
86 // TClass::New(), we get a void*, that can be cast back to a vector with
87 //
88 // static_cast<vector<T>*>(raw);
89 //
90 // However, as soon as we pass that same void* to TBranch::SetAddress this no
91 // longer works and the actual cast has to be
92 //
93 // *static_cast<vector<T>**>(raw);
94 //
95 // To make it possible to always use the first form, after we leave the Root
96 // parts of reading, this function is populated in the createBuffers call of each
97 // datatype where we have the necessary type information (from code
98 // generation) to do the second cast and assign the result of that to the data
99 // field again.
101};
102
103} // namespace podio
104
105#endif // PODIO_COLLECTIONBUFFERS_H
std::unique_ptr< std::vector< T > > UVecPtr
std::vector< std::pair< std::string, void * > > VectorMembersInfo
std::vector< UVecPtr< podio::ObjectID > > CollRefCollection
std::function< void(CollectionReadBuffers &)> RecastFuncT
CollectionReadBuffers(CollectionWriteBuffers buffers)
CollectionReadBuffers(const CollectionReadBuffers &)=default
CollectionReadBuffers(void *d, CollRefCollection *ref, VectorMembersInfo *vec, CreateFuncT &&createFunc, RecastFuncT &&recastFunc)
CollRefCollection * references
std::vector< DataT > * dataAsVector()
std::function< std::unique_ptr< podio::CollectionBase >(podio::CollectionReadBuffers, bool)> CreateFuncT
CollectionReadBuffers & operator=(const CollectionReadBuffers &)=default
static std::vector< T > * asVector(void *raw)
VectorMembersInfo * vectorMembers
VectorMembersInfo * vectorMembers
std::vector< DataT > * dataAsVector()
CollRefCollection * references
static std::vector< T > * asVector(void *raw)