ParallelData.h
Go to the documentation of this file.
1 /**
2  * @file lardata/RecoBaseProxy/ProxyBase/ParallelData.h
3  * @brief Auxiliary data from parallel data products.
4  * @author Gianluca Petrillo (petrillo@fnal.gov)
5  * @date July 27, 2017
6  * @see lardata/RecoBaseProxy/ProxyBase.h
7  *
8  * This library is header-only.
9  */
10 
11 #ifndef LARDATA_RECOBASEPROXY_PROXYBASE_PARALLELDATA_H
12 #define LARDATA_RECOBASEPROXY_PROXYBASE_PARALLELDATA_H
13 
14 // LArSoft libraries
15 #include "lardata/Utilities/TupleLookupByTag.h" // util::makeTagged(), ...
16 #include "larcorealg/CoreUtils/ContainerMeta.h" // util::collection_value_t, ...
17 
18 // C/C++ standard
19 #include <utility> // std::declval()
20 #include <type_traits> // std::is_convertible<>, ...
21 #include <cstdlib> // std::size_t
22 
23 
24 
25 namespace proxy {
26 
27  // --- BEGIN LArSoftProxiesParallelData --------------------------------------
28  /// @addtogroup LArSoftProxiesParallelData
29  /// @{
30 
31  /**
32  * @brief Wraps a collection into a parallel data collection object.
33  * @tparam AuxColl type of parallel data data product container
34  * @tparam Aux type of parallel data to be associated to the main objects
35  * (if omitted: `AuxColl::value_type`)
36  * @tparam Tag the tag labelling this associated data (if omitted: as `Aux`)
37  * @param data data collection to be wrapped
38  * @return a new `ParallelData` wrapping the information in `data`
39  *
40  * The data collection must be non-temporary and it is treated as fulfilling
41  * @ref LArSoftProxyDefinitionParallelData "parallel data product"
42  * requirements.
43  *
44  * Example:
45  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
46  * std::vector<recob::TrackFitHitInfo> trackData;
47  * // ...
48  * auto auxData = makeParallelData(trackData);
49  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
50  * where the tag of the parallel data is now `recob::TrackFitHitInfo` and
51  * `auxData` behaviour becomes undefined as soon as `trackData` falls out of
52  * scope.
53  */
54  template <
55  typename AuxColl,
56  typename Aux = util::collection_value_t<AuxColl>,
57  typename Tag = Aux
58  >
59  auto makeParallelData(AuxColl const& data);
60 
61 
62  //----------------------------------------------------------------------------
63  namespace details {
64 
65  /**
66  * @brief Object to draft parallel data interface.
67  * @tparam AuxColl type of the parallel data collection
68  * @tparam Aux type of the associated object
69  * @tparam Tag tag this data is labeled with
70  *
71  * Allows:
72  * * random access (no index check guarantee)
73  * * forward iteration
74  *
75  * Construction is not part of the interface.
76  */
77  template <
78  typename AuxColl,
79  typename Aux /* = util::collection_value_t<AuxColl> */,
80  typename Tag /* = Aux */
81  >
82  class ParallelData {
83  using This_t = ParallelData<AuxColl, Aux, Tag>; ///< This type.
84 
85  /// Type of auxiliary collection.
86  using parallel_data_t = AuxColl;
87 
88  /// Type of the value of auxiliary collection element.
89  using aux_t = Aux; // unused
90 
91  /// Type returned when accessing an auxiliary collection element.
93 
95 
96  public:
97  using tag = Tag; ///< Tag of this association proxy.
98 
99  /// Type returned when accessing auxiliary data.
100  using auxiliary_data_t
101  = decltype(util::makeTagged<tag>(std::declval<aux_element_t>()));
102 
103  /// Constructor: points to the specified data collection.
105  : fData(&data)
106  {}
107 
108  /// Returns an iterator pointing to the first data element.
109  auto begin() const -> decltype(auto)
110  { return fData->begin(); }
111 
112  /// Returns an iterator pointing past the last data element.
113  auto end() const -> decltype(auto)
114  { return fData->end(); }
115 
116  /// Returns the element with the specified index (no check performed).
117  auto operator[] (std::size_t index) const -> decltype(auto)
118  {
119  static_assert(
120  std::is_convertible<decltype(getElement(index)), auxiliary_data_t>(),
121  "Inconsistent data types."
122  );
123  return getElement(index);
124  }
125 
126  /// Returns whether this data is labeled with the specified tag.
127  template <typename TestTag>
128  static constexpr bool hasTag() { return std::is_same<TestTag, tag>(); }
129 
130  /// Returns a pointer to the whole data collection.
131  parallel_data_t const* data() const { return fData; }
132 
133  /// Returns a reference to the whole data collection.
134  parallel_data_t const& dataRef() const { return *(data()); }
135 
136  private:
137 
138  parallel_data_t const* fData; ///< Reference to the original data product.
139 
140  auto getElement(std::size_t index) const -> decltype(auto)
141  { return util::makeTagged<tag>(fData->operator[](index)); }
142 
143  }; // class ParallelData<>
144 
145 
146  //--------------------------------------------------------------------------
147 
148  } // namespace details
149 
150 
151  /// @}
152  // --- END LArSoftProxiesParallelData ----------------------------------------
153 
154 } // namespace proxy
155 
156 
157 //------------------------------------------------------------------------------
158 //--- template implementation
159 //------------------------------------------------------------------------------
160 namespace proxy {
161 
162  //----------------------------------------------------------------------------
163  //--- makeParallelData() implementations
164  //----------------------------------------------------------------------------
165  template <
166  typename AuxColl,
167  typename Aux /* = util::collection_value_t<AuxColl>*/,
168  typename Tag /* = Aux */
169  >
170  auto makeParallelData(AuxColl const& data) {
171 
172  // Ahh, simplicity.
174 
175  } // makeParallelData(AuxColl)
176 
177 
178  //----------------------------------------------------------------------------
179 
180 } // namespace proxy
181 
182 
183 #endif // LARDATA_RECOBASEPROXY_PROXYBASE_PARALLELDATA_H
auto operator[](std::size_t index) const -> decltype(auto)
Returns the element with the specified index (no check performed).
Definition: ParallelData.h:117
Object to draft parallel data interface.
Definition: ParallelData.h:82
parallel_data_t const * data() const
Returns a pointer to the whole data collection.
Definition: ParallelData.h:131
parallel_data_t const * fData
Reference to the original data product.
Definition: ParallelData.h:138
util::collection_value_constant_access_t< AuxProxyColl > aux_element_t
Type returned when accessing an auxiliary collection element.
Definition: ParallelData.h:92
Definition: tag.cpp:4
static constexpr bool hasTag()
Returns whether this data is labeled with the specified tag.
Definition: ParallelData.h:128
intermediate_table::const_iterator const_iterator
AuxProxyColl parallel_data_t
Type of auxiliary collection.
Definition: ParallelData.h:86
auto begin() const -> decltype(auto)
Returns an iterator pointing to the first data element.
Definition: ParallelData.h:109
auto makeParallelData(AuxColl const &data)
Wraps a collection into a parallel data collection object.
Definition: ParallelData.h:170
parallel_data_t const & dataRef() const
Returns a reference to the whole data collection.
Definition: ParallelData.h:134
Aux aux_t
Type of the value of auxiliary collection element.
Definition: ParallelData.h:89
ParallelData(parallel_data_t const &data)
Constructor: points to the specified data collection.
Definition: ParallelData.h:104
Utilities to address elements of a tuple-like class by tag.
auto getElement(std::size_t index) const -> decltype(auto)
Definition: ParallelData.h:140
decltype(util::makeTagged< tag >(std::declval< aux_element_t >())) auxiliary_data_t
Type returned when accessing auxiliary data.
Definition: ParallelData.h:101
typename parallel_data_t::const_iterator parallel_data_iterator_t
Definition: ParallelData.h:94
typename collection_value_type< Coll >::type collection_value_t
Type contained in the collection Coll.
Definition: ContainerMeta.h:65
C++ metaprogramming utilities for dealing with containers.
typename collection_value_constant_access_type< Coll >::type collection_value_constant_access_t
Type obtained by constant access to element of collection Coll.
Definition: ContainerMeta.h:87
auto end() const -> decltype(auto)
Returns an iterator pointing past the last data element.
Definition: ParallelData.h:113