PtrRemapper.h
Go to the documentation of this file.
1 #ifndef art_Framework_Core_PtrRemapper_h
2 #define art_Framework_Core_PtrRemapper_h
3 ////////////////////////////////////////////////////////////////////////
4 // PtrRemapper
5 //
6 // Class to aid in remapping Ptrs in various settings from items in
7 // one branch to (presumably the equivalent) items in another branch
8 // of the same type.
9 //
10 // This class is primarily for use in product mixing and will be
11 // properly initialized by the time it is provided as an argument to
12 // the user-supplied mixing function.
13 //
14 // PtrRemapper is a function object, so all of its work is done with
15 // the apply operator -- operator(). This means that if your mixing
16 // function has an argument (e.g.)
17 //
18 // art::PtrRemapper const& remap
19 //
20 // then the usage is:
21 //
22 // remap(...)
23 //
24 // There are several signatures to operator() and because they are all
25 // templates, they can look fairly impenetrable. It is recommended
26 // therefore to use this header documentation to decide what signature
27 // is most appropriate for your use rather than looking below at the
28 // prototypes or the implementation: there really are "No
29 // User-serviceable Parts."
30 //
31 // Notes common to several signatures.
32 //
33 // * With the exception of the (rarely required) signature 10 (see its
34 // specific documentation), all template arguments are deducible from
35 // the function arguments and therefore it is not necessary to specify
36 // them in <>.
37 //
38 // * Commonly-used arguments:
39 //
40 // * std::vector<COLLECTION<PROD> const*> const& in
41 //
42 // * OutIter& out
43 //
44 // OutIter is a variable of category insert_iterator (usually
45 // created by, for instance std::back_inserter(container)) into a
46 // container of Ptr (either PtrVector or some other collection of
47 // Ptr).
48 //
49 // * offset is a single offset into the container into which the
50 // Ptr points. It should be of type
51 // convertible-to-container::size_type.
52 //
53 // * offsets is an arbitrary container of such offsets as described
54 // in the documentation in
55 // art/Persistency/Common/CollectionUtilities.h.
56 //
57 //
58 // Available signatures to operator() and example usage:
59 //
60 // 1. Remap a single Ptr.
61 //
62 // Ptr<A> newPtr(remap(oldPtr, offset));
63 //
64 // 2. Remap a single PtrVector.
65 //
66 // PtrVector<A> newPV(remap(oldPV, offset));
67 //
68 // 3. Remap a compatible collection (including PtrVector) of Ptr
69 // providing begin, end iterators. (This will also remap a compatible
70 // collection of PtrVector, but not of PtrVector const* -- for the
71 // latter, see 4-10.)
72 //
73 // PtrVector<A> newPV;
74 // remap(oldPV.begin(),
75 // oldPV.end(),
76 // std::back_inserter(newPV),
77 // offset);
78 //
79 // 4. Remap and flatten a set of products which are containers of
80 // Ptrs (which includes PtrVector).
81 //
82 // remap(in, out, offsets)
83 //
84 // where offsets is likely calculated by the appropriate call to
85 // art::flattenCollections. See
86 // art/Persistency/Common/CollectionUtilities for details.
87 //
88 // 5. Remap and flatten a set of containers of Ptrs (including
89 // PtrVector) which may be obtained from a component of the provided
90 // product. Provide a free function of the correct signature to return
91 // a reference to the container of Ptrs given a secondary product,
92 // e.g.:
93 //
94 // PtrVector<B> const& myfunc(A const* prod) {
95 // return prod->myBs();
96 // }
97 //
98 // remap(in, out, offsets, &myfunc);
99 //
100 // 6. Remap and flatten a set of containers of Ptrs (including
101 // PtrVector) which may be obtained from a component of the provided
102 // product. Provide the name of a member function of the provided
103 // product which is an accessor for the container (taking no
104 // arguments).
105 //
106 // remap(in, out, offsets, &A::myBs);
107 //
108 // 7. Remap and flatten a set of containers of Ptrs (including
109 // PtrVector) which may be obtained from a component of the provided
110 // product. Provide the name of a member datum of the provided product
111 // which is the container.
112 //
113 // remap(in, out, offsets, &A::myBs_);
114 //
115 // 8. Remap and flatten a set of containers of Ptrs (including
116 // PtrVector) which is a component of the provided product using the
117 // provided accessor member function of a class which is not the
118 // product.
119 //
120 // class Aprocessor {
121 // public:
122 // B const& myBs(A const*);
123 // };
124 //
125 // Aprocessor myAp;
126 //
127 // remap(in, out, offsets, Aprocessor::myBs, myAp);
128 //
129 // Note: if the compiler complains about an unresolved overload set
130 // for this signature, try an explicit:
131 //
132 // const_cast<Aprocessor&>(myAp);
133 //
134 // 9. Remap and flatten a set of containers of Ptrs (including
135 // PtrVector) which is a component of the provided product using the
136 // provided const accessor member function of a class which is not the
137 // product.
138 //
139 // class Aprocessor {
140 // public:
141 // B const& myBs(A const*) const;
142 // };
143 //
144 // Aprocessor myAp;
145 //
146 // remap(in, out, offsets, Aprocessor::myBs, myAp);
147 //
148 // Note: if the compiler complains about an unresolved overload set
149 // for this signature, try an explicit:
150 //
151 // const_cast<Aprocessor const&>(myAp);
152 //
153 // 10. More general version of 5-9. that takes a final argument which is
154 // of arbitrary type provided it or its operator() has the correct
155 // signature. The drawback is that one of the template arguments (CONT,
156 // specifying the type of the collection of Ptrs you wish to remap) is
157 // not deducible, meaning that instead of:
158 //
159 // remap(...);
160 //
161 // one must type (e.g.):
162 //
163 // remap.operator()<std::vector<art::Ptr<B> > >(...)
164 //
165 // Therefore, 4-9. are the recommended signatures for
166 // straightforward client code -- this one is provided for maximum
167 // flexibility.
168 //
169 ////////////////////////////////////////////////////////////////////////
170 
177 #include "cetlib/exempt_ptr.h"
178 
179 #include <map>
180 
181 namespace art {
182  class PtrRemapper;
183  class ProdToProdMapBuilder;
184 
185  namespace PtrRemapperDetail {
186  // Function template used by 4.
187  template <typename PROD>
188  PROD const&
189  simpleProdReturner(PROD const* prod)
190  {
191  return *prod;
192  }
193 
194  // Function object used by 10.
195  template <typename CONT, typename PROD, typename CALLBACK>
196  class ContReturner {
197  public:
198  explicit ContReturner(CALLBACK callback) : callback_(callback) {}
199  CONT const&
200  operator()(PROD const* prod) const
201  {
202  return callback_(prod);
203  }
204 
205  private:
206  CALLBACK callback_;
207  };
208 
209  template <typename CONT, typename PROD>
210  class ContReturner<CONT, PROD, CONT const& (PROD::*)() const> {
211  public:
212  typedef CONT const& (PROD::*CALLBACK)() const;
213  explicit ContReturner(CALLBACK callback) : callback_(callback) {}
214  CONT const&
215  operator()(PROD const* prod) const
216  {
217  return (prod->*callback_)();
218  }
219 
220  private:
221  CALLBACK callback_;
222  };
223 
224  template <typename CONT, typename PROD>
225  class ContReturner<CONT, PROD, CONT PROD::*const> {
226  public:
227  typedef CONT PROD::*const CALLBACK;
228  explicit ContReturner(CALLBACK callback) : callback_(callback) {}
229  CONT const&
230  operator()(PROD const* prod) const
231  {
232  return prod->*callback_;
233  }
234 
235  private:
237  };
238  } // namespace PtrRemapperDetail
239 } // namespace art
240 
242 public:
243  //////////////////////////////////////////////////////////////////////
244  // Signatures for operator() -- see documentation at top of header.
245 
246  // 1.
247  template <typename PROD, typename SIZE_TYPE>
248  Ptr<PROD> operator()(Ptr<PROD> const& oldPtr, SIZE_TYPE offset) const;
249 
250  // 2.
251  template <typename PROD, typename SIZE_TYPE>
253  SIZE_TYPE offset) const;
254 
255  // 3.
256  template <typename InIter, typename OutIter, typename SIZE_TYPE>
257  void operator()(InIter beg, InIter end, OutIter out, SIZE_TYPE offset) const;
258 
259  // 4.
260  template <typename OutIter, typename PROD, typename OFFSETS>
261  void operator()(std::vector<PROD const*> const& in,
262  OutIter out,
263  OFFSETS const& offsets) const;
264 
265  // 5.
266  template <typename CONT, typename OutIter, typename PROD, typename OFFSETS>
267  void operator()(std::vector<PROD const*> const& in,
268  OutIter out,
269  OFFSETS const& offsets,
270  CONT const& (*extractor)(PROD const*)) const;
271 
272  // 6.
273  template <typename CONT, typename OutIter, typename PROD, typename OFFSETS>
274  void operator()(std::vector<PROD const*> const& in,
275  OutIter out,
276  OFFSETS const& offsets,
277  CONT const& (PROD::*extractor)() const) const;
278 
279  // 7.
280  template <typename CONT, typename OutIter, typename PROD, typename OFFSETS>
281  void operator()(std::vector<PROD const*> const& in,
282  OutIter out,
283  OFFSETS const& offsets,
284  CONT PROD::*const data) const;
285 
286  // 8.
287  template <typename PROD,
288  typename OutIter,
289  typename CONT,
290  typename X,
291  typename OFFSETS>
292  void operator()(std::vector<PROD const*> const& in,
293  OutIter out,
294  OFFSETS const& offsets,
295  CONT const& (X::*extractor)(PROD const*),
296  X& x) const;
297 
298  // 9.
299  template <typename PROD,
300  typename OutIter,
301  typename CONT,
302  typename X,
303  typename OFFSETS>
304  void operator()(std::vector<PROD const*> const& in,
305  OutIter out,
306  OFFSETS const& offsets,
307  CONT const& (X::*extractor)(PROD const*)const,
308  X const& x) const;
309 
310  // 10.
311  template <typename CONT,
312  typename CALLBACK,
313  typename OutIter,
314  typename PROD,
315  typename OFFSETS>
316  void operator()(std::vector<PROD const*> const& in,
317  OutIter out,
318  OFFSETS const& offsets,
319  CALLBACK extractor) const;
320 
321 private:
322  friend class ProdToProdMapBuilder;
323  using ProdTransMap_t = std::map<ProductID, ProductID>;
324 
325  // The following data members are filled by
326  // ProdToProdBuilder::populateRemapper, *not* by PtrRemapper.
327  ProdTransMap_t prodTransMap_{};
329 };
330 
331 // 1.
332 template <typename PROD, typename SIZE_TYPE>
335  SIZE_TYPE const offset) const
336 {
337  if (oldPtr.id().isValid()) {
338  auto iter = prodTransMap_.find(oldPtr.id());
339  if (iter == cend(prodTransMap_)) {
341  << "PtrRemapper: could not find old ProductID " << oldPtr.id()
342  << " in translation table: already translated?\n";
343  }
344  auto productGetter = event_->productGetter(iter->second);
345  if (productGetter == nullptr) {
347  << "PtrRemapper: cannot create output "
348  << TypeID{typeid(art::Ptr<PROD>)}.className()
349  << " with ProductID: " << iter->second
350  << "\nbecause the product is not known. Perhaps the output product "
351  "was misspecified for product mixing.\n";
352  }
353 
354  return oldPtr.isNonnull() ?
355  Ptr<PROD>{iter->second, oldPtr.key() + offset, productGetter} :
356  Ptr<PROD>{iter->second};
357  }
358 
359  // Default-constructed.
360  return Ptr<PROD>{};
361 }
362 
363 // 2.
364 template <typename PROD, typename SIZE_TYPE>
367  SIZE_TYPE const offset) const
368 {
370  result.reserve(old.size());
371  this->operator()(old.begin(),
372  old.end(),
373  std::back_inserter(result),
374  offset); // 3.
375  return result;
376 }
377 
378 // 3.
379 template <typename InIter, typename OutIter, typename SIZE_TYPE>
380 void
382  InIter const end,
383  OutIter out,
384  SIZE_TYPE const offset) const
385 {
386  // Need to assume that all Ptr containers and consistent internally
387  // and with each other due to a lack of productGetters.
388 
389  // Not using transform here allows instantiation for iterator to
390  // collection of Ptr or collection of PtrVector.
391  for (auto i = beg; i != end; ++i) {
392  // Note: this could be signature 1 OR 2 of operator(). If the user
393  // calls this signature (3) with iterators into a collection of
394  // PtrVector, then the call order will be 3, 2, 3, 1 due to the
395  // templates that will be instantiated i.e. the relationship
396  // between signatures 2 and 3 is *not* infinitely recursive.
397  *out++ = this->operator()(*i, offset); // 1 OR 2.
398  }
399 }
400 
401 // 4.
402 template <typename OutIter, typename PROD, typename OFFSETS>
403 void
404 art::PtrRemapper::operator()(std::vector<PROD const*> const& in,
405  OutIter out,
406  OFFSETS const& offsets) const
407 {
408  this->operator()(in,
409  out,
410  offsets,
411  PtrRemapperDetail::simpleProdReturner<PROD>); // 5.
412 }
413 
414 // 5.
415 template <typename CONT, typename OutIter, typename PROD, typename OFFSETS>
416 void
417 art::PtrRemapper::operator()(std::vector<PROD const*> const& in,
418  OutIter out,
419  OFFSETS const& offsets,
420  CONT const& (*extractor)(PROD const*)) const
421 {
422  this->operator()<CONT, CONT const& (*)(PROD const*)>(in,
423  out,
424  offsets,
425  extractor); // 10.
426 }
427 
428 // 6.
429 template <typename CONT, typename OutIter, typename PROD, typename OFFSETS>
430 void
431 art::PtrRemapper::operator()(std::vector<PROD const*> const& in,
432  OutIter out,
433  OFFSETS const& offsets,
434  CONT const& (PROD::*extractor)() const) const
435 {
436  this->operator()<CONT, CONT const& (PROD::*)() const>(in,
437  out,
438  offsets,
439  extractor); // 10.
440 }
441 
442 // 7.
443 template <typename CONT, typename OutIter, typename PROD, typename OFFSETS>
444 void
445 art::PtrRemapper::operator()(std::vector<PROD const*> const& in,
446  OutIter out,
447  OFFSETS const& offsets,
448  CONT PROD::*const data) const
449 {
450  this->operator()<CONT, CONT PROD::*const>(in, out, offsets, data); // 10.
451 }
452 
453 // 8.
454 template <typename PROD,
455  typename OutIter,
456  typename CONT,
457  typename X,
458  typename OFFSETS>
459 void
460 art::PtrRemapper::operator()(std::vector<PROD const*> const& in,
461  OutIter out,
462  OFFSETS const& offsets,
463  CONT const& (X::*)(PROD const*),
464  X& x) const
465 {
466  this->operator()<CONT>(
467  in, out, offsets, [&x](auto& elem) { elem.extractor(x); }); // 10.
468 }
469 
470 // 9.
471 template <typename PROD,
472  typename OutIter,
473  typename CONT,
474  typename X,
475  typename OFFSETS>
476 void
477 art::PtrRemapper::operator()(std::vector<PROD const*> const& in,
478  OutIter out,
479  OFFSETS const& offsets,
480  CONT const& (X::*)(PROD const*)const,
481  X const& x) const
482 {
483  this->operator()<CONT>(
484  in, out, offsets, [&x](auto& elem) { elem.extractor(x); }); // 10.
485 }
486 
487 // 10.
488 template <typename CONT,
489  typename CALLBACK,
490  typename OutIter,
491  typename PROD,
492  typename OFFSETS>
493 void
494 art::PtrRemapper::operator()(std::vector<PROD const*> const& in,
495  OutIter out,
496  OFFSETS const& offsets,
497  CALLBACK extractor) const
498 {
499  if (in.size() != offsets.size()) {
501  << "Collection size of " << in.size()
502  << " disagrees with offset container size of " << offsets.size() << ".\n";
503  }
504  auto i = in.begin();
505  auto const e = in.end();
506  auto off_iter = offsets.begin();
508  extractor};
509  for (; i != e; ++i, ++off_iter) {
510  CONT const& cont{returner.operator()(*i)};
511  this->operator()(cont.begin(), cont.end(), out, *off_iter); // 3.
512  }
513 }
514 
515 #endif /* art_Framework_Core_PtrRemapper_h */
516 
517 // Local Variables:
518 // mode: c++
519 // End:
end
while True: pbar.update(maxval-len(onlies[E][S])) #print iS, "/", len(onlies[E][S]) found = False for...
void reserve(size_type n)
Definition: PtrVector.h:337
decltype(auto) constexpr cend(T &&obj)
ADL-aware version of std::cend.
Definition: StdUtils.h:87
static QCString result
iterator begin()
Definition: PtrVector.h:217
static QCString className
Definition: declinfo.cpp:669
const double e
CONT const & operator()(PROD const *prod) const
Definition: PtrRemapper.h:200
key_type key() const noexcept
Definition: Ptr.h:216
std::map< ProductID, ProductID > ProdTransMap_t
Definition: PtrRemapper.h:323
iterator end()
Definition: PtrVector.h:231
PROD const & simpleProdReturner(PROD const *prod)
Definition: PtrRemapper.h:189
ProductID id() const noexcept
Definition: Ptr.h:190
size_type size() const
Definition: PtrVector.h:302
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
bool isNonnull() const noexcept
Definition: Ptr.h:166
list x
Definition: train.py:276
Ptr< PROD > operator()(Ptr< PROD > const &oldPtr, SIZE_TYPE offset) const
Definition: fwd.h:31