|
| 1 | +// Copyright CERN and copyright holders of ALICE O2. This software is |
| 2 | +// distributed under the terms of the GNU General Public License v3 (GPL |
| 3 | +// Version 3), copied verbatim in the file "COPYING". |
| 4 | +// |
| 5 | +// See http://alice-o2.web.cern.ch/license for full licensing information. |
| 6 | +// |
| 7 | +// In applying this license CERN does not waive the privileges and immunities |
| 8 | +// granted to it by virtue of its status as an Intergovernmental Organization |
| 9 | +// or submit itself to any jurisdiction. |
| 10 | + |
| 11 | +#ifndef FLATTERRESTORE_H |
| 12 | +#define FLATTERRESTORE_H |
| 13 | + |
| 14 | +/// @file FlattenRestore.h |
| 15 | +/// @author Matthias Richter |
| 16 | +/// @since 2020-04-05 |
| 17 | +/// @brief Utilities to copy complex objects to flat buffer and restore |
| 18 | + |
| 19 | +#include <type_traits> |
| 20 | + |
| 21 | +namespace o2::algorithm |
| 22 | +{ |
| 23 | +namespace flatten |
| 24 | +{ |
| 25 | + |
| 26 | +/// Calculate cumulative value size of a variable number of arguments |
| 27 | +/// The function takes parameters by reference and calculates the memory size of |
| 28 | +/// all parameters together. The pointer attribute is removed. |
| 29 | +/// |
| 30 | +/// Example: |
| 31 | +/// char* array1; |
| 32 | +/// int* array2; |
| 33 | +/// float* array3; |
| 34 | +/// size = value_size(array1, array2, array3); |
| 35 | +/// // size is sizeof(char) + sizeof(int) + sizeof(float) |
| 36 | +template <typename ValueType, typename... Args> |
| 37 | +constexpr size_t value_size(ValueType const& value, Args&&... args) |
| 38 | +{ |
| 39 | + size_t size = sizeof(typename std::remove_pointer<typename std::remove_reference<ValueType>::type>::type); |
| 40 | + if constexpr (sizeof...(Args) > 0) { |
| 41 | + size += value_size(std::forward<Args>(args)...); |
| 42 | + } |
| 43 | + return size; |
| 44 | +} |
| 45 | + |
| 46 | +/// Copy the content of variable number of arrays with the same extent to a buffer |
| 47 | +/// The target pointer is passed be reference and incremented while copying |
| 48 | +/// @param wrtptr write pointer |
| 49 | +/// @param count extent of the arrays |
| 50 | +/// @param args a variable number of pointers to arrays |
| 51 | +/// @return copied size in bytes |
| 52 | +template <typename TargetType, typename ValueType, typename... Args> |
| 53 | +static size_t copy_to(TargetType& wrtptr, size_t count, ValueType* array, Args&&... args) |
| 54 | +{ |
| 55 | + static_assert(std::is_pointer<TargetType>::value == true, "need reference to pointer"); |
| 56 | + static_assert(sizeof(typename std::remove_pointer<TargetType>::type) == 1, "need char-like pointer"); |
| 57 | + |
| 58 | + size_t copySize = 0; |
| 59 | + if (array != nullptr) { |
| 60 | + copySize = count * value_size(array); |
| 61 | + memcpy(wrtptr, array, copySize); |
| 62 | + wrtptr += copySize; |
| 63 | + } else if (count > 0) { |
| 64 | + throw std::runtime_error("invalid nullptr to array of " + std::to_string(count) + " element(s)"); |
| 65 | + } |
| 66 | + if constexpr (sizeof...(Args) > 0) { |
| 67 | + copySize += copy_to(wrtptr, count, std::forward<Args>(args)...); |
| 68 | + } |
| 69 | + return copySize; |
| 70 | +} |
| 71 | + |
| 72 | +/// Set pointers to regions in source buffer |
| 73 | +/// A variable number of pointer arguments of consecutive arrays in a buffer are set according |
| 74 | +/// to the type size and the extent of arrays |
| 75 | +/// The read pointer is passed be reference and incremented while copying |
| 76 | +/// @param readptr the source buffer |
| 77 | +/// @param count extent of the arrays |
| 78 | +/// @param args a variable number of references of pointers to arrays |
| 79 | +/// @return handled raw size in bytes |
| 80 | +template <typename BufferType, typename ValueType, typename... Args> |
| 81 | +static size_t set_from(BufferType& readptr, size_t count, ValueType& array, Args&&... args) |
| 82 | +{ |
| 83 | + static_assert(std::is_pointer<typename std::remove_reference<ValueType>::type>::value == true, "need reference to pointer"); |
| 84 | + array = reinterpret_cast<typename std::remove_reference<ValueType>::type>(readptr); |
| 85 | + size_t readSize = count * value_size(array); |
| 86 | + readptr += readSize; |
| 87 | + if constexpr (sizeof...(Args) > 0) { |
| 88 | + readSize += set_from(readptr, count, std::forward<Args>(args)...); |
| 89 | + } |
| 90 | + return readSize; |
| 91 | +} |
| 92 | + |
| 93 | +/// Calculate the total size of a sequence of arrays with the same extent |
| 94 | +/// the first argument is a dummy argument to have the same signature as copy_to |
| 95 | +/// and set_from |
| 96 | +/// @param dummyptr unused |
| 97 | +/// @param count extent of the arrays |
| 98 | +/// @param args a variable number of references of pointers to arrays |
| 99 | +/// @return total size |
| 100 | +template <typename BufferType, typename... Args> |
| 101 | +static size_t calc_size(BufferType const& dummyptr, size_t count, Args&&... args) |
| 102 | +{ |
| 103 | + return count * value_size(std::forward<Args>(args)...); |
| 104 | +} |
| 105 | + |
| 106 | +} // namespace flatten |
| 107 | +} // namespace o2::algorithm |
| 108 | + |
| 109 | +#endif |
0 commit comments