|
| 1 | +// Copyright 2025 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 | + |
| 12 | +/// |
| 13 | +/// \file Data.inl |
| 14 | +/// \author Michal Tichak |
| 15 | +/// |
| 16 | + |
| 17 | +#include <optional> |
| 18 | +#include <string_view> |
| 19 | + |
| 20 | +namespace o2::quality_control::core |
| 21 | +{ |
| 22 | + |
| 23 | +template <typename ContainerMap> |
| 24 | +template <typename Result> |
| 25 | +std::optional<std::reference_wrapper<const Result>> DataGeneric<ContainerMap>::get(std::string_view key) |
| 26 | +{ |
| 27 | + if (const auto foundIt = mObjects.find(key); foundIt != mObjects.end()) { |
| 28 | + if (auto* casted = std::any_cast<Result>(&foundIt->second); casted != nullptr) { |
| 29 | + return { *casted }; |
| 30 | + } |
| 31 | + } |
| 32 | + return std::nullopt; |
| 33 | +} |
| 34 | + |
| 35 | +template <typename ContainerMap> |
| 36 | +template <typename T, typename... Args> |
| 37 | +void DataGeneric<ContainerMap>::emplace(std::string_view key, Args&&... args) |
| 38 | +{ |
| 39 | + mObjects.emplace(key, std::any{ std::in_place_type<T>, std::forward<Args>(args)... }); |
| 40 | +} |
| 41 | + |
| 42 | +template <typename ContainerMap> |
| 43 | +template <typename T> |
| 44 | +void DataGeneric<ContainerMap>::insert(std::string_view key, const T& value) |
| 45 | +{ |
| 46 | + mObjects.insert({ std::string{ key }, value }); |
| 47 | +} |
| 48 | + |
| 49 | +namespace internal |
| 50 | +{ |
| 51 | + |
| 52 | +template <typename T> |
| 53 | +static const T* any_cast_try_shared_raw_ptr(const std::any& value) |
| 54 | +{ |
| 55 | + // sadly it is necessary to check for any of these types if we want to test for |
| 56 | + // shared_ptr, raw ptr and a value |
| 57 | + if (auto* casted = std::any_cast<std::shared_ptr<T>>(&value); casted != nullptr) { |
| 58 | + return casted->get(); |
| 59 | + } |
| 60 | + if (auto* casted = std::any_cast<std::shared_ptr<const T>>(&value); casted != nullptr) { |
| 61 | + return casted->get(); |
| 62 | + } |
| 63 | + if (auto* casted = std::any_cast<T*>(&value); casted != nullptr) { |
| 64 | + return *casted; |
| 65 | + } |
| 66 | + if (auto* casted = std::any_cast<const T*>(&value); casted != nullptr) { |
| 67 | + return *casted; |
| 68 | + } |
| 69 | + return std::any_cast<T>(&value); |
| 70 | +} |
| 71 | + |
| 72 | +template <typename T> |
| 73 | +static constexpr auto any_to_specific = std::views::transform([](const auto& pair) -> std::pair<std::string_view, const T*> { return { pair.first, any_cast_try_shared_raw_ptr<T>(pair.second) }; }); |
| 74 | + |
| 75 | +static constexpr auto filter_nullptr_in_pair = std::views::filter([](const auto& pair) { return pair.second != nullptr; }); |
| 76 | + |
| 77 | +static constexpr auto filter_nullptr = std::views::filter([](const auto* ptr) -> bool { return ptr != nullptr; }); |
| 78 | + |
| 79 | +static constexpr auto pair_to_reference = std::views::transform([](const auto& pair) -> const auto& { return *pair.second; }); |
| 80 | + |
| 81 | +static constexpr auto pair_to_value = std::views::transform([](const auto& pair) { return pair.second; }); |
| 82 | + |
| 83 | +static constexpr auto pointer_to_reference = std::views::transform([](const auto* ptr) -> auto& { return *ptr; }); |
| 84 | + |
| 85 | +} // namespace internal |
| 86 | + |
| 87 | +template <typename ContainerMap> |
| 88 | +template <typename T> |
| 89 | +auto DataGeneric<ContainerMap>::iterateByType() const |
| 90 | +{ |
| 91 | + using namespace internal; |
| 92 | + return mObjects | any_to_specific<T> | filter_nullptr_in_pair | pair_to_reference; |
| 93 | +} |
| 94 | + |
| 95 | +template <typename ContainerMap> |
| 96 | +template <typename T, std::predicate<const std::pair<std::string_view, const T*>&> Pred> |
| 97 | +auto DataGeneric<ContainerMap>::iterateByTypeAndFilter(Pred&& filter) const |
| 98 | +{ |
| 99 | + using namespace internal; |
| 100 | + return mObjects | any_to_specific<T> | filter_nullptr_in_pair | std::views::filter(filter) | pair_to_reference; |
| 101 | +} |
| 102 | + |
| 103 | +template <typename ContainerMap> |
| 104 | +template <typename StoredType, typename ResultingType, std::predicate<const std::pair<std::string_view, const StoredType*>&> Pred, invocable_r<const ResultingType*, const StoredType*> Transform> |
| 105 | +auto DataGeneric<ContainerMap>::iterateByTypeFilterAndTransform(Pred&& filter, Transform&& transform) const |
| 106 | +{ |
| 107 | + using namespace internal; |
| 108 | + return mObjects | |
| 109 | + any_to_specific<StoredType> | |
| 110 | + filter_nullptr_in_pair | |
| 111 | + std::views::filter(filter) | |
| 112 | + pair_to_value | |
| 113 | + std::views::transform(transform) | |
| 114 | + filter_nullptr | |
| 115 | + pointer_to_reference; |
| 116 | +} |
| 117 | + |
| 118 | +template <typename ContainerMap> |
| 119 | +size_t DataGeneric<ContainerMap>::size() const noexcept |
| 120 | +{ |
| 121 | + return mObjects.size(); |
| 122 | +} |
| 123 | + |
| 124 | +} // namespace o2::quality_control::core |
0 commit comments