00001 #ifndef ASG_TOOLS__ANA_TOOL_HANDLE_H
00002 #define ASG_TOOLS__ANA_TOOL_HANDLE_H
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include <AsgTools/AsgTool.h>
00018 #include <AsgTools/Deprecated.h>
00019 #include <AsgTools/ToolHandle.h>
00020 #include <atomic>
00021 #include <list>
00022 #include <map>
00023 #include <mutex>
00024
00025 namespace asg
00026 {
00027 #ifdef ROOTCORE
00028 typedef asg::AsgTool parentType_t;
00029 typedef IAsgTool interfaceType_t;
00030 #else
00031 typedef INamedInterface parentType_t;
00032 typedef IAlgTool interfaceType_t;
00033 #endif
00034
00035 template<class T> class AnaToolHandle;
00036
00037 namespace detail
00038 {
00039 class AnaToolShare;
00040
00043
00044 class AnaToolCleanup
00045 {
00046
00047
00048
00049
00053 public:
00054 void swap (AnaToolCleanup& that);
00055
00056
00067 public:
00068 void addCleanup (AnaToolCleanup val_cleanup, bool post = true);
00069
00071 public:
00072 void addCleanup (const std::shared_ptr<void>& val_cleanup,
00073 bool post = true);
00074
00075
00076
00077
00078
00079
00080
00081 private:
00082 std::list<std::shared_ptr<void> > m_cleanup;
00083 };
00084
00085
00086
00089
00090 class AnaToolProperty
00091 {
00092
00093
00094
00095
00099 public:
00100 virtual ~AnaToolProperty () noexcept = default;
00101
00102 #ifdef ROOTCORE
00110 public:
00111 virtual StatusCode
00112 applyPropertyRootCore (AsgTool& tool, const std::string& name,
00113 AnaToolCleanup& cleanup)
00114 const = 0;
00115 #endif
00116
00117 #ifndef ROOTCORE
00124 public:
00125 virtual StatusCode
00126 applyPropertyAthena (const std::string& toolName,
00127 const std::string& name,
00128 AnaToolCleanup& cleanup)
00129 const = 0;
00130 #endif
00131 };
00132
00133
00134
00141
00142 class AnaToolConfig
00143 {
00144
00145
00146
00147
00151 public:
00152 void swap (AnaToolConfig& that) noexcept;
00153
00154
00158 public:
00159 bool empty () const noexcept;
00160
00161
00165 public:
00166 const std::string& type () const noexcept;
00167
00171 public:
00172 void setType (std::string type) noexcept;
00173
00174 #ifdef ROOTCORE
00186 public:
00187 template<typename Type>
00188 void registerNew ();
00189 #endif
00190
00191
00199 public:
00200 template<typename Type> StatusCode
00201 setProperty (const std::string& val_name, const Type& val_value);
00202
00204 public:
00205 template<typename Type> StatusCode
00206 setProperty (const std::string& val_name,
00207 const AnaToolHandle<Type>& val_value);
00208
00209 #ifndef ROOTCORE
00211 public:
00212 template<typename Type> StatusCode
00213 setProperty (const std::string& val_name,
00214 const ToolHandle<Type>& val_value);
00215
00217 public:
00218 template<typename Type> StatusCode
00219 setProperty (const std::string& val_name,
00220 const ToolHandleArray<Type>& val_value);
00221 #endif
00222
00223
00225 public:
00226 StatusCode
00227 setProperty (const std::string& val_name,
00228 const char *val_value);
00229
00230
00236 public:
00237 void addProperty (const std::string& name,
00238 const std::shared_ptr<AnaToolProperty>& property);
00239
00240
00246 public:
00247 template<typename ToolType> StatusCode
00248 makeTool (const std::string& name,
00249 parentType_t *parent,
00250 ToolHandle<ToolType>& th,
00251 AnaToolCleanup& cleanup) const;
00252
00253
00259 public:
00260 StatusCode
00261 makeBaseTool (const std::string& name,
00262 parentType_t *parent,
00263 ToolHandle<interfaceType_t>& th,
00264 AnaToolCleanup& cleanup) const;
00265
00266
00267 #ifndef ROOTCORE
00274 public:
00275 StatusCode
00276 applyPropertiesAthena (const std::string& toolName,
00277 AnaToolCleanup& cleanup) const;
00278 #endif
00279
00280
00281
00282
00283
00284
00285
00287 private:
00288 std::string m_type;
00289
00291 private:
00292 std::function<StatusCode (AsgTool*&, const std::string&)> m_factory;
00293
00295 private:
00296 std::map<std::string,std::shared_ptr<AnaToolProperty> > m_properties;
00297
00298
00299 #ifdef ROOTCORE
00306 private:
00307 StatusCode
00308 makeToolRootCore (const std::string& toolName, IAsgTool*& toolPtr,
00309 detail::AnaToolCleanup& cleanup) const;
00310 #endif
00311
00312
00313 #ifdef ROOTCORE
00319 private:
00320 StatusCode
00321 allocateTool (AsgTool*& toolPtr, const std::string& toolName) const;
00322 #endif
00323 };
00324
00325
00326
00329
00330 enum class AnaToolHandleMode
00331 {
00337 EMPTY,
00338
00340 CREATE_PRIVATE,
00341
00343 CREATE_SHARED,
00344
00346 RETRIEVE_SHARED,
00347
00352 USER
00353 };
00354 }
00355
00356
00357
00363 template <typename T>
00364 std::ostream& operator << (std::ostream& str, const AnaToolHandle<T>& obj);
00365
00366
00449
00450 template<class T>
00451 class AnaToolHandle final
00452 {
00453
00454
00455
00456
00460 public:
00461 void testInvariant () const;
00462
00463
00474 public:
00475 explicit AnaToolHandle (const std::string& val_name = "",
00476 parentType_t *val_parent = nullptr);
00477
00478
00482 public:
00483 AnaToolHandle (AnaToolHandle<T>&& that);
00484
00485
00496 public:
00497 AnaToolHandle (const AnaToolHandle<T>& that);
00498
00499
00503 public:
00504 ~AnaToolHandle () noexcept;
00505
00506
00517 public:
00518 AnaToolHandle& operator = (const AnaToolHandle<T>& that);
00519
00520
00524 public:
00525 AnaToolHandle<T>& operator = (AnaToolHandle<T>&& that);
00526
00527
00531 public:
00532 void swap (AnaToolHandle<T>& that) noexcept;
00533
00534
00543 public:
00544 bool empty () const;
00545
00549 public:
00550 bool isPublic () const noexcept;
00551
00556 public:
00557 bool isInitialized () const noexcept;
00558
00559
00567 public:
00568 template<typename T2> void
00569 declarePropertyFor (T2 *tool, const std::string& name,
00570 const std::string& description = "");
00571
00575 public:
00576 const ToolHandle<T>& getHandle () const noexcept;
00577
00578
00595 public:
00596 template <class T2> StatusCode
00597 setProperty (const std::string& property, const T2& value);
00598
00599
00603 public:
00604 const std::string& type () const noexcept;
00605
00610 public:
00611 void setType (std::string val_type) noexcept;
00612
00613 #ifdef ROOTCORE
00626 public:
00627 template<class T2>
00628 void setTypeRegisterNew (std::string val_type);
00629 #endif
00630
00631
00635 public:
00636 const std::string& name () const noexcept;
00637
00642 public:
00643 void setName (std::string val_name) noexcept;
00644
00651 public:
00652 std::string fullName () const;
00653
00654
00660 public:
00661 std::string typeAndName () const;
00662
00669 public:
00670 void setTypeAndName (const std::string& val_typeAndName);
00671
00676 public:
00677 void setTypeAndName (std::string val_type, std::string val_name) noexcept;
00678
00679
00686 public:
00687 StatusCode initialize ();
00688
00690 public:
00691 StatusCode retrieve ();
00692
00693
00700 public:
00701 T *operator -> ();
00702
00709 public:
00710 const T *operator -> () const;
00711
00717 public:
00718 T& operator * ();
00719
00725 public:
00726 const T& operator * () const;
00727
00734 public:
00735 T *get ();
00736
00743 public:
00744 const T *get () const;
00745
00746
00755 public:
00756 bool isUserConfigured () const noexcept;
00757
00758
00768 public:
00769 detail::AnaToolHandleMode mode () const;
00770
00771
00781 public:
00782 const detail::AnaToolConfig& config () const;
00783
00784
00794 public:
00795 bool allowEmpty () const noexcept;
00796
00797
00798
00803 public:
00804 void setAllowEmpty (bool val_allowEmpty = true) noexcept;
00805
00806
00807
00808
00809
00810
00811
00825 public:
00826 bool isConfigurable () const;
00827
00828 public:
00829 ASG_DEPRECATED ("please use isInitialized() instead")
00830 bool inPremakeState () const noexcept {
00831 return !isInitialized();}
00832
00833 public:
00834 ASG_DEPRECATED ("please use isInitialized() instead")
00835 bool inBrokenState () const noexcept {
00836 return false;};
00837
00838 public:
00839 ASG_DEPRECATED ("no longer need to call make()")
00840 StatusCode make () {
00841 return StatusCode::SUCCESS;};
00842
00843 public:
00844 ASG_DEPRECATED ("please use setType() or setTypeAndName() instead")
00845 StatusCode make (std::string val_type) noexcept {
00846 if (!val_type.empty()) {
00847 if (val_type.find ('/') != std::string::npos)
00848 setTypeAndName (val_type);
00849 else
00850 setType (val_type); }
00851 return StatusCode::SUCCESS; };
00852
00853
00854
00855
00856
00857
00858 #ifdef ROOTCORE
00859 public:
00860 template<class T2>
00861 ASG_DEPRECATED ("please use setTypeRegisterNew() instead")
00862 StatusCode makeNew (std::string val_type) {
00863 setTypeRegisterNew<T2> (std::move (val_type));
00864 return StatusCode::SUCCESS;}
00865 #endif
00866
00867
00868
00869
00870
00871
00872
00877 private:
00878 detail::AnaToolCleanup m_cleanup;
00879
00881 private:
00882 detail::AnaToolConfig m_config;
00883
00885 private:
00886 std::string m_name;
00887
00889 private:
00890 std::string m_parentName;
00891
00893 private:
00894 parentType_t *m_parentPtr = nullptr;
00895
00896
00907 private:
00908 std::shared_ptr<ToolHandle<T>> m_handleUser;
00909
00914 private:
00915 std::string m_originalTypeAndName;
00916
00918 private:
00919 std::atomic<bool> m_isInitialized {false};
00920
00930 private:
00931 T *m_toolPtr = nullptr;
00932
00938 private:
00939 detail::AnaToolHandleMode m_mode = detail::AnaToolHandleMode::EMPTY;
00940
00953 private:
00954 detail::AnaToolHandleMode
00955 getMode (std::shared_ptr<detail::AnaToolShare>& sharedTool) const;
00956
00958 private:
00959 detail::AnaToolHandleMode
00960 getMode () const;
00961
00962
00964 private:
00965 bool m_allowEmpty = false;
00966
00967
00973 private:
00974 StatusCode makeToolRetrieve
00975 (T*& toolPtr, ToolHandle<T>& toolHandle) const;
00976
00977
00983 private:
00984 std::recursive_mutex m_initializeMutex;
00985 };
00986 }
00987
00989 #define ASG_MAKE_ANA_TOOL(handle,type) \
00990 (ASG_SET_ANA_TOOL_TYPE(handle,type), StatusCode (StatusCode::SUCCESS))
00991
00993 #ifdef ROOTCORE
00994 #define ASG_SET_ANA_TOOL_TYPE(handle,type) \
00995 (handle).template setTypeRegisterNew<type> (#type)
00996 #else
00997 #define ASG_SET_ANA_TOOL_TYPE(handle,type) \
00998 (handle).setType (#type)
00999 #endif
01000
01001 #include <AsgTools/AnaToolHandle.icc>
01002
01003
01004 #endif