ATLAS Offline Software
Loading...
Searching...
No Matches
AnaToolHandle.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
4
6
7
8
9#ifndef ASG_TOOLS__ANA_TOOL_HANDLE_H
10#define ASG_TOOLS__ANA_TOOL_HANDLE_H
11
12// Author: Will Buttinger
13
14
15#include <AsgTools/AsgTool.h>
17#include <AsgTools/ToolHandle.h>
18#include <type_traits>
19#include <atomic>
20#include <list>
21#include <map>
22#include <mutex>
23
24namespace asg
25{
26#ifdef XAOD_STANDALONE
27 typedef INamedInterface parentType_t;
29#else
30 typedef INamedInterface parentType_t;
31 typedef IAlgTool interfaceType_t;
32#endif
33
34 template<class T> class AnaToolHandle;
35
36 namespace detail
37 {
38 class AnaToolShare;
39
40
41
44
69 }
70
71
72
78 template <typename T>
79 std::ostream& operator << (std::ostream& str, const AnaToolHandle<T>& obj);
80
81
164
165 template<class T>
166 class AnaToolHandle final
167 {
168 //
169 // public interface
170 //
171
175 public:
176 void testInvariant () const;
177
178
189 public:
190 explicit AnaToolHandle (const std::string& val_name = "",
191 parentType_t *val_parent = nullptr);
192
193
197 public:
199
200
211 public:
213
214
218 public:
219 ~AnaToolHandle () noexcept;
220
221
232 public:
233 AnaToolHandle& operator = (const AnaToolHandle<T>& that);
234
235
239 public:
240 AnaToolHandle<T>& operator = (AnaToolHandle<T>&& that);
241
242
246 public:
247 void swap (AnaToolHandle<T>& that) noexcept;
248
249
258 public:
259 bool empty () const;
260
264 public:
265 bool isPublic () const noexcept;
266
271 public:
272 bool isInitialized () const noexcept;
273
274
282 public:
283 template<typename T2> void
284 declarePropertyFor (T2 *tool, const std::string& name,
285 const std::string& description = "");
286
290 public:
291 const ToolHandle<T>& getHandle () const noexcept;
292
293
311 public:
312 // ToolHandle and ToolHandleArray have ctors that can accept a single int,
313 // so use enable_if to avoid ambiguities.
314 template <class T2> StatusCode
315 setProperty (const std::string& property, const T2& value);
316 template <class T2>
317 typename std::enable_if<std::is_base_of_v<parentType_t, T2>, StatusCode>::type
318 setProperty (const std::string& property, const ToolHandle<T2>& value);
319 template <class T2>
320 typename std::enable_if<std::is_base_of_v<parentType_t, T2>, StatusCode>::type
321 setProperty (const std::string& property, const PublicToolHandle<T2>& value);
322 template <class T2> StatusCode
323 setProperty (const std::string& property, const AnaToolHandle<T2>& value);
324 template <class T2>
325 typename std::enable_if<std::is_base_of_v<parentType_t, T2>, StatusCode>::type
326 setProperty (const std::string& property, const ToolHandleArray<T2>& value);
328
329
333 public:
334 const std::string& type () const noexcept;
335
340 public:
341 void setType (std::string val_type) noexcept;
342
343
347 public:
348 const std::string& name () const noexcept;
349
354 public:
355 void setName (std::string val_name) noexcept;
356
363 public:
364 std::string fullName () const;
365
366
372 public:
374
381 public:
382 void setTypeAndName (const std::string& val_typeAndName);
383
388 public:
389 void setTypeAndName (std::string val_type, std::string val_name) noexcept;
390
391
398 public:
399 StatusCode initialize ();
400
402 public:
403 StatusCode retrieve ();
404
405
412 public:
413 T *operator -> ();
414
421 public:
422 const T *operator -> () const;
423
429 public:
430 T& operator * ();
431
437 public:
438 const T& operator * () const;
439
446 public:
447 T *get ();
448
455 public:
457
458
467 public:
469
470
480 public:
481 detail::AnaToolHandleMode mode () const;
482
483
493 public:
495
496
506 public:
507 bool allowEmpty () const noexcept;
508
509
510
515 public:
516 void setAllowEmpty (bool val_allowEmpty = true) noexcept;
517
518
519
520 //
521 // deprecated interface
522 //
523
537 public:
539
540 public:
541 [[deprecated("please use isInitialized() instead")]]
542 bool inPremakeState () const noexcept {
543 return !isInitialized();}
544
545 public:
546 [[deprecated("please use isInitialized() instead")]]
547 bool inBrokenState () const noexcept {
548 return false;};
549
550 public:
551 [[deprecated("no longer need to call make()")]]
552 StatusCode make () {
553 return StatusCode::SUCCESS;};
554
555 public:
556 [[deprecated("please use setType() or setTypeAndName() instead")]]
557 StatusCode make (const std::string& val_type) noexcept {
558 if (!val_type.empty()) {
559 if (val_type.find ('/') != std::string::npos)
560 setTypeAndName (val_type);
561 else
562 setType (val_type); }
563 return StatusCode::SUCCESS; };
564
565
566
567 //
568 // private interface
569 //
570
579 private:
580 std::vector<std::function<StatusCode ()>> m_extraInit;
581
585 private:
586 std::shared_ptr<void> m_cleanup;
587
589 private:
591
593 private:
594 std::string m_name;
595
597 private:
599
600
611 private:
612 std::shared_ptr<ToolHandle<T>> m_handleUser;
613
618 private:
620
622 private:
623 std::atomic<bool> m_isInitialized {false};
624
634 private:
635 std::atomic<T*> m_toolPtr = nullptr;
636
642 private:
644
657 private:
659 getMode (std::shared_ptr<detail::AnaToolShare>& sharedTool) const;
660
662 private:
664 getMode () const;
665
666
668 private:
669 bool m_allowEmpty = false;
670
671
677 private:
679 (T*& toolPtr, ToolHandle<T>& toolHandle) const;
680
681
687 private:
688 std::recursive_mutex m_initializeMutex;
689 };
690}
691
693#define ASG_MAKE_ANA_TOOL(handle,type) \
694 (ASG_SET_ANA_TOOL_TYPE(handle,type), StatusCode (StatusCode::SUCCESS))
695
697#define ASG_SET_ANA_TOOL_TYPE(handle,type) \
698 (handle).setType (#type)
699
701
702
703#endif
a modified tool handle that allows its owner to configure new tools from the C++ side
const std::string & type() const noexcept
the type configured for this AnaToolHandle
detail::AnaToolHandleMode m_mode
the value of getMode cached when we initialize the tool
AnaToolHandle(const std::string &val_name="", parentType_t *val_parent=nullptr)
create a tool handle with the given name and parent
~AnaToolHandle() noexcept
standard destructor
void setName(std::string val_name) noexcept
set the value of name
StatusCode setProperty(const std::string &property, const T2 &value)
set the given property of the tool.
const AsgToolConfig & config() const
the detail::AnaToolConfig for this handle
bool inPremakeState() const noexcept
void testInvariant() const
test the invariant of this object
StatusCode make(const std::string &val_type) noexcept
bool isPublic() const noexcept
whether this is a public tool (or tool handle)
StatusCode makeToolRetrieve(T *&toolPtr, ToolHandle< T > &toolHandle) const
make a tool by retrieving the ToolHandle
void setType(std::string val_type) noexcept
set the value of type
void setAllowEmpty(bool val_allowEmpty=true) noexcept
set the value of allowEmpty
AnaToolHandle(AnaToolHandle< T > &&that)
move constructor
void declarePropertyFor(T2 *tool, const std::string &name, const std::string &description="")
declare as property on the given tool
bool inBrokenState() const noexcept
std::shared_ptr< ToolHandle< T > > m_handleUser
the ToolHandle exposed to the user
StatusCode initialize()
initialize the tool
std::atomic< T * > m_toolPtr
the pointer to the tool we use
AsgToolConfig m_config
the configuration for this tool
std::shared_ptr< void > m_cleanup
any stuff we need to release as part of cleanup
detail::AnaToolHandleMode getMode() const
get the mode with which this ToolHandle will be initialized
std::string m_originalTypeAndName
the typeAndName at time of creation
detail::AnaToolHandleMode mode() const
the detail::AnaToolHandleMode for this handle
const std::string & name() const noexcept
the name configured for this AnaToolHandle
bool empty() const
whether this ToolHandle is completely empty, i.e.
void setTypeAndName(const std::string &val_typeAndName)
set the value of type and name
std::string fullName() const
the full name of the tool to be used during tool initialization
bool m_allowEmpty
the value of allowEmpty
AnaToolHandle(const AnaToolHandle< T > &that)
copy constructor
void swap(AnaToolHandle< T > &that) noexcept
standard swap
bool isConfigurable() const
whether the tool can be configured by us, i.e.
bool isUserConfigured() const
whether this tool handle has been configured by the user
std::string m_name
the value of name
const ToolHandle< T > & getHandle() const noexcept
the tool handle we wrap
std::vector< std::function< StatusCode()> > m_extraInit
any extra initialization we need to do before creating the tool
std::string typeAndName() const
the value of type and name
bool isInitialized() const noexcept
whether initialize has been called successfully, i.e.
std::recursive_mutex m_initializeMutex
a mutex to ensure that we don't call initialize twice concurrently
parentType_t * m_parentPtr
the pointer to the parent
bool allowEmpty() const noexcept
whether the user is allowed to configure this with an empty tool handle
detail::AnaToolHandleMode getMode(std::shared_ptr< detail::AnaToolShare > &sharedTool) const
get the mode with which this ToolHandle will be initialized
std::atomic< bool > m_isInitialized
the value of isInitialized
StatusCode retrieve()
initialize the tool
T * get()
access the tool
an object that can create a AsgTool
Base class for the dual-use tool interface classes.
Definition IAsgTool.h:41
std::string description
glabal timer - how long have I taken so far?
Definition hcg.cxx:93
AnaToolHandleMode
the mode with which an AnaToolHandle object is initialized
@ USER
retrieve a tool from the user tool handle
@ CREATE_SHARED
create a shared tool normally
@ RETRIEVE_SHARED
retrieve a shared tool
@ EMPTY
do not create a tool
@ CREATE_PRIVATE
create a private tool normally
std::ostream & operator<<(std::ostream &str, const AnaToolHandle< T > &obj)
standard output operator
IAlgTool interfaceType_t
INamedInterface parentType_t
STL namespace.