getElementAddresses.h
Go to the documentation of this file.
1 #ifndef canvas_Persistency_Common_getElementAddresses_h
2 #define canvas_Persistency_Common_getElementAddresses_h
3 
6 
7 #include <string>
8 #include <typeinfo>
9 #include <vector>
10 
11 namespace art {
12  template <typename Collection>
13  void getElementAddresses(Collection const& coll,
14  std::type_info const& iToType,
15  std::vector<unsigned long> const& indices,
16  std::vector<void const*>& oPtr);
17 
18  template <typename T>
20  std::type_info const& iToType,
21  std::vector<unsigned long> const& indices,
22  std::vector<void const*>& oPtr);
23 
24  namespace detail {
25  class value_type_helper;
26  }
27 }
28 
30 public:
31  static std::string const&
33  {
34  static std::string const pair_stem_s("std::pair<");
35  return pair_stem_s;
36  }
37 
38  static size_t
40  {
41  static size_t const pair_stem_offset_s = pair_stem().size();
42  return pair_stem_offset_s;
43  }
44 
45  bool
46  starts_with_pair(std::string const& type_name, size_t const pos)
47  {
48  return type_name.compare(pos, pair_stem_offset(), pair_stem()) == 0;
49  }
50 
51  template <typename T>
52  size_t
54  {
55  static std::string const mapped_type =
56  cet::demangle_symbol(typeid(T).name());
57  size_t pos = 0;
58  while (starts_with_pair(mapped_type, pos)) {
59  pos += pair_stem_offset();
60  }
61  return pos;
62  }
63 };
64 
65 template <typename Collection>
66 void
67 art::getElementAddresses(Collection const& coll,
68  std::type_info const& iToType,
69  std::vector<unsigned long> const& indices,
70  std::vector<void const*>& oPtr)
71 {
72  oPtr.reserve(indices.size());
73  for (auto const index : indices) {
74  auto it = coll.cbegin();
75  advance(it, index);
76  oPtr.push_back(detail::maybeCastObj(
78  }
79 }
80 
81 template <typename T>
82 void
84  std::type_info const& iToType,
85  std::vector<unsigned long> const& indices,
86  std::vector<void const*>& oPtr)
87 {
89  std::string const wanted_type =
90  uniform_type_name(cet::demangle_symbol(iToType.name()));
91  static size_t const pos = vh.look_past_pair<T>();
92  oPtr.reserve(indices.size());
93  if ((pos < wanted_type.size()) && vh.starts_with_pair(wanted_type, pos)) {
94  // Want value_type.
95  for (auto const index : indices) {
96  auto it = obj.find(cet::map_vector_key{index});
97  auto ptr = (it == obj.cend()) ? nullptr : &*it;
98  oPtr.push_back(detail::maybeCastObj(ptr, iToType));
99  }
100  } else {
101  // Want mapped_type.
102  for (auto const index : indices) {
103  auto ptr = obj.getOrNull(cet::map_vector_key{index});
104  oPtr.push_back(detail::maybeCastObj(ptr, iToType));
105  }
106  }
107 }
108 
109 #endif /* canvas_Persistency_Common_getElementAddresses_h */
110 
111 // Local Variables:
112 // mode: c++
113 // End:
static QCString name
Definition: declinfo.cpp:673
const_iterator cend() const noexcept
Definition: map_vector.h:228
std::string string
Definition: nybbler.cc:12
iterator find(key_type key)
Definition: map_vector.h:381
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)
void const * maybeCastObj(void const *address, std::type_info const &tiFrom, std::type_info const &tiTo)
mapped_type * getOrNull(key_type key)
Definition: map_vector.h:417