2 Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
5 #ifndef ASGTOOLS_ANATOOLHANDLE_ICC
6 #define ASGTOOLS_ANATOOLHANDLE_ICC
8 #include <AsgTools/MessageCheckAsgTools.h>
9 #include "CxxUtils/checker_macros.h"
13 #ifndef XAOD_STANDALONE
14 #include "Gaudi/Interfaces/IOptionsSvc.h"
22 #ifdef XAOD_STANDALONE
23 StatusCode makeToolRootCore (const std::string& type,
24 const std::string& name,
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 );
37 /// \brief do a checked cast from one ToolHandle interface to
42 /// interface not supported for tool
43 template<typename T1,typename T2> StatusCode
44 toolHandleCast (ToolHandle<T1>& to, ToolHandle<T2>& from)
46 using namespace msgUserCode;
48 #ifdef XAOD_STANDALONE
49 to = ToolHandle<T1> (dynamic_cast<T1*>(&*from));
50 if (!from.empty() && to.operator->() == nullptr)
52 ANA_MSG_ERROR ("failed to cast from type " << typeid(T2).name() << " to " << typeid(T1).name());
53 return StatusCode::FAILURE;
56 to = ToolHandle<T1> (from.typeAndName(), from.parent());
58 ANA_CHECK (to.retrieve());
60 return StatusCode::SUCCESS;
67 /// \brief the information needed to share a tool
75 /// \brief standard constructor
81 AnaToolShare (const ToolHandle<interfaceType_t>& val_th,
82 std::shared_ptr<void> val_cleanup,
83 std::vector<std::function<StatusCode ()>>&& extraInit);
86 /// \brief the tool we contain
89 /// \post result != nullptr
91 ToolHandle<interfaceType_t>& th ();
99 /// \brief the value of \ref tool
101 ToolHandle<interfaceType_t> m_th;
103 /// \brief resources to release when we are releasing the tool
105 std::vector<std::function<StatusCode ()>> m_extraInit;
107 /// \brief resources to release when we are releasing the tool
109 std::shared_ptr<void> m_cleanup;
116 /// \brief a service for sharing tools of a given type
118 class AnaToolShareList
124 /// \brief the singleton we are using
128 static AnaToolShareList& singleton () noexcept;
131 /// \brief get the share for the given name, or nullptr if no
132 /// share has been defined
138 std::shared_ptr<AnaToolShare>
139 getShare (const std::string& name) const;
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
150 std::shared_ptr<AnaToolShare>
151 setShare (const std::string& name,
152 std::unique_ptr<AnaToolShare> val_share);
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
161 /// tool creation failures\n
165 makeShare (const std::string& name,
166 const AsgToolConfig& config,
167 std::vector<std::function<StatusCode ()>>&& extraInit,
168 std::shared_ptr<AnaToolShare>& result);
176 /// \brief the shares we manage
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;
186 template <typename T>
187 std::ostream& operator << (std::ostream& str, const AnaToolHandle<T>& obj)
189 return str << obj.getHandle();
194 template<class T> void AnaToolHandle<T> ::
195 testInvariant () const
197 #define CHECK_INVARIANT(x) \
198 if (!(x)) { std::cerr << __FILE__ << ":" << __LINE__ << ": invariant violated: " << #x << std::endl; std::abort(); }
200 // General requirements
201 CHECK_INVARIANT (m_handleUser != nullptr);
202 CHECK_INVARIANT (m_name == m_config.name());
204 #undef CHECK_INVARIANT
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())
215 setTypeAndName (val_name);
218 this->testInvariant ();
224 template<class T> AnaToolHandle<T> ::
225 AnaToolHandle (AnaToolHandle<T>&& that)
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)
243 if (m_isInitialized.load())
245 m_toolPtr = that.m_toolPtr;
246 m_mode = that.m_mode;
247 m_extraInit = that.m_extraInit;
248 m_cleanup = that.m_cleanup;
251 #ifndef XAOD_STANDALONE
252 if (!m_handleUser->empty())
253 (*m_handleUser)->release();
257 that.testInvariant ();
264 template<class T> AnaToolHandle<T>& AnaToolHandle<T> ::
265 operator = (const AnaToolHandle<T>& that)
268 AnaToolHandle<T> (that).swap (*this);
274 template<class T> void AnaToolHandle<T> ::
275 swap (AnaToolHandle<T>& that) noexcept
279 that.testInvariant ();
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);
288 ToolHandle<T> tmp = *m_handleUser;
289 #ifndef XAOD_STANDALONE
293 *m_handleUser = *that.m_handleUser;
294 *that.m_handleUser = tmp;
296 m_originalTypeAndName.swap (that.m_originalTypeAndName);
298 const auto tmp = m_isInitialized.load();
299 m_isInitialized = that.m_isInitialized.load();
300 that.m_isInitialized = tmp;
302 std::swap (m_toolPtr, that.m_toolPtr);
303 std::swap (m_mode, that.m_mode);
304 std::swap (m_allowEmpty, that.m_allowEmpty);
308 that.testInvariant ();
314 template<class T> AnaToolHandle<T>& AnaToolHandle<T> ::
315 operator = (AnaToolHandle<T>&& that)
324 template<class T> AnaToolHandle<T> ::
325 ~AnaToolHandle () noexcept
327 using namespace msgToolHandle;
329 this->testInvariant ();
334 template<class T> bool
339 this->testInvariant ();
343 case detail::AnaToolHandleMode::EMPTY:
345 case detail::AnaToolHandleMode::CREATE_PRIVATE:
346 case detail::AnaToolHandleMode::CREATE_SHARED:
347 case detail::AnaToolHandleMode::RETRIEVE_SHARED:
349 case detail::AnaToolHandleMode::USER:
350 return m_handleUser->empty();
352 return false; //compiler dummy
357 template<class T> inline bool AnaToolHandle<T> ::
358 isPublic () const noexcept
361 this->testInvariant ();
363 return m_parentPtr == nullptr;
368 template<class T> inline const std::string& AnaToolHandle<T> ::
369 type () const noexcept
372 this->testInvariant ();
374 return m_config.type();
379 template<class T> inline const std::string&
381 name () const noexcept
384 this->testInvariant ();
391 template<typename T> template<typename T2>
392 StatusCode AnaToolHandle<T> ::
393 setProperty (const std::string& property, const T2& value)
395 using namespace msgToolHandle;
398 this->testInvariant ();
401 ANA_MSG_FATAL ("AnaToolHandle has already been initialized: " << *this);
406 return m_config.setProperty (property, value);
411 template<typename T> template<typename T2>
412 typename std::enable_if<std::is_base_of_v<parentType_t, T2>, StatusCode>::type
414 setProperty (const std::string& property,
415 const ToolHandle<T2>& value)
417 using namespace msgToolHandle;
420 this->testInvariant ();
423 ANA_MSG_FATAL ("AnaToolHandle has already been initialized: " << *this);
428 ANA_MSG_DEBUG ("adding sub-tool from ToolHandle: " << value);
430 return setProperty (property, std::string ());
431 #ifndef XAOD_STANDALONE
432 else if (value.isPublic())
434 else if (value.parentName() == "ToolSvc")
437 return setProperty (property,
438 value.type() + "/" + value.name());
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;
447 return setProperty (property, value.name());
454 template<typename T> template<typename T2>
455 StatusCode AnaToolHandle<T> ::
456 setProperty (const std::string& property,
457 const AnaToolHandle<T2>& value)
459 using namespace msgToolHandle;
462 this->testInvariant ();
465 ANA_MSG_FATAL ("AnaToolHandle has already been initialized: " << *this);
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());
475 ANA_MSG_DEBUG ("adding sub-tool from AnaToolHandle with mode " << unsigned (value.mode()) << " and name " << value.name());
476 switch (value.mode())
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
486 ANA_CHECK (toolhandle.initialize());
487 return StatusCode::SUCCESS;
489 return m_config.setProperty (property, value.name());
490 case detail::AnaToolHandleMode::USER:
491 return setProperty (property, value.getHandle());
493 return StatusCode::FAILURE; //compiler dummy
498 template<typename T> template<typename T2>
499 typename std::enable_if<std::is_base_of_v<parentType_t, T2>, StatusCode>::type
501 setProperty (const std::string& property,
502 const ToolHandleArray<T2>& value)
504 using namespace msgToolHandle;
507 this->testInvariant ();
510 ANA_MSG_FATAL ("AnaToolHandle has already been initialized: " << *this);
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)
520 if (toolHandle.empty())
522 ANA_MSG_ERROR ("trying to initialize ToolHandleArray property " << property << " with empty handle");
523 return StatusCode::FAILURE;
526 tools.push_back (toolHandle.type() + "/" + toolHandle.name());
529 return setProperty (property, tools);
534 template<class T> StatusCode
538 using namespace msgToolHandle;
540 // ensure we don't initialize twice concurrently
541 std::lock_guard<std::recursive_mutex> lock (m_initializeMutex);
544 this->testInvariant ();
547 ANA_MSG_FATAL ("AnaToolHandle has already been initialized: " << *this);
552 std::shared_ptr<detail::AnaToolShare> sharedTool;
553 const detail::AnaToolHandleMode mode = getMode (sharedTool);
555 ToolHandle<T> th (typeAndName(), m_parentPtr);
556 std::shared_ptr<void> cleanup;
560 case detail::AnaToolHandleMode::EMPTY:
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));
567 case detail::AnaToolHandleMode::CREATE_SHARED:
568 ANA_CHECK (detail::AnaToolShareList::singleton().makeShare (m_name, m_config, std::move (m_extraInit), sharedTool));
570 case detail::AnaToolHandleMode::RETRIEVE_SHARED:
571 assert (sharedTool != nullptr);
572 ANA_CHECK (detail::toolHandleCast (th, sharedTool->th()));
573 #ifndef XAOD_STANDALONE
577 cleanup = sharedTool;
579 case detail::AnaToolHandleMode::USER:
581 if (th.empty() && !m_allowEmpty)
583 ANA_MSG_ERROR ("user configured an empty handle for a non-empty AnaToolHandle: " << *this);
584 return StatusCode::FAILURE;
589 T *toolPtr = nullptr;
590 ANA_CHECK (makeToolRetrieve (toolPtr, th));
593 #ifndef XAOD_STANDALONE
598 std::swap (m_cleanup, cleanup);
600 m_isInitialized = true;
603 this->testInvariant ();
605 return StatusCode::SUCCESS;
610 template<class T> StatusCode
615 return initialize ();
619 template<class T> bool
621 isConfigurable () const
623 using namespace msgToolHandle;
625 this->testInvariant ();
629 case detail::AnaToolHandleMode::EMPTY:
630 case detail::AnaToolHandleMode::CREATE_PRIVATE:
631 case detail::AnaToolHandleMode::CREATE_SHARED:
633 case detail::AnaToolHandleMode::RETRIEVE_SHARED:
634 case detail::AnaToolHandleMode::USER:
637 return true; //compiler dummy
646 bool AnaToolHandle<T> ::
647 isInitialized () const noexcept
650 this->testInvariant ();
652 return m_isInitialized;
657 template<class T> inline T *
667 template<class T> inline const T *
677 template<class T> inline T&
687 template<class T> inline const T&
697 template<class T> inline T*
701 using namespace msgToolHandle;
704 this->testInvariant ();
707 if (m_isInitialized.load())
710 std::lock_guard<std::recursive_mutex> lock (m_initializeMutex);
711 if (!m_isInitialized)
712 ANA_CHECK_THROW (initialize());
713 assert (m_isInitialized);
719 template<class T> inline const T*
723 AnaToolHandle<T>* this_nc ATLAS_THREAD_SAFE = const_cast<AnaToolHandle<T>*>(this);
724 return this_nc->get();
729 template <class T> template<typename T2> void
731 declarePropertyFor (T2 *tool, const std::string& name,
732 const std::string& description)
734 using namespace msgToolHandle;
736 this->testInvariant ();
739 ANA_MSG_FATAL ("can't declare tool handle " << *this << " as property " << name << " after tool has been instantiated");
744 if (m_parentPtr != nullptr && m_parentPtr != tool)
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());
749 if (m_originalTypeAndName.empty())
750 m_originalTypeAndName = m_handleUser->typeAndName ();
751 ANA_CHECK_THROW (tool->declareProperty (name, *m_handleUser, description));
759 template <class T> const ToolHandle<T>&
761 getHandle () const noexcept
764 this->testInvariant ();
766 return *m_handleUser;
771 template <class T> bool
773 isUserConfigured () const noexcept
775 using namespace msgToolHandle;
777 this->testInvariant ();
782 case detail::AnaToolHandleMode::EMPTY:
783 case detail::AnaToolHandleMode::CREATE_PRIVATE:
784 case detail::AnaToolHandleMode::CREATE_SHARED:
786 case detail::AnaToolHandleMode::RETRIEVE_SHARED:
787 case detail::AnaToolHandleMode::USER:
790 return false; //compiler dummy
795 template<class T> std::string
800 this->testInvariant ();
802 #ifndef XAOD_STANDALONE
803 return m_handleUser->parentName() + "." + name();
805 std::string toolName;
807 toolName = m_parentPtr->name() + "." + name();
809 toolName = "ToolSvc." + name();
816 template<class T> detail::AnaToolHandleMode AnaToolHandle<T> ::
817 getMode (std::shared_ptr<detail::AnaToolShare>& sharedTool) const
819 using namespace msgToolHandle;
821 this->testInvariant ();
824 assert (!m_isInitialized);
826 if (m_handleUser->isSet())
827 return detail::AnaToolHandleMode::USER;
829 if (m_config.empty() && m_name.empty())
830 return detail::AnaToolHandleMode::EMPTY;
832 if (!m_originalTypeAndName.empty() &&
833 m_handleUser->typeAndName() != m_originalTypeAndName)
834 return detail::AnaToolHandleMode::USER;
836 #ifdef XAOD_STANDALONE
837 if (m_parentPtr != nullptr)
839 if (m_handleUser->parentName() != m_parentPtr->name())
840 return detail::AnaToolHandleMode::USER;
843 if (m_handleUser->parentName() != "ToolSvc")
844 return detail::AnaToolHandleMode::USER;
850 if ((sharedTool = detail::AnaToolShareList::singleton()
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;
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;
866 if (detail::hasPropertiesInCatalogue (fullName()))
867 return detail::AnaToolHandleMode::USER;
870 if (m_config.empty() && !m_handleUser->typeAndName().empty() && (m_handleUser->type() != this->type() || m_handleUser->name() != this->name()))
871 return detail::AnaToolHandleMode::USER;
874 return detail::AnaToolHandleMode::CREATE_SHARED;
876 return detail::AnaToolHandleMode::CREATE_PRIVATE;
881 template<class T> detail::AnaToolHandleMode AnaToolHandle<T> ::
887 std::shared_ptr<detail::AnaToolShare> sharedTool;
888 return getMode (sharedTool);
893 template<class T> std::string AnaToolHandle<T> ::
897 this->testInvariant ();
899 return m_config.typeAndName();
904 template<class T> void AnaToolHandle<T> ::
905 setType (std::string val_type) noexcept
907 using namespace msgToolHandle;
909 this->testInvariant ();
912 ANA_MSG_FATAL ("AnaToolHandle has already been initialized: " << *this);
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());
923 this->testInvariant ();
929 template<class T> void AnaToolHandle<T> ::
930 setName (std::string val_name) noexcept
932 using namespace msgToolHandle;
934 this->testInvariant ();
937 ANA_MSG_FATAL ("AnaToolHandle has already been initialized: " << *this);
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());
949 this->testInvariant ();
955 template<class T> void AnaToolHandle<T> ::
956 setTypeAndName (const std::string& val_typeAndName)
958 using namespace msgToolHandle;
962 ANA_MSG_FATAL ("AnaToolHandle has already been initialized: " << *this);
966 auto split = val_typeAndName.find ("/");
967 if (split != std::string::npos)
969 setTypeAndName (val_typeAndName.substr (0, split),
970 val_typeAndName.substr (split+1));
973 setTypeAndName (val_typeAndName, val_typeAndName);
979 template<class T> void AnaToolHandle<T> ::
980 setTypeAndName (std::string val_type, std::string val_name) noexcept
982 using namespace msgToolHandle;
986 ANA_MSG_FATAL ("AnaToolHandle has already been initialized: " << *this);
990 setType (std::move (val_type));
991 setName (std::move (val_name));
996 template<class T> StatusCode AnaToolHandle<T> ::
997 makeToolRetrieve (T*& toolPtr, ToolHandle<T>& toolHandle) const
999 using namespace msgToolHandle;
1001 if (toolHandle.empty())
1004 return StatusCode::SUCCESS;
1009 toolPtr = toolHandle.operator->();
1010 if (toolPtr == nullptr)
1012 ANA_MSG_ERROR ("failed to retrieve tool from tool handle " << *m_handleUser);
1013 return StatusCode::FAILURE;
1015 return StatusCode::SUCCESS;
1016 } catch (std::exception& e)
1018 ANA_MSG_ERROR ("encountered exception during tool retrieval (" << toolHandle << "): " << e.what());
1019 return StatusCode::FAILURE;
1025 template<class T> detail::AnaToolHandleMode AnaToolHandle<T> ::
1029 this->testInvariant ();
1036 template<class T> const AsgToolConfig& AnaToolHandle<T> ::
1040 this->testInvariant ();
1047 template<class T> bool AnaToolHandle<T> ::
1048 allowEmpty () const noexcept
1051 this->testInvariant ();
1053 return m_allowEmpty;
1058 template<class T> void AnaToolHandle<T> ::
1059 setAllowEmpty (bool val_allowEmpty) noexcept
1061 using namespace msgToolHandle;
1063 this->testInvariant ();
1064 if (isInitialized())
1066 ANA_MSG_FATAL ("AnaToolHandle has already been initialized: " << *this);
1070 m_allowEmpty = val_allowEmpty;
1074 #endif // ASGTOOLS_ANATOOLHANDLE_ICC