12
12
13
13
namespace Jug ::Algo::detail {
14
14
15
+ enum class DataMode : unsigned { kInput , kOutput };
16
+
15
17
// Generate properties for each of the data arguments
16
- template <class T , bool kIsInput > class DataElement {
18
+ template <class T , DataMode kMode > class DataElement {
19
+
17
20
public:
18
- using value_type = std::conditional_t <algorithms::is_input_v<T> , algorithms::input_type_t <T>,
21
+ using value_type = std::conditional_t <kMode == DataMode:: kInput , algorithms::input_type_t <T>,
19
22
algorithms::output_type_t <T>>;
20
23
using data_type = algorithms::data_type_t <T>;
24
+ constexpr static const bool kIsOptional = algorithms::is_optional_v<T>;
21
25
22
- DataElement (gsl::not_null<GaudiAlgorithm*> owner, std::string_view name)
23
- : m_owner{owner}, m_data_name(m_owner, name, " " ) {}
26
+ template <class Owner >
27
+ DataElement (Owner* owner, std::string_view name)
28
+ : m_data_name{std::make_unique<Gaudi::Property<std::string>>(owner, std::string (name), " " )}
29
+ , m_owner{owner} {}
24
30
void init () {
25
31
if (m_handle) {
26
32
// treat error: already initialized
27
33
}
28
- if (!m_data_name. empty ()) {
34
+ if (!m_data_name-> empty ()) {
29
35
m_handle = std::make_unique<DataHandle<data_type>>(
30
- m_data_name, (kIsInput ? Gaudi::DataHandle::Reader : Gaudi::DataHandle::Writer), m_owner);
36
+ *m_data_name,
37
+ ((kMode == DataMode::kInput ) ? Gaudi::DataHandle::Reader : Gaudi::DataHandle::Writer),
38
+ m_owner);
31
39
} else if (!algorithms::is_optional_v<T>) {
32
40
// treat error: member not optional but no collection name given
33
41
}
34
42
}
35
43
value_type get () const {
36
- if (!m_handle) {
37
- return nullptr ;
44
+ if constexpr (kIsOptional ) {
45
+ if (!m_handle) {
46
+ return nullptr ;
47
+ }
38
48
}
39
- if constexpr (kIsInput ) {
49
+ if constexpr (kMode == DataMode:: kInput ) {
40
50
return m_handle->get ();
41
51
} else {
42
52
return m_handle->createAndPut ();
43
53
}
44
54
}
45
55
46
56
private:
47
- GaudiAlgorithm* m_owner;
48
- Gaudi::Property<std::string> m_data_name;
49
- std::unique_ptr<DataHandle<T>> m_handle;
57
+ std::unique_ptr<Gaudi::Property<std::string>>
58
+ m_data_name; // This needs to be a pointer, else things go wrong once we go through
59
+ // createElements - probably something about passing the Property through an
60
+ // rvalue (or copy) constructor
61
+ std::unique_ptr<DataHandle<data_type>> m_handle;
62
+ gsl::not_null<Gaudi::Algorithm*> m_owner;
50
63
};
51
64
52
65
// Specialization for vectors
53
- template <class T , class A , bool kIsInput > class DataElement <std::vector<T, A>, kIsInput > {
66
+ template <class T , class A , DataMode kMode > class DataElement <std::vector<T, A>, kMode > {
54
67
public:
55
- using value_type = std::conditional_t <algorithms::is_input_v<T> , algorithms::input_type_t <T>,
68
+ using value_type = std::conditional_t <kMode == DataMode:: kInput , algorithms::input_type_t <T>,
56
69
algorithms::output_type_t <T>>;
57
70
using data_type = algorithms::data_type_t <T>;
58
71
59
- DataElement (gsl::not_null<GaudiAlgorithm*> owner, std::string_view name)
60
- : m_owner{owner}, m_data_names(m_owner, name, " " ) {}
72
+ template <class Owner >
73
+ DataElement (Owner* owner, std::string_view name)
74
+ : m_data_names{std::make_unique<Gaudi::Property<std::vector<std::string>>>(
75
+ owner, std::string (name), {})}
76
+ , m_owner{owner} {}
61
77
void init () {
62
78
if (!m_handles.empty ()) {
63
79
// treat error: already initialized
64
80
}
65
- if (!m_data_names. empty ()) {
66
- for (const auto & name : m_data_names) {
81
+ if (!m_data_names-> empty ()) {
82
+ for (const auto & name : * m_data_names) {
67
83
if (!name.empty ()) {
68
84
m_handles.emplace_back (std::make_unique<DataHandle<data_type>>(
69
- name, (kIsInput ? Gaudi::DataHandle::Reader : Gaudi::DataHandle::Writer), m_owner));
85
+ name,
86
+ (kMode == DataMode::kInput ? Gaudi::DataHandle::Reader : Gaudi::DataHandle::Writer),
87
+ m_owner));
70
88
} else {
71
89
// treat error: empty name
72
90
}
@@ -78,7 +96,7 @@ template <class T, class A, bool kIsInput> class DataElement<std::vector<T, A>,
78
96
std::vector<value_type> get () const {
79
97
std::vector<value_type> ret;
80
98
for (auto & handle : m_handles) {
81
- if constexpr (kIsInput ) {
99
+ if constexpr (kMode == DataMode:: kInput ) {
82
100
ret.emplace_back (handle->get ());
83
101
} else {
84
102
ret.emplace_back (handle->createAndPut ());
@@ -88,16 +106,15 @@ template <class T, class A, bool kIsInput> class DataElement<std::vector<T, A>,
88
106
}
89
107
90
108
private:
91
- GaudiAlgorithm* m_owner;
92
- Gaudi::Property<std::vector<std::string>> m_data_names;
109
+ std::unique_ptr<Gaudi::Property<std::vector<std::string>>> m_data_names;
93
110
std::vector<std::unique_ptr<DataHandle<T>>> m_handles;
111
+ gsl::not_null<Gaudi::Algorithm*> m_owner;
94
112
};
95
113
96
- template <bool kIsInput , class NamesArray , class Tuple , size_t ... I>
97
- auto createElements (GaudiAlgorithm* owner, const NamesArray& names, const Tuple&,
98
- std::index_sequence<I...>)
99
- -> std::tuple<DataElement<std::tuple_element_t<I, Tuple>, kIsInput>...> {
100
- return {{owner, std::get<I>(names)}...};
114
+ template <DataMode kMode , class Owner , class NamesArray , class Tuple , size_t ... I>
115
+ auto createElements (Owner* owner, const NamesArray& names, const Tuple&, std::index_sequence<I...>)
116
+ -> std::tuple<DataElement<std::tuple_element_t<I, Tuple>, kMode>...> {
117
+ return {DataElement<std::tuple_element_t <I, Tuple>, kMode >(owner, std::get<I>(names))...};
101
118
}
102
119
103
120
// Call ::get() on each element of the HandleTuple, and return the result in the format of
@@ -111,28 +128,28 @@ ReturnTuple getElements(HandleTuple& handles, std::index_sequence<I...>) {
111
128
112
129
template <class Data > class DataProxy {
113
130
public:
114
- static constexpr bool kIsInput = algorithms::is_input_v<Data>;
115
- using value_type = Data;
116
- using data_type = typename Data::data_type;
117
- constexpr static size_t kSize = Data::kSize ;
118
- using names_type = typename Data::DataNames;
131
+ static constexpr DataMode kMode =
132
+ (algorithms::is_input_v<Data> ? DataMode::kInput : DataMode::kOutput );
133
+ using value_type = typename Data::value_type;
134
+ using data_type = typename Data::data_type;
135
+ constexpr static size_t kSize = Data::kSize ;
136
+ using names_type = typename Data::key_type;
119
137
using elements_type =
120
- decltype (createElements<kIsInput >(std::declval<GaudiAlgorithm*>(), names_type(), data_type(),
121
- std::make_index_sequence<kSize >()));
138
+ decltype (createElements<kMode >(std::declval<GaudiAlgorithm*>(), names_type(), data_type(),
139
+ std::make_index_sequence<kSize >()));
122
140
123
- DataProxy (gsl::not_null<GaudiAlgorithm*> owner, const names_type& names)
124
- : m_owner{owner}
125
- , m_elements{createElements< kIsInput >(m_owner, names, data_type (),
126
- std::make_index_sequence<kSize >())} {}
141
+ template < class Owner >
142
+ DataProxy (Owner* owner, const names_type& names)
143
+ : m_elements{
144
+ createElements< kMode >(owner, names, data_type (), std::make_index_sequence<kSize >())} {}
127
145
void init () {
128
- std::apply ([](auto el) { el.init (); }, m_elements);
146
+ std::apply ([](auto &&... el) { ( el.init (), ... ); }, m_elements);
129
147
}
130
148
value_type get () const {
131
149
return getElements<value_type>(m_elements, std::make_index_sequence<kSize >());
132
150
}
133
151
134
152
private:
135
- GaudiAlgorithm* m_owner;
136
153
elements_type m_elements;
137
154
};
138
155
0 commit comments