ATLAS Offline Software
StandaloneToolHandle.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 
7 
8 #ifndef ASG_TOOLS__STANDALONE_TOOL_HANDLE_H
9 #define ASG_TOOLS__STANDALONE_TOOL_HANDLE_H
10 
11 #include <AsgTools/AsgToolConfig.h>
12 
13 namespace asg
14 {
42 
43  template<typename T> class StandaloneToolHandle final
44  {
47 
48  public:
49  StandaloneToolHandle () = default;
50  StandaloneToolHandle (const std::string& typeAndName)
51  : m_config (typeAndName) {}
52 
53 
57 
61 
64  T *get () {
65  checkIsInitialized (); return m_pointer;}
66  const T *get () const {
67  checkIsInitialized (); return m_pointer;}
68  T *operator -> () {
69  checkIsInitialized (); return m_pointer;}
70  const T *operator -> () const {
71  checkIsInitialized (); return m_pointer;}
72  T& operator * () {
73  checkIsInitialized (); return *m_pointer;}
74  const T& operator * () const {
75  checkIsInitialized (); return *m_pointer;}
77 
82  const ToolHandle<std::remove_const_t<T>>& getHandle ();
83 
84 
85 
94 
95  public:
96 
97  auto typeAndName () const {
98  return m_config.typeAndName();}
99  auto name () const {
100  return m_config.name();}
101  void setTypeAndName (const std::string& typeAndName) {
104  template<typename T2>
105  StatusCode setProperty (const std::string& name, T2&& value) {
107  return m_config.setProperty (name, value);}
108  StatusCode createPrivateTool (const std::string& name,
109  const std::string& toolType) {
111  return m_config.createPrivateTool (name, toolType);}
112 
113 
114 
117 
118  private:
119 
121  T *m_pointer {nullptr};
122 
124  ToolHandle<std::remove_const_t<T>> m_toolHandle;
125 
127  std::shared_ptr<void> m_cleanup;
128 
131 
132 
134  void checkNotInitialized () const;
135 
141  void checkIsInitialized () const;
142  };
143 
144 
145 
147  retrieve ()
148  {
149  if (m_pointer == nullptr)
150  return initialize ();
151  else
152  return StatusCode::SUCCESS;
153  }
154 
155 
156 
158  initialize ()
159  {
160  checkNotInitialized ();
161  if (m_config.makeTool (m_toolHandle, m_cleanup, true).isFailure())
162  return StatusCode::FAILURE;
163  m_pointer = &*m_toolHandle;
164  return StatusCode::SUCCESS;
165  }
166 
167 
168 
169  template<typename T> const ToolHandle<std::remove_const_t<T>>& StandaloneToolHandle<T> ::
170  getHandle ()
171  {
172  checkIsInitialized ();
173  return m_toolHandle;
174  }
175 
176 
177 
178  template<typename T> void StandaloneToolHandle<T> ::
179  checkNotInitialized () const
180  {
181  if (m_pointer)
182  throw std::logic_error ("trying to configure StandaloneToolHandle after it was initialized/retrieved");
183  }
184 
185 
186 
187  template<typename T> void StandaloneToolHandle<T> ::
188  checkIsInitialized () const
189  {
190  if (m_pointer == nullptr)
191  throw std::logic_error ("trying to use StandaloneToolHandle that was never initialized/retrieved");
192  }
193 
194 
195 
196  template <typename T>
197  std::ostream& operator << (std::ostream& str, const StandaloneToolHandle<T>& obj)
198  {
199  return str << "StandaloneToolHandle:" << obj.typeAndName();
200  }
201 }
202 
203 #endif
asg::StandaloneToolHandle::checkIsInitialized
void checkIsInitialized() const
check that we are initialized, forcing in-place initialization if necessary
Definition: StandaloneToolHandle.h:188
asg::StandaloneToolHandle::get
const T * get() const
Definition: StandaloneToolHandle.h:66
asg::StandaloneToolHandle::m_toolHandle
ToolHandle< std::remove_const_t< T > > m_toolHandle
the ToolHandle holding the tool after initialization
Definition: StandaloneToolHandle.h:124
asg::StandaloneToolHandle::get
T * get()
access the wrapped tool
Definition: StandaloneToolHandle.h:64
asg::operator<<
std::ostream & operator<<(std::ostream &str, const AnaToolHandle< T > &obj)
standard output operator
initialize
void initialize()
Definition: run_EoverP.cxx:894
asg
Definition: DataHandleTestTool.h:28
athena.value
value
Definition: athena.py:124
asg::StandaloneToolHandle::StandaloneToolHandle
StandaloneToolHandle()=default
asg::StandaloneToolHandle::m_config
AsgToolConfig m_config
the actual configuration of the tool
Definition: StandaloneToolHandle.h:130
asg::StandaloneToolHandle::m_pointer
T * m_pointer
the cached pointer to the tool
Definition: StandaloneToolHandle.h:121
asg::StandaloneToolHandle::initialize
StatusCode initialize()
initialize the tool, will fail if the tool was already initialized
Definition: StandaloneToolHandle.h:158
asg::AsgToolConfig
an object that can create a AsgTool
Definition: AsgToolConfig.h:22
asg::StandaloneToolHandle::createPrivateTool
StatusCode createPrivateTool(const std::string &name, const std::string &toolType)
Definition: StandaloneToolHandle.h:108
asg::StandaloneToolHandle::setProperty
StatusCode setProperty(const std::string &name, T2 &&value)
Definition: StandaloneToolHandle.h:105
asg::StandaloneToolHandle
an "initializing" ToolHandle for stand-alone applications
Definition: StandaloneToolHandle.h:44
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
asg::StandaloneToolHandle::name
auto name() const
Definition: StandaloneToolHandle.h:99
AsgToolConfig.h
asg::StandaloneToolHandle::StandaloneToolHandle
StandaloneToolHandle(const std::string &typeAndName)
Definition: StandaloneToolHandle.h:50
asg::StandaloneToolHandle::operator*
T & operator*()
Definition: StandaloneToolHandle.h:72
asg::StandaloneToolHandle::retrieve
StatusCode retrieve()
initialize the tool, will succeed if the tool was already initialized
Definition: StandaloneToolHandle.h:147
asg::AsgComponentConfig::setTypeAndName
void setTypeAndName(const std::string &val_typeAndName)
set type and name at the same time
Definition: AsgComponentConfig.cxx:116
asg::StandaloneToolHandle::checkNotInitialized
void checkNotInitialized() const
check that we are not initialized yet
Definition: StandaloneToolHandle.h:179
asg::StandaloneToolHandle::operator->
T * operator->()
Definition: StandaloneToolHandle.h:68
asg::StandaloneToolHandle::setTypeAndName
void setTypeAndName(const std::string &typeAndName)
Definition: StandaloneToolHandle.h:101
asg::AsgComponentConfig::setProperty
StatusCode setProperty(const std::string &name, const T &value)
set the given property
asg::AsgComponentConfig::typeAndName
std::string typeAndName() const
get type and name at the same time
Definition: AsgComponentConfig.cxx:106
asg::StandaloneToolHandle::m_cleanup
std::shared_ptr< void > m_cleanup
the tool-cleanup pointer
Definition: StandaloneToolHandle.h:127
str
Definition: BTagTrackIpAccessor.cxx:11
asg::AsgComponentConfig::name
const std::string & name() const noexcept
the name of the component
Definition: AsgComponentConfig.cxx:90
asg::StandaloneToolHandle::typeAndName
auto typeAndName() const
Definition: StandaloneToolHandle.h:97
python.PyAthena.obj
obj
Definition: PyAthena.py:132
asg::StandaloneToolHandle::getHandle
const ToolHandle< std::remove_const_t< T > > & getHandle()
get the contained handle
Definition: StandaloneToolHandle.h:170
asg::AsgComponentConfig::createPrivateTool
StatusCode createPrivateTool(const std::string &name, const std::string &toolType)
create a private tool of the given name and type
Definition: AsgComponentConfig.cxx:150