ATLAS Offline Software
NavigationToken.icc
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 ////////////////////////////////////
6 // Navigationtoken Implementation //
7 ////////////////////////////////////
8 
9 #include <iostream>
10 #include <iomanip>
11 
12 // query depth control
13 template<typename CHILD, typename CHILDPAR, typename HASH>
14 bool
15 NavigationToken<CHILD,CHILDPAR,HASH>::pushQuery(const INavigable& /*parent*/,
16  const INavigable& /*child*/ )
17 {
18  return true;
19  // return ( m_navCondition != 0 )
20  // ? ( m_navCondition->accept(&parent) )
21  // : true;
22 }
23 
24 // relational parameter access
25 template<typename CHILD, typename CHILDPAR, typename HASH>
26 CHILDPAR
27 NavigationToken<CHILD,CHILDPAR,HASH>::getParameter(const_child_ptr data ) const
28 {
29  typename NavigationToken::tokenConstIterator found = m_data.find(data);
30  return ( found != m_data.end()
31  ? (*found).second
32  : CHILDPAR() );
33 }
34 
35 template<typename CHILD, typename CHILDPAR, typename HASH>
36 CHILDPAR
37 NavigationToken<CHILD,CHILDPAR,HASH>::getParameter( const_iterator& anIter )
38  const
39 {
40  return anIter.getParameter();
41 }
42 
43 // clear/reset token
44 template <typename CHILD, typename CHILDPAR, typename HASH>
45 void NavigationToken<CHILD,CHILDPAR,HASH>::clear()
46 {
47  typedef typename tokenStore::allocator_type alloc_t;
48  m_data.clear();
49  alloc_t::get_allocator(m_data).reset();
50  if ( m_navSelector != 0 ) m_navSelector->reset();
51  if ( m_navCondition != 0 ) m_navCondition->reset();
52 }
53 
54 
55 namespace Navigation_detail {
56 
57 
58 // Add a pointer/weight pair to the map.
59 template <typename MAP, typename const_child_ptr, typename CHILDPAR>
60 inline
61 void addToMap (MAP& map,
62  const_child_ptr data,
63  CHILDPAR weight)
64 {
65  // Insert into container iff nothing already exists with this key.
66  // But return an iterator pointing at the element in any case.
67  std::pair<typename MAP::iterator, bool> ret =
68  map.insert (std::make_pair (data, weight));
69  if ( !ret.second )
70  {
71  // Didn't insert (because an element was already there).
72  // Add weights.
73  ret.first->second += weight;
74  }
75 }
76 
77 
78 // Add a pointer/weight pair to the map.
79 // Specialization for the DefaultWeight case.
80 template <typename MAP, typename const_child_ptr>
81 inline
82 void addToMap (MAP& map,
83  const_child_ptr data,
84  NavigationDefaults::DefaultWeight /*weight*/)
85 {
86  // In this case, we don't need to worry about propagating or summing
87  // weights. We just need to keep track of unique child pointers.
88  // Make sure the pointer is in the map.
89  map[data];
90 }
91 
92 
93 } // namespace Navigation_detail
94 
95 
96 // set object in token
97 template <typename CHILD, typename CHILDPAR, typename HASH>
98 void NavigationToken<CHILD,CHILDPAR,HASH>::setObject(const_child_ptr data,
99  CHILDPAR weight )
100 {
101  // check pointer validity
102  if ( data != 0 )
103  {
104  if ( m_navSelector == 0 || (m_navSelector !=0 && m_navSelector->accept(data,weight)) )
105  {
106  Navigation_detail::addToMap (m_data, data, weight);
107  }
108  }
109 }
110 
111 // check if queried object is of requested type itself
112 template <typename CHILD, typename CHILDPAR, typename HASH>
113 bool NavigationToken<CHILD,CHILDPAR,HASH>::trySetObject(const
114  INavigable* theObject)
115 {
116  // See if this is the correct type.
117  // Cache the last complete type we rejected, to cut down on the
118  // number of dynamic_cast calls.
119  const std::type_info* ti = &typeid (*theObject);
120  if (ti == m_lastReject)
121  return false;
122  const_child_ptr thePtr = dynamic_cast<const_child_ptr>(theObject);
123  if ( thePtr != 0 )
124  {
125  this->setObject(thePtr);
126  return true;
127  }
128  m_lastReject = ti;
129  return false;
130 }
131 
132 // check if queried object is of requested type itself, including parameters
133 template <typename CHILD, typename CHILDPAR, typename HASH>
134 bool
135 NavigationToken<CHILD,CHILDPAR,HASH>::trySetObject(const
136  INavigable* theObject,
137  const std::any& theWeight)
138 {
139  // See if this is the correct type.
140  // Cache the last complete type we rejected, to cut down on the
141  // number of dynamic_cast calls.
142  const std::type_info* ti = &typeid (*theObject);
143  if (ti == m_lastReject)
144  return false;
145  const_child_ptr thePtr = dynamic_cast<const_child_ptr>(theObject);
146  try {
147  CHILDPAR myWeight(std::any_cast<CHILDPAR>(theWeight));
148  if ( thePtr != 0 )
149  {
150  setObject(thePtr,myWeight);
151  return true;
152  }
153  m_lastReject = ti;
154  } catch(...) { }
155  return false;
156 }
157 
158 // dump token
159 template <typename CHILD, typename CHILDPAR, typename HASH>
160 void NavigationToken<CHILD,CHILDPAR,HASH>::dumpStore()
161 {
162  std::cout << "[NavigationToken::dumpStore] - # elements in store: "
163  << this->size() << std::endl;
164  tokenConstIterator first = m_data.begin();
165  unsigned int iCtr = 0;
166  for ( ; first != m_data.end(); first++ )
167  {
168  iCtr++;
169  std::cout << "Element " << std::setw(6) << iCtr << ": pointer to object "
170  << (*first).first
171  << ", parameter "
172  << (*first).second
173  << std::endl;
174  }
175 }