ATLAS Offline Software
AnaToolHandle.icc
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #ifndef ASGTOOLS_ANATOOLHANDLE_ICC
6 #define ASGTOOLS_ANATOOLHANDLE_ICC
7 
8 #include <AsgTools/MessageCheckAsgTools.h>
9 #include "CxxUtils/checker_macros.h"
10 #include <assert.h>
11 #include <cstdlib>
12 
13 #ifndef XAOD_STANDALONE
14 #include "Gaudi/Interfaces/IOptionsSvc.h"
15 #endif
16 
17 
18 namespace asg
19 {
20  namespace detail
21  {
22 #ifdef XAOD_STANDALONE
23  StatusCode makeToolRootCore (const std::string& type,
24  const std::string& name,
25  AsgTool*& tool);
26 #else
27  StatusCode readToolConfig (AsgToolConfig& config, const std::string& toolName);
28  StatusCode copyPropertiesInCatalogue( const std::string& fromTool, const std::string& toTool );
29  StatusCode hasPropertiesInCatalogue( const std::string& toolName );
30  StatusCode addPropertyToCatalogue( const std::string& toolName, const std::string& property, const std::string& val);
31  StatusCode removePropertyFromCatalogue( const std::string& toolName, const std::string& property);
32  StatusCode toolExists( const std::string& fullName, interfaceType_t*& tool );
33 #endif
34 
35 
36 
37  /// \brief do a checked cast from one ToolHandle interface to
38  /// another
39  /// \par Guarantee
40  /// basic
41  /// \par Failures
42  /// interface not supported for tool
43  template<typename T1,typename T2> StatusCode
44  toolHandleCast (ToolHandle<T1>& to, ToolHandle<T2>& from)
45  {
46  using namespace msgUserCode;
47 
48 #ifdef XAOD_STANDALONE
49  to = ToolHandle<T1> (dynamic_cast<T1*>(&*from));
50  if (!from.empty() && to.operator->() == nullptr)
51  {
52  ANA_MSG_ERROR ("failed to cast from type " << typeid(T2).name() << " to " << typeid(T1).name());
53  return StatusCode::FAILURE;
54  }
55 #else
56  to = ToolHandle<T1> (from.typeAndName(), from.parent());
57  if (!from.empty())
58  ANA_CHECK (to.retrieve());
59 #endif
60  return StatusCode::SUCCESS;
61  }
62 
63 
64 
65 
66 
67  /// \brief the information needed to share a tool
68 
69  class AnaToolShare
70  {
71  //
72  // public interface
73  //
74 
75  /// \brief standard constructor
76  /// \par Guarantee
77  /// strong
78  /// \par Failures
79  /// out of memory II
80  public:
81  AnaToolShare (const ToolHandle<interfaceType_t>& val_th,
82  std::shared_ptr<void> val_cleanup,
83  std::vector<std::function<StatusCode ()>>&& extraInit);
84 
85 
86  /// \brief the tool we contain
87  /// \par Guarantee
88  /// no-fail
89  /// \post result != nullptr
90  public:
91  ToolHandle<interfaceType_t>& th ();
92 
93 
94 
95  //
96  // private interface
97  //
98 
99  /// \brief the value of \ref tool
100  private:
101  ToolHandle<interfaceType_t> m_th;
102 
103  /// \brief resources to release when we are releasing the tool
104  private:
105  std::vector<std::function<StatusCode ()>> m_extraInit;
106 
107  /// \brief resources to release when we are releasing the tool
108  private:
109  std::shared_ptr<void> m_cleanup;
110  };
111 
112 
113 
114 
115 
116  /// \brief a service for sharing tools of a given type
117 
118  class AnaToolShareList
119  {
120  //
121  // public interface
122  //
123 
124  /// \brief the singleton we are using
125  /// \par Guarantee
126  /// no-fail
127  public:
128  static AnaToolShareList& singleton () noexcept;
129 
130 
131  /// \brief get the share for the given name, or nullptr if no
132  /// share has been defined
133  /// \par Guarantee
134  /// strong
135  /// \par Failures
136  /// out of memory I
137  public:
138  std::shared_ptr<AnaToolShare>
139  getShare (const std::string& name) const;
140 
141 
142  /// \brief set the share for the given name
143  /// \return the share to use, this is either the share passed in
144  /// or the already set share
145  /// \par Guarantee
146  /// strong
147  /// \par Failures
148  /// out of memory II
149  private:
150  std::shared_ptr<AnaToolShare>
151  setShare (const std::string& name,
152  std::unique_ptr<AnaToolShare> val_share);
153 
154 
155  /// \brief make or get a share for the given name
156  /// \return the share to use, either freshly created or
157  /// retrieved from the store
158  /// \par Guarantee
159  /// strong
160  /// \par Failures
161  /// tool creation failures\n
162  /// out of memory II
163  public:
164  StatusCode
165  makeShare (const std::string& name,
166  const AsgToolConfig& config,
167  std::vector<std::function<StatusCode ()>>&& extraInit,
168  std::shared_ptr<AnaToolShare>& result);
169 
170 
171 
172  //
173  // private interface
174  //
175 
176  /// \brief the shares we manage
177  private:
178  std::map<std::string,std::weak_ptr<AnaToolShare> > m_shared;
179  mutable std::recursive_mutex m_mutex;
180  typedef std::lock_guard<std::recursive_mutex> lock_t;
181  };
182  }
183 
184 
185 
186  template <typename T>
187  std::ostream& operator << (std::ostream& str, const AnaToolHandle<T>& obj)
188  {
189  return str << obj.getHandle();
190  }
191 
192 
193 
194  template<class T> void AnaToolHandle<T> ::
195  testInvariant () const
196  {
197 #define CHECK_INVARIANT(x) \
198  if (!(x)) { std::cerr << __FILE__ << ":" << __LINE__ << ": invariant violated: " << #x << std::endl; std::abort(); }
199 
200  // General requirements
201  CHECK_INVARIANT (m_handleUser != nullptr);
202  CHECK_INVARIANT (m_name == m_config.name());
203 
204 #undef CHECK_INVARIANT
205  }
206 
207 
208 
209  template<class T> AnaToolHandle<T> ::
210  AnaToolHandle (const std::string& val_name, parentType_t *val_parent)
211  : m_parentPtr (val_parent),
212  m_handleUser (new ToolHandle<T> (val_name, val_parent)),
213  m_allowEmpty (val_name.empty())
214  {
215  setTypeAndName (val_name);
216 
217 #ifndef NDEBUG
218  this->testInvariant ();
219 #endif
220  }
221 
222 
223 
224  template<class T> AnaToolHandle<T> ::
225  AnaToolHandle (AnaToolHandle<T>&& that)
226  : AnaToolHandle ()
227  {
228  swap (that);
229  }
230 
231 
232 
233  template<class T> AnaToolHandle<T> ::
234  AnaToolHandle (const AnaToolHandle<T>& that)
235  : m_config (that.m_config),
236  m_name (that.m_name),
237  m_parentPtr (that.m_parentPtr),
238  m_handleUser (new ToolHandle<T> (*that.m_handleUser)),
239  m_originalTypeAndName (that.m_originalTypeAndName),
240  m_isInitialized (that.m_isInitialized.load()),
241  m_allowEmpty (that.m_allowEmpty)
242  {
243  if (m_isInitialized.load())
244  {
245  m_toolPtr = that.m_toolPtr;
246  m_mode = that.m_mode;
247  m_extraInit = that.m_extraInit;
248  m_cleanup = that.m_cleanup;
249  }
250 
251 #ifndef XAOD_STANDALONE
252  if (!m_handleUser->empty())
253  (*m_handleUser)->release();
254 #endif
255 
256 #ifndef NDEBUG
257  that.testInvariant ();
258  testInvariant ();
259 #endif
260  }
261 
262 
263 
264  template<class T> AnaToolHandle<T>& AnaToolHandle<T> ::
265  operator = (const AnaToolHandle<T>& that)
266  {
267  // no invariant used
268  AnaToolHandle<T> (that).swap (*this);
269  return *this;
270  }
271 
272 
273 
274  template<class T> void AnaToolHandle<T> ::
275  swap (AnaToolHandle<T>& that) noexcept
276  {
277 #ifndef NDEBUG
278  testInvariant ();
279  that.testInvariant ();
280 #endif
281 
282  std::swap (m_extraInit, that.m_extraInit);
283  std::swap (m_cleanup, that.m_cleanup);
284  std::swap (m_config, that.m_config);
285  m_name.swap (that.m_name);
286  std::swap (m_parentPtr, that.m_parentPtr);
287  {
288  ToolHandle<T> tmp = *m_handleUser;
289 #ifndef XAOD_STANDALONE
290  if (!tmp.empty())
291  tmp->release();
292 #endif
293  *m_handleUser = *that.m_handleUser;
294  *that.m_handleUser = tmp;
295  }
296  m_originalTypeAndName.swap (that.m_originalTypeAndName);
297  {
298  const auto tmp = m_isInitialized.load();
299  m_isInitialized = that.m_isInitialized.load();
300  that.m_isInitialized = tmp;
301  }
302  std::swap (m_toolPtr, that.m_toolPtr);
303  std::swap (m_mode, that.m_mode);
304  std::swap (m_allowEmpty, that.m_allowEmpty);
305 
306 #ifndef NDEBUG
307  testInvariant ();
308  that.testInvariant ();
309 #endif
310  }
311 
312 
313 
314  template<class T> AnaToolHandle<T>& AnaToolHandle<T> ::
315  operator = (AnaToolHandle<T>&& that)
316  {
317  // no invariant used
318  swap (that);
319  return *this;
320  }
321 
322 
323 
324  template<class T> AnaToolHandle<T> ::
325  ~AnaToolHandle () noexcept
326  {
327  using namespace msgToolHandle;
328 #ifndef NDEBUG
329  this->testInvariant ();
330 #endif
331  }
332 
333 
334  template<class T> bool
335  AnaToolHandle<T> ::
336  empty () const
337  {
338 #ifndef NDEBUG
339  this->testInvariant ();
340 #endif
341  switch (getMode())
342  {
343  case detail::AnaToolHandleMode::EMPTY:
344  return true;
345  case detail::AnaToolHandleMode::CREATE_PRIVATE:
346  case detail::AnaToolHandleMode::CREATE_SHARED:
347  case detail::AnaToolHandleMode::RETRIEVE_SHARED:
348  return false;
349  case detail::AnaToolHandleMode::USER:
350  return m_handleUser->empty();
351  }
352  return false; //compiler dummy
353  }
354 
355 
356 
357  template<class T> inline bool AnaToolHandle<T> ::
358  isPublic () const noexcept
359  {
360 #ifndef NDEBUG
361  this->testInvariant ();
362 #endif
363  return m_parentPtr == nullptr;
364  }
365 
366 
367 
368  template<class T> inline const std::string& AnaToolHandle<T> ::
369  type () const noexcept
370  {
371 #ifndef NDEBUG
372  this->testInvariant ();
373 #endif
374  return m_config.type();
375  }
376 
377 
378 
379  template<class T> inline const std::string&
380  AnaToolHandle<T> ::
381  name () const noexcept
382  {
383 #ifndef NDEBUG
384  this->testInvariant ();
385 #endif
386  return m_name;
387  }
388 
389 
390 
391  template<typename T> template<typename T2>
392  StatusCode AnaToolHandle<T> ::
393  setProperty (const std::string& property, const T2& value)
394  {
395  using namespace msgToolHandle;
396 
397 #ifndef NDEBUG
398  this->testInvariant ();
399  if (isInitialized())
400  {
401  ANA_MSG_FATAL ("AnaToolHandle has already been initialized: " << *this);
402  std::abort();
403  }
404 #endif
405 
406  return m_config.setProperty (property, value);
407  }
408 
409 
410 
411  template<typename T> template<typename T2>
412  typename std::enable_if<std::is_base_of_v<parentType_t, T2>, StatusCode>::type
413  AnaToolHandle<T> ::
414  setProperty (const std::string& property,
415  const ToolHandle<T2>& value)
416  {
417  using namespace msgToolHandle;
418 
419 #ifndef NDEBUG
420  this->testInvariant ();
421  if (isInitialized())
422  {
423  ANA_MSG_FATAL ("AnaToolHandle has already been initialized: " << *this);
424  std::abort();
425  }
426 #endif
427 
428  ANA_MSG_DEBUG ("adding sub-tool from ToolHandle: " << value);
429  if (value.empty())
430  return setProperty (property, std::string ());
431 #ifndef XAOD_STANDALONE
432  else if (value.isPublic())
433 #else
434  else if (value.parentName() == "ToolSvc")
435 #endif
436  {
437  return setProperty (property,
438  value.type() + "/" + value.name());
439  } else
440  {
441 #ifndef XAOD_STANDALONE
442  AsgToolConfig subToolConfig;
443  ANA_CHECK (detail::readToolConfig (subToolConfig, value.parentName() + "." + value.name()));
444  ANA_CHECK (m_config.addPrivateTool (property, subToolConfig));
445  return StatusCode::SUCCESS;
446 #else
447  return setProperty (property, value.name());
448 #endif
449  }
450  }
451 
452 
453 
454  template<typename T> template<typename T2>
455  StatusCode AnaToolHandle<T> ::
456  setProperty (const std::string& property,
457  const AnaToolHandle<T2>& value)
458  {
459  using namespace msgToolHandle;
460 
461 #ifndef NDEBUG
462  this->testInvariant ();
463  if (isInitialized())
464  {
465  ANA_MSG_FATAL ("AnaToolHandle has already been initialized: " << *this);
466  std::abort();
467  }
468 #endif
469 
470  // once initialized the AnaToolHandle is not that different from
471  // a regular ToolHandle and we just use it as such
472  if (value.isInitialized())
473  return setProperty (property, value.getHandle());
474 
475  ANA_MSG_DEBUG ("adding sub-tool from AnaToolHandle with mode " << unsigned (value.mode()) << " and name " << value.name());
476  switch (value.mode())
477  {
478  case detail::AnaToolHandleMode::EMPTY:
479  return setProperty (property, ToolHandle<T2> ());
480  case detail::AnaToolHandleMode::CREATE_PRIVATE:
481  return m_config.addPrivateTool (property, value.config());
482  case detail::AnaToolHandleMode::CREATE_SHARED:
483  case detail::AnaToolHandleMode::RETRIEVE_SHARED:
484  m_extraInit.emplace_back ([toolhandle = value] () mutable -> StatusCode
485  {
486  ANA_CHECK (toolhandle.initialize());
487  return StatusCode::SUCCESS;
488  });
489  return m_config.setProperty (property, value.name());
490  case detail::AnaToolHandleMode::USER:
491  return setProperty (property, value.getHandle());
492  }
493  return StatusCode::FAILURE; //compiler dummy
494  }
495 
496 
497 
498  template<typename T> template<typename T2>
499  typename std::enable_if<std::is_base_of_v<parentType_t, T2>, StatusCode>::type
500  AnaToolHandle<T> ::
501  setProperty (const std::string& property,
502  const ToolHandleArray<T2>& value)
503  {
504  using namespace msgToolHandle;
505 
506 #ifndef NDEBUG
507  this->testInvariant ();
508  if (isInitialized())
509  {
510  ANA_MSG_FATAL ("AnaToolHandle has already been initialized: " << *this);
511  std::abort();
512  }
513 #endif
514 
515  std::vector<std::string> tools;
516  // loop over toolhandles in array, strip off any parent naming
517  // and set property with the result
518  for (auto& toolHandle : value)
519  {
520  if (toolHandle.empty())
521  {
522  ANA_MSG_ERROR ("trying to initialize ToolHandleArray property " << property << " with empty handle");
523  return StatusCode::FAILURE;
524  }
525 
526  tools.push_back (toolHandle.type() + "/" + toolHandle.name());
527  }
528 
529  return setProperty (property, tools);
530  }
531 
532 
533 
534  template<class T> StatusCode
535  AnaToolHandle<T> ::
536  initialize ()
537  {
538  using namespace msgToolHandle;
539 
540  // ensure we don't initialize twice concurrently
541  std::lock_guard<std::recursive_mutex> lock (m_initializeMutex);
542 
543 #ifndef NDEBUG
544  this->testInvariant ();
545  if (isInitialized())
546  {
547  ANA_MSG_FATAL ("AnaToolHandle has already been initialized: " << *this);
548  std::abort();
549  }
550 #endif
551 
552  std::shared_ptr<detail::AnaToolShare> sharedTool;
553  const detail::AnaToolHandleMode mode = getMode (sharedTool);
554 
555  ToolHandle<T> th (typeAndName(), m_parentPtr);
556  std::shared_ptr<void> cleanup;
557 
558  switch (mode)
559  {
560  case detail::AnaToolHandleMode::EMPTY:
561  break;
562  case detail::AnaToolHandleMode::CREATE_PRIVATE:
563  for (auto& extraInit : m_extraInit)
564  ANA_CHECK (extraInit());
565  ANA_CHECK (m_config.makeTool (th, cleanup, true));
566  break;
567  case detail::AnaToolHandleMode::CREATE_SHARED:
568  ANA_CHECK (detail::AnaToolShareList::singleton().makeShare (m_name, m_config, std::move (m_extraInit), sharedTool));
569  // FALLTHROUGH
570  case detail::AnaToolHandleMode::RETRIEVE_SHARED:
571  assert (sharedTool != nullptr);
572  ANA_CHECK (detail::toolHandleCast (th, sharedTool->th()));
573 #ifndef XAOD_STANDALONE
574  if (!th.empty())
575  th->release ();
576 #endif
577  cleanup = sharedTool;
578  break;
579  case detail::AnaToolHandleMode::USER:
580  th = *m_handleUser;
581  if (th.empty() && !m_allowEmpty)
582  {
583  ANA_MSG_ERROR ("user configured an empty handle for a non-empty AnaToolHandle: " << *this);
584  return StatusCode::FAILURE;
585  }
586  break;
587  }
588 
589  T *toolPtr = nullptr;
590  ANA_CHECK (makeToolRetrieve (toolPtr, th));
591 
592  *m_handleUser = th;
593 #ifndef XAOD_STANDALONE
594  if (!th.empty())
595  th->release ();
596 #endif
597  m_mode = mode;
598  std::swap (m_cleanup, cleanup);
599  m_toolPtr = toolPtr;
600  m_isInitialized = true;
601 
602 #ifndef NDEBUG
603  this->testInvariant ();
604 #endif
605  return StatusCode::SUCCESS;
606  }
607 
608 
609 
610  template<class T> StatusCode
611  AnaToolHandle<T> ::
612  retrieve ()
613  {
614  // no invariant used
615  return initialize ();
616  }
617 
618 
619  template<class T> bool
620  AnaToolHandle<T> ::
621  isConfigurable () const
622  {
623  using namespace msgToolHandle;
624 #ifndef NDEBUG
625  this->testInvariant ();
626 #endif
627  switch (getMode())
628  {
629  case detail::AnaToolHandleMode::EMPTY:
630  case detail::AnaToolHandleMode::CREATE_PRIVATE:
631  case detail::AnaToolHandleMode::CREATE_SHARED:
632  return true;
633  case detail::AnaToolHandleMode::RETRIEVE_SHARED:
634  case detail::AnaToolHandleMode::USER:
635  return false;
636  }
637  return true; //compiler dummy
638  }
639 
640 
641 
642  template<class T>
643 #ifndef NDEBUG
644  inline
645 #endif
646  bool AnaToolHandle<T> ::
647  isInitialized () const noexcept
648  {
649 #ifndef NDEBUG
650  this->testInvariant ();
651 #endif
652  return m_isInitialized;
653  }
654 
655 
656 
657  template<class T> inline T *
658  AnaToolHandle<T> ::
659  operator -> ()
660  {
661  // no invariant used
662  return get ();
663  }
664 
665 
666 
667  template<class T> inline const T *
668  AnaToolHandle<T> ::
669  operator -> () const
670  {
671  // no invariant used
672  return get ();
673  }
674 
675 
676 
677  template<class T> inline T&
678  AnaToolHandle<T> ::
679  operator * ()
680  {
681  // no invariant used
682  return *get ();
683  }
684 
685 
686 
687  template<class T> inline const T&
688  AnaToolHandle<T> ::
689  operator * () const
690  {
691  // no invariant used
692  return *get ();
693  }
694 
695 
696 
697  template<class T> inline T*
698  AnaToolHandle<T> ::
699  get ()
700  {
701  using namespace msgToolHandle;
702 
703 #ifndef NDEBUG
704  this->testInvariant ();
705 #endif
706 
707  if (m_isInitialized.load())
708  return m_toolPtr;
709 
710  std::lock_guard<std::recursive_mutex> lock (m_initializeMutex);
711  if (!m_isInitialized)
712  ANA_CHECK_THROW (initialize());
713  assert (m_isInitialized);
714  return m_toolPtr;
715  }
716 
717 
718 
719  template<class T> inline const T*
720  AnaToolHandle<T> ::
721  get () const
722  {
723  AnaToolHandle<T>* this_nc ATLAS_THREAD_SAFE = const_cast<AnaToolHandle<T>*>(this);
724  return this_nc->get();
725  }
726 
727 
728 
729  template <class T> template<typename T2> void
730  AnaToolHandle<T> ::
731  declarePropertyFor (T2 *tool, const std::string& name,
732  const std::string& description)
733  {
734  using namespace msgToolHandle;
735 #ifndef NDEBUG
736  this->testInvariant ();
737  if (isInitialized())
738  {
739  ANA_MSG_FATAL ("can't declare tool handle " << *this << " as property " << name << " after tool has been instantiated");
740  std::abort ();
741  }
742 #endif
743 
744  if (m_parentPtr != nullptr && m_parentPtr != tool)
745  {
746  ANA_MSG_FATAL ("can't declare tool handle " << *this << " as property for tool " << tool->name() << " as it has a different parent tool " << m_parentPtr->name());
747  std::abort ();
748  }
749  if (m_originalTypeAndName.empty())
750  m_originalTypeAndName = m_handleUser->typeAndName ();
751  ANA_CHECK_THROW (tool->declareProperty (name, *m_handleUser, description));
752  }
753 
754 
755 
756 
757 
758 
759  template <class T> const ToolHandle<T>&
760  AnaToolHandle<T> ::
761  getHandle () const noexcept
762  {
763 #ifndef NDEBUG
764  this->testInvariant ();
765 #endif
766  return *m_handleUser;
767  }
768 
769 
770 
771  template <class T> bool
772  AnaToolHandle<T> ::
773  isUserConfigured () const noexcept
774  {
775  using namespace msgToolHandle;
776 #ifndef NDEBUG
777  this->testInvariant ();
778 #endif
779 
780  switch (getMode())
781  {
782  case detail::AnaToolHandleMode::EMPTY:
783  case detail::AnaToolHandleMode::CREATE_PRIVATE:
784  case detail::AnaToolHandleMode::CREATE_SHARED:
785  return false;
786  case detail::AnaToolHandleMode::RETRIEVE_SHARED:
787  case detail::AnaToolHandleMode::USER:
788  return true;
789  }
790  return false; //compiler dummy
791  }
792 
793 
794 
795  template<class T> std::string
796  AnaToolHandle<T> ::
797  fullName () const
798  {
799 #ifndef NDEBUG
800  this->testInvariant ();
801 #endif
802 #ifndef XAOD_STANDALONE
803  return m_handleUser->parentName() + "." + name();
804 #else
805  std::string toolName;
806  if (m_parentPtr)
807  toolName = m_parentPtr->name() + "." + name();
808  else
809  toolName = "ToolSvc." + name();
810  return toolName;
811 #endif
812  }
813 
814 
815 
816  template<class T> detail::AnaToolHandleMode AnaToolHandle<T> ::
817  getMode (std::shared_ptr<detail::AnaToolShare>& sharedTool) const
818  {
819  using namespace msgToolHandle;
820 #ifndef NDEBUG
821  this->testInvariant ();
822 #endif
823 
824  assert (!m_isInitialized);
825 
826  if (m_handleUser->isSet())
827  return detail::AnaToolHandleMode::USER;
828 
829  if (m_config.empty() && m_name.empty())
830  return detail::AnaToolHandleMode::EMPTY;
831 
832  if (!m_originalTypeAndName.empty() &&
833  m_handleUser->typeAndName() != m_originalTypeAndName)
834  return detail::AnaToolHandleMode::USER;
835 
836 #ifdef XAOD_STANDALONE
837  if (m_parentPtr != nullptr)
838  {
839  if (m_handleUser->parentName() != m_parentPtr->name())
840  return detail::AnaToolHandleMode::USER;
841  } else
842  {
843  if (m_handleUser->parentName() != "ToolSvc")
844  return detail::AnaToolHandleMode::USER;
845  }
846 #endif
847 
848  if (isPublic())
849  {
850  if ((sharedTool = detail::AnaToolShareList::singleton()
851  .getShare (name())))
852  return detail::AnaToolHandleMode::RETRIEVE_SHARED;
853 #ifdef XAOD_STANDALONE
854  /// \todo check whether this is actually what we want to do
855  if (ToolStore::contains<T> (m_handleUser->name()))
856  return detail::AnaToolHandleMode::USER;
857 #endif
858  }
859 
860 #ifndef XAOD_STANDALONE
861  //for athena, all we do here is check if the tool already exists
862  interfaceType_t *tool = nullptr;
863  if( detail::toolExists( fullName(), tool ) )
864  return detail::AnaToolHandleMode::USER;
865 
866  if (detail::hasPropertiesInCatalogue (fullName()))
867  return detail::AnaToolHandleMode::USER;
868 #endif
869 
870  if (m_config.empty() && !m_handleUser->typeAndName().empty() && (m_handleUser->type() != this->type() || m_handleUser->name() != this->name()))
871  return detail::AnaToolHandleMode::USER;
872 
873  if (isPublic())
874  return detail::AnaToolHandleMode::CREATE_SHARED;
875  else
876  return detail::AnaToolHandleMode::CREATE_PRIVATE;
877  }
878 
879 
880 
881  template<class T> detail::AnaToolHandleMode AnaToolHandle<T> ::
882  getMode () const
883  {
884  if (m_isInitialized)
885  return m_mode;
886 
887  std::shared_ptr<detail::AnaToolShare> sharedTool;
888  return getMode (sharedTool);
889  }
890 
891 
892 
893  template<class T> std::string AnaToolHandle<T> ::
894  typeAndName () const
895  {
896 #ifndef NDEBUG
897  this->testInvariant ();
898 #endif
899  return m_config.typeAndName();
900  }
901 
902 
903 
904  template<class T> void AnaToolHandle<T> ::
905  setType (std::string val_type) noexcept
906  {
907  using namespace msgToolHandle;
908 #ifndef NDEBUG
909  this->testInvariant ();
910  if (isInitialized())
911  {
912  ANA_MSG_FATAL ("AnaToolHandle has already been initialized: " << *this);
913  std::abort();
914  }
915 #endif
916 
917  m_config.setType (std::move (val_type));
918  if (m_originalTypeAndName.empty() &&
919  !this->type().empty() && !this->name().empty())
920  m_handleUser->setTypeAndName (this->type() + "/" + this->name());
921 
922 #ifndef NDEBUG
923  this->testInvariant ();
924 #endif
925  }
926 
927 
928 
929  template<class T> void AnaToolHandle<T> ::
930  setName (std::string val_name) noexcept
931  {
932  using namespace msgToolHandle;
933 #ifndef NDEBUG
934  this->testInvariant ();
935  if (isInitialized())
936  {
937  ANA_MSG_FATAL ("AnaToolHandle has already been initialized: " << *this);
938  std::abort();
939  }
940 #endif
941 
942  m_config.setName (val_name);
943  m_name = std::move (val_name);
944  if (m_originalTypeAndName.empty() &&
945  !this->type().empty() && !this->name().empty())
946  m_handleUser->setTypeAndName (this->type() + "/" + this->name());
947 
948 #ifndef NDEBUG
949  this->testInvariant ();
950 #endif
951  }
952 
953 
954 
955  template<class T> void AnaToolHandle<T> ::
956  setTypeAndName (const std::string& val_typeAndName)
957  {
958  using namespace msgToolHandle;
959 #ifndef NDEBUG
960  if (isInitialized())
961  {
962  ANA_MSG_FATAL ("AnaToolHandle has already been initialized: " << *this);
963  std::abort();
964  }
965 #endif
966  auto split = val_typeAndName.find ("/");
967  if (split != std::string::npos)
968  {
969  setTypeAndName (val_typeAndName.substr (0, split),
970  val_typeAndName.substr (split+1));
971  } else
972  {
973  setTypeAndName (val_typeAndName, val_typeAndName);
974  }
975  }
976 
977 
978 
979  template<class T> void AnaToolHandle<T> ::
980  setTypeAndName (std::string val_type, std::string val_name) noexcept
981  {
982  using namespace msgToolHandle;
983 #ifndef NDEBUG
984  if (isInitialized())
985  {
986  ANA_MSG_FATAL ("AnaToolHandle has already been initialized: " << *this);
987  std::abort();
988  }
989 #endif
990  setType (std::move (val_type));
991  setName (std::move (val_name));
992  }
993 
994 
995 
996  template<class T> StatusCode AnaToolHandle<T> ::
997  makeToolRetrieve (T*& toolPtr, ToolHandle<T>& toolHandle) const
998  {
999  using namespace msgToolHandle;
1000 
1001  if (toolHandle.empty())
1002  {
1003  toolPtr = nullptr;
1004  return StatusCode::SUCCESS;
1005  }
1006 
1007  try
1008  {
1009  toolPtr = toolHandle.operator->();
1010  if (toolPtr == nullptr)
1011  {
1012  ANA_MSG_ERROR ("failed to retrieve tool from tool handle " << *m_handleUser);
1013  return StatusCode::FAILURE;
1014  }
1015  return StatusCode::SUCCESS;
1016  } catch (std::exception& e)
1017  {
1018  ANA_MSG_ERROR ("encountered exception during tool retrieval (" << toolHandle << "): " << e.what());
1019  return StatusCode::FAILURE;
1020  }
1021  }
1022 
1023 
1024 
1025  template<class T> detail::AnaToolHandleMode AnaToolHandle<T> ::
1026  mode () const
1027  {
1028 #ifndef NDEBUG
1029  this->testInvariant ();
1030 #endif
1031  return getMode ();
1032  }
1033 
1034 
1035 
1036  template<class T> const AsgToolConfig& AnaToolHandle<T> ::
1037  config () const
1038  {
1039 #ifndef NDEBUG
1040  this->testInvariant ();
1041 #endif
1042  return m_config;
1043  }
1044 
1045 
1046 
1047  template<class T> bool AnaToolHandle<T> ::
1048  allowEmpty () const noexcept
1049  {
1050 #ifndef NDEBUG
1051  this->testInvariant ();
1052 #endif
1053  return m_allowEmpty;
1054  }
1055 
1056 
1057 
1058  template<class T> void AnaToolHandle<T> ::
1059  setAllowEmpty (bool val_allowEmpty) noexcept
1060  {
1061  using namespace msgToolHandle;
1062 #ifndef NDEBUG
1063  this->testInvariant ();
1064  if (isInitialized())
1065  {
1066  ANA_MSG_FATAL ("AnaToolHandle has already been initialized: " << *this);
1067  std::abort();
1068  }
1069 #endif
1070  m_allowEmpty = val_allowEmpty;
1071  }
1072 }
1073 
1074 #endif // ASGTOOLS_ANATOOLHANDLE_ICC