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 // cppcheck-suppress constStatement; false positive
590 T *toolPtr = nullptr;
591 ANA_CHECK (makeToolRetrieve (toolPtr, th));
594 #ifndef XAOD_STANDALONE
599 std::swap (m_cleanup, cleanup);
601 m_isInitialized = true;
604 this->testInvariant ();
606 return StatusCode::SUCCESS;
611 template<class T> StatusCode
616 return initialize ();
620 template<class T> bool
622 isConfigurable () const
624 using namespace msgToolHandle;
626 this->testInvariant ();
630 case detail::AnaToolHandleMode::EMPTY:
631 case detail::AnaToolHandleMode::CREATE_PRIVATE:
632 case detail::AnaToolHandleMode::CREATE_SHARED:
634 case detail::AnaToolHandleMode::RETRIEVE_SHARED:
635 case detail::AnaToolHandleMode::USER:
638 return true; //compiler dummy
647 bool AnaToolHandle<T> ::
648 isInitialized () const noexcept
651 this->testInvariant ();
653 return m_isInitialized;
658 template<class T> inline T *
668 template<class T> inline const T *
678 template<class T> inline T&
688 template<class T> inline const T&
698 template<class T> inline T*
702 using namespace msgToolHandle;
705 this->testInvariant ();
708 if (m_isInitialized.load())
711 std::lock_guard<std::recursive_mutex> lock (m_initializeMutex);
712 if (!m_isInitialized)
713 ANA_CHECK_THROW (initialize());
714 assert (m_isInitialized);
720 template<class T> inline const T*
724 AnaToolHandle<T>* this_nc ATLAS_THREAD_SAFE = const_cast<AnaToolHandle<T>*>(this);
725 return this_nc->get();
730 template <class T> template<typename T2> void
732 declarePropertyFor (T2 *tool, const std::string& name,
733 const std::string& description)
735 using namespace msgToolHandle;
737 this->testInvariant ();
740 ANA_MSG_FATAL ("can't declare tool handle " << *this << " as property " << name << " after tool has been instantiated");
745 if (m_parentPtr != nullptr && m_parentPtr != tool)
747 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());
750 if (m_originalTypeAndName.empty())
751 m_originalTypeAndName = m_handleUser->typeAndName ();
752 ANA_CHECK_THROW (tool->declareProperty (name, *m_handleUser, description));
760 template <class T> const ToolHandle<T>&
762 getHandle () const noexcept
765 this->testInvariant ();
767 return *m_handleUser;
772 template <class T> bool
774 isUserConfigured () const noexcept
776 using namespace msgToolHandle;
778 this->testInvariant ();
783 case detail::AnaToolHandleMode::EMPTY:
784 case detail::AnaToolHandleMode::CREATE_PRIVATE:
785 case detail::AnaToolHandleMode::CREATE_SHARED:
787 case detail::AnaToolHandleMode::RETRIEVE_SHARED:
788 case detail::AnaToolHandleMode::USER:
791 return false; //compiler dummy
796 template<class T> std::string
801 this->testInvariant ();
803 #ifndef XAOD_STANDALONE
804 return m_handleUser->parentName() + "." + name();
806 std::string toolName;
808 toolName = m_parentPtr->name() + "." + name();
810 toolName = "ToolSvc." + name();
817 template<class T> detail::AnaToolHandleMode AnaToolHandle<T> ::
818 getMode (std::shared_ptr<detail::AnaToolShare>& sharedTool) const
820 using namespace msgToolHandle;
822 this->testInvariant ();
825 assert (!m_isInitialized);
827 if (m_handleUser->isSet())
828 return detail::AnaToolHandleMode::USER;
830 if (m_config.empty() && m_name.empty())
831 return detail::AnaToolHandleMode::EMPTY;
833 if (!m_originalTypeAndName.empty() &&
834 m_handleUser->typeAndName() != m_originalTypeAndName)
835 return detail::AnaToolHandleMode::USER;
837 #ifdef XAOD_STANDALONE
838 if (m_parentPtr != nullptr)
840 if (m_handleUser->parentName() != m_parentPtr->name())
841 return detail::AnaToolHandleMode::USER;
844 if (m_handleUser->parentName() != "ToolSvc")
845 return detail::AnaToolHandleMode::USER;
851 if ((sharedTool = detail::AnaToolShareList::singleton()
853 return detail::AnaToolHandleMode::RETRIEVE_SHARED;
854 #ifdef XAOD_STANDALONE
855 /// \todo check whether this is actually what we want to do
856 if (ToolStore::contains<T> (m_handleUser->name()))
857 return detail::AnaToolHandleMode::USER;
861 #ifndef XAOD_STANDALONE
862 //for athena, all we do here is check if the tool already exists
863 interfaceType_t *tool = nullptr;
864 if( detail::toolExists( fullName(), tool ) )
865 return detail::AnaToolHandleMode::USER;
867 if (detail::hasPropertiesInCatalogue (fullName()))
868 return detail::AnaToolHandleMode::USER;
871 if (m_config.empty() && !m_handleUser->typeAndName().empty() && (m_handleUser->type() != this->type() || m_handleUser->name() != this->name()))
872 return detail::AnaToolHandleMode::USER;
875 return detail::AnaToolHandleMode::CREATE_SHARED;
877 return detail::AnaToolHandleMode::CREATE_PRIVATE;
882 template<class T> detail::AnaToolHandleMode AnaToolHandle<T> ::
888 std::shared_ptr<detail::AnaToolShare> sharedTool;
889 return getMode (sharedTool);
894 template<class T> std::string AnaToolHandle<T> ::
898 this->testInvariant ();
900 return m_config.typeAndName();
905 template<class T> void AnaToolHandle<T> ::
906 setType (std::string val_type) noexcept
908 using namespace msgToolHandle;
910 this->testInvariant ();
913 ANA_MSG_FATAL ("AnaToolHandle has already been initialized: " << *this);
918 m_config.setType (std::move (val_type));
919 if (m_originalTypeAndName.empty() &&
920 !this->type().empty() && !this->name().empty())
921 m_handleUser->setTypeAndName (this->type() + "/" + this->name());
924 this->testInvariant ();
930 template<class T> void AnaToolHandle<T> ::
931 setName (std::string val_name) noexcept
933 using namespace msgToolHandle;
935 this->testInvariant ();
938 ANA_MSG_FATAL ("AnaToolHandle has already been initialized: " << *this);
943 m_config.setName (val_name);
944 m_name = std::move (val_name);
945 if (m_originalTypeAndName.empty() &&
946 !this->type().empty() && !this->name().empty())
947 m_handleUser->setTypeAndName (this->type() + "/" + this->name());
950 this->testInvariant ();
956 template<class T> void AnaToolHandle<T> ::
957 setTypeAndName (const std::string& val_typeAndName)
959 using namespace msgToolHandle;
963 ANA_MSG_FATAL ("AnaToolHandle has already been initialized: " << *this);
967 auto split = val_typeAndName.find ("/");
968 if (split != std::string::npos)
970 setTypeAndName (val_typeAndName.substr (0, split),
971 val_typeAndName.substr (split+1));
974 setTypeAndName (val_typeAndName, val_typeAndName);
980 template<class T> void AnaToolHandle<T> ::
981 setTypeAndName (std::string val_type, std::string val_name) noexcept
983 using namespace msgToolHandle;
987 ANA_MSG_FATAL ("AnaToolHandle has already been initialized: " << *this);
991 setType (std::move (val_type));
992 setName (std::move (val_name));
997 template<class T> StatusCode AnaToolHandle<T> ::
998 makeToolRetrieve (T*& toolPtr, ToolHandle<T>& toolHandle) const
1000 using namespace msgToolHandle;
1002 if (toolHandle.empty())
1005 return StatusCode::SUCCESS;
1010 toolPtr = toolHandle.operator->();
1011 if (toolPtr == nullptr)
1013 ANA_MSG_ERROR ("failed to retrieve tool from tool handle " << *m_handleUser);
1014 return StatusCode::FAILURE;
1016 return StatusCode::SUCCESS;
1017 } catch (std::exception& e)
1019 ANA_MSG_ERROR ("encountered exception during tool retrieval (" << toolHandle << "): " << e.what());
1020 return StatusCode::FAILURE;
1026 template<class T> detail::AnaToolHandleMode AnaToolHandle<T> ::
1030 this->testInvariant ();
1037 template<class T> const AsgToolConfig& AnaToolHandle<T> ::
1041 this->testInvariant ();
1048 template<class T> bool AnaToolHandle<T> ::
1049 allowEmpty () const noexcept
1052 this->testInvariant ();
1054 return m_allowEmpty;
1059 template<class T> void AnaToolHandle<T> ::
1060 setAllowEmpty (bool val_allowEmpty) noexcept
1062 using namespace msgToolHandle;
1064 this->testInvariant ();
1065 if (isInitialized())
1067 ANA_MSG_FATAL ("AnaToolHandle has already been initialized: " << *this);
1071 m_allowEmpty = val_allowEmpty;
1075 #endif // ASGTOOLS_ANATOOLHANDLE_ICC