Project
Loading...
Searching...
No Matches
AbstractRefAccessor.h
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
15
16#ifndef O2_POLYACCESSOR_H
17#define O2_POLYACCESSOR_H
18
19#include <array>
20#include <gsl/span>
21
22namespace o2
23{
24namespace dataformats
25{
26
27/*
28 AbstractRefAccessor allows to register multiple containers of objects convertible to type T and
29 to access them by providing a global index indicating the registered source ID and the
30 index within the original container
31*/
32
33template <typename T, int N>
35{
36 public:
38 template <typename C>
39 void registerContainer(const C& cont, int src)
40 {
41 mSizeOfs[src] = sizeof(typename std::remove_reference<decltype(cont)>::type::value_type);
42 mSizes[src] = cont.size();
43 mContainerPtr[src] = reinterpret_cast<const char*>(cont.data());
44 }
45
46 bool isLoaded(int src) const
47 {
48 return mSizeOfs[src] != 0;
49 }
50
52 template <typename U>
53 const U& get_as(int src, int idx) const
54 {
55 return *reinterpret_cast<const U*>(getPtr(src, idx));
56 }
57
59 template <typename U, typename I>
60 const U& get_as(const I globIdx) const
61 {
62 return get_as<U>(globIdx.getSource(), globIdx.getIndex());
63 }
64
66 const T& get(int src, int idx) const
67 {
68 return get_as<T>(src, idx);
69 }
70
72 template <typename I>
73 const T& get(const I globIdx) const
74 {
75 return get_as<T>(globIdx.getSource(), globIdx.getIndex());
76 }
77
79 template <typename U>
80 auto getSpan(int src) const
81 {
82 return getSize(src) ? gsl::span<const U>(reinterpret_cast<const U*>(getPtr(src, 0)), getSize(src)) : gsl::span<const U>();
83 }
84
85 size_t getSize(int src) const
86 {
87 return mSizes[src];
88 }
89
90 private:
91 auto getPtr(int src, int idx) const { return mContainerPtr[src] + mSizeOfs[src] * idx; }
92
93 std::array<size_t, N> mSizeOfs{}; // sizeof for all containers elements
94 std::array<size_t, N> mSizes{}; // size of eack container
95 std::array<const char*, N> mContainerPtr{}; // pointers on attached containers
96};
97
98} // namespace dataformats
99} // namespace o2
100
101#endif
auto getSpan(int src) const
access particula source container as a span
const U & get_as(const I globIdx) const
get object as user provided type from explicitly provided source, index
const T & get(const I globIdx) const
get object from the global index
const T & get(int src, int idx) const
get object from explicitly provided source, index
void registerContainer(const C &cont, int src)
register container in the accessor
const U & get_as(int src, int idx) const
get object as user provided type from explicitly provided source, index
GLenum src
Definition glcorearb.h:1767
a couple of static helper functions to create timestamp values for CCDB queries or override obsolete ...