LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
setPtr.h
Go to the documentation of this file.
1 #ifndef canvas_Persistency_Common_setPtr_h
2 #define canvas_Persistency_Common_setPtr_h
3 
6 #include "cetlib/map_vector.h"
7 #include "cetlib_except/demangle.h"
8 
9 #include <iterator>
10 #include <typeinfo>
11 
12 namespace art {
13  template <class COLLECTION>
14  void setPtr(COLLECTION const& coll,
15  std::type_info const& iToType,
16  unsigned long iIndex,
17  void const*& oPtr);
18 
19  template <typename T>
20  void setPtr(cet::map_vector<T> const& obj,
21  std::type_info const& iToType,
22  unsigned long iIndex,
23  void const*& oPtr);
24 }
25 
26 template <class Collection>
27 void
28 art::setPtr(Collection const& coll,
29  std::type_info const& iToType,
30  unsigned long iIndex,
31  void const*& oPtr)
32 {
33  using product_type = Collection;
34  auto it = coll.begin();
35  if (iIndex >= coll.size()) {
36  throw Exception{
38  "An out-of-bounds error was encountered while setting an art::Ptr.\n"}
39  << "An attempt was made to access an element with index " << iIndex
40  << " for a container with a size of " << coll.size() << ".\n"
41  << "The container is of type " +
42  cet::demangle_symbol(typeid(Collection).name())
43  << ".\n";
44  }
45  advance(it, iIndex);
47  iToType);
48 }
49 
50 template <typename T>
51 void
52 art::setPtr(cet::map_vector<T> const& obj,
53  std::type_info const& iToType,
54  unsigned long iIndex,
55  void const*& oPtr)
56 {
58  std::string const wanted_type =
59  uniform_type_name(cet::demangle_symbol(iToType.name()));
60  static size_t pos = vh.look_past_pair<T>();
61  auto const it = obj.findOrThrow(cet::map_vector_key{iIndex});
62  assert(it != obj.end());
63  if (pos < wanted_type.size() and vh.starts_with_pair(wanted_type, pos)) {
64  // Want value_type, not mapped_type;
65  oPtr = detail::maybeCastObj(&*it, iToType);
66  } else {
67  oPtr = detail::maybeCastObj(&it->second, iToType);
68  }
69 }
70 
71 #endif /* canvas_Persistency_Common_setPtr_h */
72 
73 // Local Variables:
74 // mode: c++
75 // End:
bool starts_with_pair(std::string const &type_name, size_t const pos)
std::string uniform_type_name(std::type_info const &tid)
void setPtr(COLLECTION const &coll, std::type_info const &iToType, unsigned long iIndex, void const *&oPtr)
static auto const * address(const_iterator const &i)
Definition: GetProduct.h:9
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
Definition: MVAAlg.h:12
void const * maybeCastObj(void const *address, std::type_info const &tiFrom, std::type_info const &tiTo)