LArSoft  v06_85_00
Liquid Argon Software toolkit - http://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 
6 
7 #include <string>
8 #include <typeinfo>
9 #include <vector>
10 
11 namespace art {
12  template <class COLLECTION>
13  void getElementAddresses(COLLECTION const& coll,
14  const std::type_info& iToType,
15  const std::vector<unsigned long>& iIndices,
16  std::vector<void const*>& oPtr);
17 
18  template <typename T>
19  void getElementAddresses(cet::map_vector<T> const& obj,
20  const std::type_info& iToType,
21  const std::vector<unsigned long>& iIndices,
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 pos = 0)
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 <class COLLECTION>
66 void
67 art::getElementAddresses(COLLECTION const& coll,
68  const std::type_info& iToType,
69  const std::vector<unsigned long>& iIndices,
70  std::vector<void const*>& oPtr)
71 {
72  typedef COLLECTION product_type;
73  oPtr.reserve(iIndices.size());
74  for (std::vector<unsigned long>::const_iterator itIndex = iIndices.begin(),
75  itEnd = iIndices.end();
76  itIndex != itEnd;
77  ++itIndex) {
78  auto it = coll.begin();
79  advance(it, *itIndex);
80  oPtr.push_back(detail::maybeCastObj(
82  }
83 }
84 
85 template <typename T>
86 void
87 art::getElementAddresses(cet::map_vector<T> const& obj,
88  const std::type_info& iToType,
89  const std::vector<unsigned long>& iIndices,
90  std::vector<void const*>& oPtr)
91 {
92  typedef cet::map_vector<T> product_type;
93  typedef typename product_type::const_iterator iter;
95  std::string const wanted_type =
96  uniform_type_name(cet::demangle_symbol(iToType.name()));
97  static size_t pos = vh.look_past_pair<T>();
98  oPtr.reserve(iIndices.size());
99  if ((pos < wanted_type.size()) && vh.starts_with_pair(wanted_type, pos)) {
100  // Want value_type.
101  for (std::vector<unsigned long>::const_iterator itIndex = iIndices.begin(),
102  itEnd = iIndices.end();
103  itIndex != itEnd;
104  ++itIndex) {
105  iter it = obj.find(cet::map_vector_key(*itIndex));
106  oPtr.push_back(
107  detail::maybeCastObj((it == obj.end()) ? 0 : &(*it), iToType));
108  }
109  } else {
110  // Want mapped_type.
111  for (std::vector<unsigned long>::const_iterator itIndex = iIndices.begin(),
112  itEnd = iIndices.end();
113  itIndex != itEnd;
114  ++itIndex) {
115  oPtr.push_back(detail::maybeCastObj(
116  obj.getOrNull(cet::map_vector_key(*itIndex)), iToType));
117  }
118  }
119 }
120 
121 #endif /* canvas_Persistency_Common_getElementAddresses_h */
122 
123 // Local Variables:
124 // mode: c++
125 // End:
void const * maybeCastObj(element_type const *address, std::type_info const &tiTo)
Definition: maybeCastObj.h:21
bool starts_with_pair(std::string const &type_name, size_t pos=0)
static std::string const & pair_stem()
std::string uniform_type_name(std::type_info const &tid)
intermediate_table::const_iterator const_iterator
void getElementAddresses(COLLECTION const &coll, const std::type_info &iToType, const std::vector< unsigned long > &iIndices, std::vector< void const * > &oPtr)
HLT enums.