LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
getElementAddresses.h
Go to the documentation of this file.
1 #ifndef canvas_Persistency_Common_getElementAddresses_h
2 #define canvas_Persistency_Common_getElementAddresses_h
3 
7 
8 #include <string>
9 #include <typeinfo>
10 #include <vector>
11 
12 namespace art {
13  template <typename Collection>
14  void getElementAddresses(Collection const& coll,
15  std::type_info const& iToType,
16  std::vector<unsigned long> const& indices,
17  std::vector<void const*>& oPtr);
18 
19  template <typename T>
20  void getElementAddresses(cet::map_vector<T> const& obj,
21  std::type_info const& iToType,
22  std::vector<unsigned long> const& indices,
23  std::vector<void const*>& oPtr);
24 
25  namespace detail {
26  class value_type_helper;
27  }
28 }
29 
31 public:
32  static std::string const&
34  {
35  static std::string const pair_stem_s("std::pair<");
36  return pair_stem_s;
37  }
38 
39  static size_t
41  {
42  static size_t const pair_stem_offset_s = pair_stem().size();
43  return pair_stem_offset_s;
44  }
45 
46  bool
47  starts_with_pair(std::string const& type_name, size_t const pos)
48  {
49  return type_name.compare(pos, pair_stem_offset(), pair_stem()) == 0;
50  }
51 
52  template <typename T>
53  size_t
55  {
56  static std::string const mapped_type =
57  cet::demangle_symbol(typeid(T).name());
58  size_t pos = 0;
59  while (starts_with_pair(mapped_type, pos)) {
60  pos += pair_stem_offset();
61  }
62  return pos;
63  }
64 };
65 
66 template <typename Collection>
67 void
68 art::getElementAddresses(Collection const& coll,
69  std::type_info const& iToType,
70  std::vector<unsigned long> const& indices,
71  std::vector<void const*>& oPtr)
72 {
73  oPtr.reserve(indices.size());
74  for (auto const index : indices) {
75  auto it = coll.cbegin();
76  advance(it, index);
77  oPtr.push_back(detail::maybeCastObj(
79  }
80 }
81 
82 template <typename T>
83 void
84 art::getElementAddresses(cet::map_vector<T> const& obj,
85  std::type_info const& iToType,
86  std::vector<unsigned long> const& indices,
87  std::vector<void const*>& oPtr)
88 {
90  std::string const wanted_type =
91  uniform_type_name(cet::demangle_symbol(iToType.name()));
92  static size_t const pos = vh.look_past_pair<T>();
93  oPtr.reserve(indices.size());
94  if ((pos < wanted_type.size()) && vh.starts_with_pair(wanted_type, pos)) {
95  // Want value_type.
96  for (auto const index : indices) {
97  auto it = obj.find(cet::map_vector_key{index});
98  auto ptr = (it == obj.cend()) ? nullptr : &*it;
99  oPtr.push_back(detail::maybeCastObj(ptr, iToType));
100  }
101  } else {
102  // Want mapped_type.
103  for (auto const index : indices) {
104  auto ptr = obj.getOrNull(cet::map_vector_key{index});
105  oPtr.push_back(detail::maybeCastObj(ptr, iToType));
106  }
107  }
108 }
109 
110 #endif /* canvas_Persistency_Common_getElementAddresses_h */
111 
112 // Local Variables:
113 // mode: c++
114 // End:
bool starts_with_pair(std::string const &type_name, size_t const pos)
static std::string const & pair_stem()
std::string uniform_type_name(std::type_info const &tid)
constexpr std::array< std::size_t, geo::vect::dimension< Vector >)> indices()
Returns a sequence of indices valid for a vector of the specified type.
void getElementAddresses(Collection const &coll, std::type_info const &iToType, std::vector< unsigned long > const &indices, std::vector< void const * > &oPtr)
Definition: MVAAlg.h:12
void const * maybeCastObj(void const *address, std::type_info const &tiFrom, std::type_info const &tiTo)