Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:57:40

0001 /***********************************************************************************\
0002 * (c) Copyright 1998-2019 CERN for the benefit of the LHCb and ATLAS collaborations *
0003 *                                                                                   *
0004 * This software is distributed under the terms of the Apache version 2 licence,     *
0005 * copied verbatim in the file "LICENSE".                                            *
0006 *                                                                                   *
0007 * In applying this licence, CERN does not waive the privileges and immunities       *
0008 * granted to it by virtue of its status as an Intergovernmental Organization        *
0009 * or submit itself to any jurisdiction.                                             *
0010 \***********************************************************************************/
0011 #ifndef GAUDIKERNEL_ITOOLSVC_H
0012 #define GAUDIKERNEL_ITOOLSVC_H
0013 
0014 #include "GaudiKernel/IInterface.h"
0015 #include "GaudiKernel/System.h"
0016 #include <functional>
0017 #include <list>
0018 #include <string>
0019 
0020 // Forward declaration
0021 class IAlgTool;
0022 
0023 /** @class IToolSvc IToolSvc.h GaudiKernel/IToolSvc.h
0024  *
0025  * The interface implemented by the IToolSvc base class.
0026  *
0027  * @author G.Corti
0028  */
0029 class GAUDI_API IToolSvc : virtual public IInterface {
0030 public:
0031   /// InterfaceID
0032   DeclareInterfaceID( IToolSvc, 2, 1 );
0033 
0034   // Typedefs
0035   typedef std::list<IAlgTool*> ListTools;
0036 
0037   /** Retrieve tool with tool dependent part of the name automatically
0038    *  assigned. By default a tool will be created if it does not exist,
0039    *  unless otherwise specified. By default it will be a common tool
0040    *  unless a parent is specified. The parent of a common tool is
0041    *  automatically taken as the ToolSvc itself.
0042    *  @param type AlgTool type name
0043    *  @param iid  the unique interface identifier
0044    *  @param tool returned tool
0045    *  @param parent constant reference to the parent (def=none)
0046    *  @param createIf creation flag (def=create if not existing)
0047    */
0048   virtual StatusCode retrieve( std::string_view type, const InterfaceID& iid, IAlgTool*& tool,
0049                                const IInterface* parent = 0, bool createIf = true ) = 0;
0050 
0051   /** Retrieve tool with tool dependent part of the name specified
0052    *  by the requester. By default a tool will be created if it does
0053    *  not exist, unless otherwise specified. By default it will be
0054    *  a common tool unless a parent is specified. The parent of a
0055    *  common tool is automatically taken as the ToolSvc itself.
0056    *  @param type AlgTool type name
0057    *  @param name name to be assigned to tool dependent part of the name
0058    *  @param iid  the unique interface identifier
0059    *  @param tool returned tool
0060    *  @param parent constant reference to parent (def=none)
0061    * @param createIf creation flag (def=create if not existing)
0062    */
0063   virtual StatusCode retrieve( std::string_view type, std::string_view name, const InterfaceID& iid, IAlgTool*& tool,
0064                                const IInterface* parent = 0, bool createIf = true ) = 0;
0065 
0066   /** Get the names of all instances of tools of a given type.
0067    *  @param toolType type of tool
0068    */
0069   virtual std::vector<std::string> getInstances( std::string_view toolType ) = 0;
0070 
0071   /** Get the names all tool instances.
0072    */
0073   virtual std::vector<std::string> getInstances() const = 0;
0074 
0075   /** Get pointers to all tool instances.
0076    */
0077   virtual std::vector<IAlgTool*> getTools() const = 0;
0078 
0079   /** Release the tool
0080    *  @param tool to be released
0081    */
0082   virtual StatusCode releaseTool( IAlgTool* tool ) = 0;
0083 
0084   /** Retrieve specified tool sub-type with tool dependent part of the name
0085    *  automatically assigned. Internally it uses the corresponding
0086    *  IToolSvc::retrieve and does the dynamic casting.
0087    *
0088    *  @code
0089    *
0090    *  IToolSvc* svc  = ... ;
0091    *  IMyTool*  tool = 0   ;
0092    *  StatusCode sc = svc->retrieveTool ( "MyToolType" , tool ) ;
0093    *
0094    *  @endcode
0095    *  For this example public tool of type <c>'MyToolType'</c>
0096    *  will be retrieved from Tool Service (created on demand).
0097    *  The full name of the tool instance is set to be
0098    *  <c>ToolSvc.MyToolType</c>
0099    *
0100    *  @code
0101    *
0102    *  IToolSvc*   svc = ... ;
0103    *  IAlgorithm* alg = ... ;
0104    *  IMyTool*  tool  = 0   ;
0105    *  StatusCode sc   = svc->retrieveTool ( "MyToolType" , tool , alg ) ;
0106    *
0107    *  @endcode
0108    *  For this example the private tool of type <c>'MyToolType'</c>
0109    *  will be retrieved from Tool Service (created on demand).
0110    *  The full name of the tool instance is set to be
0111    *  <c><AlgName>.MyToolType </c>, where
0112    *  <c><AlgName></c> is a name of the algorithm.
0113    *
0114    *
0115    *  @code
0116    *
0117    *  IToolSvc* svc  = ... ;
0118    *  IMyTool*  tool = 0   ;
0119    *  StatusCode sc = svc->retrieveTool ( "MyToolType/MyToolName" , tool ) ;
0120    *
0121    *  @endcode
0122    *  For this example public tool of type <c>'MyToolType'</c>
0123    *  will be retrieved from Tool Service (created on demand).
0124    *  The full name of the tool instance is set to be
0125    *  <c>ToolSvc.MyToolName</c>
0126    *
0127    *  @code
0128    *
0129    *  IToolSvc*   svc = ... ;
0130    *  IAlgorithm* alg = ... ;
0131    *  IMyTool*  tool  = 0   ;
0132    *  StatusCode sc   = svc ->
0133    *                retrieveTool ( "MyToolType/MyToolName" , tool , alg ) ;
0134    *
0135    *  @endcode
0136    *  For this example the private tool of type <c>'MyToolType'</c>
0137    *  will be retrieved from Tool Service (created on demand).
0138    *  The full name of the tool instance is set to be
0139    *  <c>&lt;AlgName&gt;.MyToolName </c>, where
0140    *  <c>&lt;AlgName&gt;</c> is a name of the algorithm.
0141    *
0142    *  @param tool returned tool
0143    *  @param parent constant reference to parent (def=none)
0144    *  @param createIf creation flag (def=create if not existing)
0145    *
0146    */
0147   template <class T>
0148   StatusCode retrieveTool( std::string_view type, T*& tool, const IInterface* parent = nullptr, bool createIf = true ) {
0149     return retrieve( type, T::interfaceID(), (IAlgTool*&)tool, parent, createIf );
0150   }
0151 
0152   /** Retrieve specified tool sub-type with tool dependent part of the name
0153    *  tool dependent part of the name specified by the requester.
0154    *  Internally it uses the corresponding IToolSvc::retrieve and does the
0155    *  dynamic casting.
0156    *
0157    *  @code
0158    *
0159    *  IToolSvc* svc  = ... ;
0160    *  IMyTool*  tool = 0   ;
0161    *  StatusCode sc = svc->retrieveTool ( "MyToolType" ,
0162    *                                      "MyToolName" , tool ) ;
0163    *
0164    *  @endcode
0165    *  For this example public tool of type <c>'MyToolType'</c>
0166    *  will be retrieved from Tool Service (created on demand).
0167    *  The full name of the tool instance is set to be
0168    *  <c>ToolSvc.MyToolName</c>
0169    *
0170    *  @code
0171    *
0172    *  IToolSvc*   svc = ... ;
0173    *  IAlgorithm* alg = ... ;
0174    *  IMyTool*  tool  = 0   ;
0175    *  StatusCode sc   = svc->retrieveTool ( "MyToolType" ,
0176    *                                        "MyToolName" , tool , alg ) ;
0177    *
0178    *  @endcode
0179    *  For this example the private tool of type <c>'MyToolType'</c>
0180    *  will be retrieved from Tool Service (created on demand).
0181    *  The full name of the tool instance is set to be
0182    *  <c>&lt;AlgName&gt;.MyToolName </c>, where
0183    *  <c>&lt;AlgName&gt;</c> is a name of the algorithm.
0184    *
0185    *  If <c>name</c> is empty (<c>""</c>) it is assumed to be equal to the
0186    *  <c>type</c>
0187    *
0188    *  @param type AlgTool type name
0189    *  @param name name to be assigned to tool dependent part of the name
0190    *  @param tool returned tool
0191    *  @param parent constant reference to parent (def=none)
0192    *  @param createIf creation flag (def=create if not existing)
0193    *
0194    */
0195   template <class T>
0196   StatusCode retrieveTool( std::string_view type, std::string_view name, T*& tool, const IInterface* parent = nullptr,
0197                            bool createIf = true ) {
0198     return retrieve( type, name, T::interfaceID(), (IAlgTool*&)tool, parent, createIf );
0199   }
0200 
0201   /**  allow call-backs when a tool is a created
0202    *   or retrieved
0203    *
0204    *   @code
0205    *
0206    *   class myObserver : public ITooSvc::Observer {
0207    *        void onCreate(IAlgTool& tool) {
0208    *            cout << "tool " << tool.name() << " created " << endl;
0209    *        }
0210    *        void onRetrieve(IAlgTool& tool) {
0211    *            cout << "someone requested " << tool.name() <<endl;
0212    *        }
0213    *
0214    *   }
0215    *
0216    *   myObserver *observer = new myObserver;
0217    *   IToolSvc* svc = ...
0218    *   svc->registerObserver(observer);
0219    *
0220    *   @endcode
0221    *
0222    *   IToolSvc implementations will call 'Observer::onCreate' when
0223    *   a tool is created, and 'Observer::onRetrieve' if a tool is
0224    *   retrieved.
0225    *
0226    *   The user is responsible of the life time of the object and must
0227    *   un-register it before it is deleted (e.g. during the finalization).
0228    *
0229    */
0230   class Observer {
0231   public:
0232     virtual ~Observer() {
0233       if ( m_unregister ) m_unregister();
0234     }
0235     void setUnregister( std::function<void()> unregister ) { m_unregister = std::move( unregister ); }
0236 
0237     virtual void onCreate( const IAlgTool* ) {}
0238     virtual void onRetrieve( const IAlgTool* ) {}
0239 
0240   private:
0241     std::function<void()> m_unregister;
0242   };
0243 
0244   virtual void registerObserver( Observer* obs ) = 0;
0245 };
0246 
0247 #endif // GAUDIKERNEL_ITOOLSVC_H