Skip to content

Commit 5597c28

Browse files
committed
data definition moved to inl file
1 parent e61e836 commit 5597c28

2 files changed

Lines changed: 125 additions & 75 deletions

File tree

Framework/include/QualityControl/Data.h

Lines changed: 9 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -39,90 +39,24 @@ class DataGeneric
3939
DataGeneric() = default;
4040

4141
template <typename Result>
42-
std::optional<std::reference_wrapper<const Result>> get(std::string_view key)
43-
{
44-
if (const auto foundIt = mObjects.find(key); foundIt != mObjects.end()) {
45-
if (auto* casted = std::any_cast<Result>(&foundIt->second); casted != nullptr) {
46-
return { *casted };
47-
}
48-
}
49-
return std::nullopt;
50-
}
42+
std::optional<std::reference_wrapper<const Result>> get(std::string_view key);
5143

5244
template <typename T, typename... Args>
53-
void emplace(std::string_view key, Args&&... args)
54-
{
55-
mObjects.emplace(key, std::any{ std::in_place_type<T>, std::forward<Args>(args)... });
56-
}
57-
58-
template <typename T>
59-
void insert(std::string_view key, const T& value)
60-
{
61-
mObjects.insert({ std::string{ key }, value });
62-
}
45+
void emplace(std::string_view key, Args&&... args);
6346

6447
template <typename T>
65-
static const T* any_cast_try_shared_ptr(const std::any& value)
66-
{
67-
// sadly it is necessary to check for any of these types if we want to test for
68-
// shared_ptr, raw ptr and a value
69-
if (auto* casted = std::any_cast<std::shared_ptr<T>>(&value); casted != nullptr) {
70-
return casted->get();
71-
}
72-
if (auto* casted = std::any_cast<std::shared_ptr<const T>>(&value); casted != nullptr) {
73-
return casted->get();
74-
}
75-
if (auto* casted = std::any_cast<T*>(&value); casted != nullptr) {
76-
return *casted;
77-
}
78-
if (auto* casted = std::any_cast<const T*>(&value); casted != nullptr) {
79-
return *casted;
80-
}
81-
return std::any_cast<T>(&value);
82-
}
48+
void insert(std::string_view key, const T& value);
8349

8450
template <typename T>
85-
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_ptr<T>(pair.second) }; });
86-
87-
static constexpr auto filter_nullptr_in_pair = std::views::filter([](const auto& pair) { return pair.second != nullptr; });
88-
89-
static constexpr auto filter_nullptr = std::views::filter([](const auto* ptr) -> bool { return ptr != nullptr; });
90-
91-
static constexpr auto pair_to_reference = std::views::transform([](const auto& pair) -> const auto& { return *pair.second; });
92-
93-
static constexpr auto pair_to_value = std::views::transform([](const auto& pair) { return pair.second; });
94-
95-
static constexpr auto pointer_to_reference = std::views::transform([](const auto* ptr) -> auto& { return *ptr; });
96-
97-
template <typename T>
98-
auto iterateByType() const
99-
{
100-
return mObjects | any_to_specific<T> | filter_nullptr_in_pair | pair_to_reference;
101-
}
51+
auto iterateByType() const;
10252

10353
template <typename T, std::predicate<const std::pair<std::string_view, const T*>&> Pred>
104-
auto iterateByTypeAndFilter(Pred&& filter) const
105-
{
106-
return mObjects | any_to_specific<T> | filter_nullptr_in_pair | std::views::filter(filter) | pair_to_reference;
107-
}
54+
auto iterateByTypeAndFilter(Pred&& filter) const;
10855

10956
template <typename StoredType, typename ResultingType, std::predicate<const std::pair<std::string_view, const StoredType*>&> Pred, invocable_r<const ResultingType*, const StoredType*> Transform>
110-
auto iterateByTypeFilterAndTransform(Pred&& filter, Transform&& transform) const
111-
{
112-
return mObjects |
113-
any_to_specific<StoredType> |
114-
filter_nullptr_in_pair |
115-
std::views::filter(filter) |
116-
pair_to_value |
117-
std::views::transform(transform) |
118-
filter_nullptr |
119-
pointer_to_reference;
120-
}
57+
auto iterateByTypeFilterAndTransform(Pred&& filter, Transform&& transform) const;
12158

122-
size_t size() const noexcept
123-
{
124-
return mObjects.size();
125-
}
59+
size_t size() const noexcept;
12660

12761
private:
12862
ContainerMap mObjects;
@@ -146,8 +80,8 @@ using transparent_unordered_map = std::unordered_map<std::string, std::any, Stri
14680

14781
using Data = DataGeneric<transparent_unordered_map>;
14882

149-
// using Data = DataGeneric<std::map<std::string, std::any, std::less<>>>;
150-
15183
} // namespace o2::quality_control::core
15284

85+
#include "Data.inl"
86+
15387
#endif
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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+
template <typename T>
50+
static const T* any_cast_try_shared_raw_ptr(const std::any& value)
51+
{
52+
// sadly it is necessary to check for any of these types if we want to test for
53+
// shared_ptr, raw ptr and a value
54+
if (auto* casted = std::any_cast<std::shared_ptr<T>>(&value); casted != nullptr) {
55+
return casted->get();
56+
}
57+
if (auto* casted = std::any_cast<std::shared_ptr<const T>>(&value); casted != nullptr) {
58+
return casted->get();
59+
}
60+
if (auto* casted = std::any_cast<T*>(&value); casted != nullptr) {
61+
return *casted;
62+
}
63+
if (auto* casted = std::any_cast<const T*>(&value); casted != nullptr) {
64+
return *casted;
65+
}
66+
return std::any_cast<T>(&value);
67+
}
68+
69+
template <typename T>
70+
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) }; });
71+
72+
static constexpr auto filter_nullptr_in_pair = std::views::filter([](const auto& pair) { return pair.second != nullptr; });
73+
74+
static constexpr auto filter_nullptr = std::views::filter([](const auto* ptr) -> bool { return ptr != nullptr; });
75+
76+
static constexpr auto pair_to_reference = std::views::transform([](const auto& pair) -> const auto& { return *pair.second; });
77+
78+
static constexpr auto pair_to_value = std::views::transform([](const auto& pair) { return pair.second; });
79+
80+
static constexpr auto pointer_to_reference = std::views::transform([](const auto* ptr) -> auto& { return *ptr; });
81+
82+
template <typename ContainerMap>
83+
template <typename T>
84+
auto DataGeneric<ContainerMap>::iterateByType() const
85+
{
86+
return mObjects | any_to_specific<T> | filter_nullptr_in_pair | pair_to_reference;
87+
}
88+
89+
template <typename ContainerMap>
90+
template <typename T, std::predicate<const std::pair<std::string_view, const T*>&> Pred>
91+
auto DataGeneric<ContainerMap>::iterateByTypeAndFilter(Pred&& filter) const
92+
{
93+
return mObjects | any_to_specific<T> | filter_nullptr_in_pair | std::views::filter(filter) | pair_to_reference;
94+
}
95+
96+
template <typename ContainerMap>
97+
template <typename StoredType, typename ResultingType, std::predicate<const std::pair<std::string_view, const StoredType*>&> Pred, invocable_r<const ResultingType*, const StoredType*> Transform>
98+
auto DataGeneric<ContainerMap>::iterateByTypeFilterAndTransform(Pred&& filter, Transform&& transform) const
99+
{
100+
return mObjects |
101+
any_to_specific<StoredType> |
102+
filter_nullptr_in_pair |
103+
std::views::filter(filter) |
104+
pair_to_value |
105+
std::views::transform(transform) |
106+
filter_nullptr |
107+
pointer_to_reference;
108+
}
109+
110+
template <typename ContainerMap>
111+
size_t DataGeneric<ContainerMap>::size() const noexcept
112+
{
113+
return mObjects.size();
114+
}
115+
116+
} // namespace o2::quality_control::core

0 commit comments

Comments
 (0)