![]() |
|
|||
File indexing completed on 2025-02-22 10:36:11
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 // ==================================================================== 0012 // SmartDataStorePtr.h 0013 // -------------------------------------------------------------------- 0014 // 0015 // Package : GaudiKernel ( The LHCb Offline System) 0016 // 0017 // Description: Implementation of a smart pointer class to access 0018 // easily (and efficiently) data stores. 0019 // 0020 // Author : M.Frank 0021 // ==================================================================== 0022 #ifndef GAUDIKERNEL_SMARTDATASTOREPTR_H 0023 #define GAUDIKERNEL_SMARTDATASTOREPTR_H 1 0024 0025 // Framework include files 0026 #include "GaudiKernel/SmartDataObjectPtr.h" 0027 0028 /** A small class used to access easily (and efficiently) data items 0029 residing in data stores. 0030 0031 The class constructors take several arguments neccessary to be passed 0032 tyo the data services in order to automatically load objects in case 0033 they are not yet loaded. This is achieved through a smart pointer 0034 mechanism i.e. by overloading the operator->() at dereferencing time 0035 the the object will be requested from the store. 0036 0037 The SmartDataStorePtr is meant to be "short living". It only makes 0038 sense to keep an object instance within e.g. the scope of one method. 0039 "long living" instances do not make sense and in the contrary 0040 would be harmful, because the information passed during construction 0041 to would be invalid and returned object pointers would actually 0042 point to hyperspace. 0043 0044 The intrinsic functionality, wether the object will be retrieved or 0045 loaded from the data store is defined by the LOADER::. 0046 0047 0048 Base Class: 0049 SmartDataObjectPtr 0050 0051 @author M.Frank 0052 @version 1.0 0053 */ 0054 template <class TYPE, class LOADER> 0055 class SmartDataStorePtr : public SmartDataObjectPtr { 0056 public: 0057 /** Standard constructor: Construct an SmartDataStorePtr instance which is 0058 able to connect to a DataObject instance 0059 which is identified by its directory entry. 0060 *** FASTEST ACCESS TO THE DATA STORE *** 0061 @param pService Pointer to the data service interface which 0062 should be used to load the object. 0063 @param pDirectory Pointer to the data directory entry. 0064 */ 0065 SmartDataStorePtr( IDataProviderSvc* pService, IRegistry* pRegistry, std::string path ) 0066 : SmartDataObjectPtr( LOADER::access(), pService, pRegistry, std::move( path ) ), m_pObject( nullptr ) {} 0067 /** Copy constructor: Construct an copy of a SmartDataStorePtr instance. 0068 @param copy Copy of Smart Pointer to object. 0069 */ 0070 SmartDataStorePtr( const SmartDataObjectPtr& copy ) : SmartDataObjectPtr( copy ), m_pObject( nullptr ) {} 0071 0072 /// Standard Destructor 0073 virtual ~SmartDataStorePtr() = default; 0074 0075 /// Automatic conversion to data type 0076 SmartDataStorePtr& operator=( DataObject* pObj ) { 0077 m_pObject = dynamic_cast<TYPE*>( pObj ); 0078 return *this; 0079 } 0080 0081 /// Automatic conversion to data type 0082 SmartDataStorePtr& operator=( const DataObject* pObj ) { 0083 m_pObject = dynamic_cast<TYPE*>( const_cast<DataObject*>( pObj ) ); 0084 return *this; 0085 } 0086 0087 /// unhides assignment operator of base class 0088 SmartDataStorePtr& operator=( const SmartDataObjectPtr& copy ) override { 0089 this->m_pObject = dynamic_cast<TYPE*>( const_cast<SmartDataObjectPtr*>( © ) ); 0090 return *this; 0091 } 0092 0093 /// Automatic conversion to data type 0094 TYPE* ptr() { return accessTypeSafeData(); } 0095 0096 /// Dereference operator: the heart of the smart pointer 0097 TYPE* operator->() { return accessTypeSafeData(); } 0098 0099 /// Dereference operator: the heart of the smart pointer 0100 TYPE& operator*() { 0101 TYPE* result = accessTypeSafeData(); 0102 return *result; 0103 } 0104 0105 /// Automatic conversion to data type 0106 operator TYPE*() { return accessTypeSafeData(); } 0107 0108 /// Automatic conversion to data type 0109 operator TYPE&() { 0110 TYPE* result = accessTypeSafeData(); 0111 return *result; 0112 } 0113 0114 /// operator int for statements like: if ( SmartDataStorePtr<XXX>(...) ) {} 0115 operator int() { return 0 != accessTypeSafeData(); } 0116 0117 /// operator ! for statements like: if ( !SmartDataStorePtr<XXX>(...) ) {} 0118 bool operator!() { return 0 == accessTypeSafeData(); } 0119 0120 /// Internal type safe accessor to data 0121 TYPE* accessTypeSafeData() { 0122 if ( 0 == m_pObject ) { m_pObject = dynamic_cast<TYPE*>( accessData() ); } 0123 return m_pObject; 0124 } 0125 0126 protected: 0127 /// Pointer to data store object 0128 mutable TYPE* m_pObject; 0129 }; 0130 0131 /** Helper to test Smart data objects efficiently 0132 This construct allows statements like: 0133 SmartDataPtr<MCVertexVector> mcvertices (evt,"/MC/MCVertices"); 0134 SmartDataPtr<MCParticleVector> mctracks (evt,"/MC/MCParticles"); 0135 if ( mctracks && mcvertices ) { ... } 0136 and tests the existence of BOTH objects in the data store. 0137 @param object_1 Smart pointer to object 1 0138 @param object_2 Smart pointer to second object 0139 @return Boolean indicating existence of both objects 0140 */ 0141 template <class A, class LDA, class B, class LDB> 0142 bool operator&&( SmartDataStorePtr<A, LDA>& object_1, SmartDataStorePtr<B, LDB>& object_2 ) { 0143 if ( 0 != object_1.accessTypeSafeData() ) { // Test existence of the first object 0144 if ( 0 != object_2.accessTypeSafeData() ) { // Test existence of the second object 0145 return true; // Fine: Both objects exist 0146 } 0147 } 0148 return false; // Tough luck: One is missing. 0149 } 0150 0151 /** Helper to test Smart data objects efficiently 0152 This construct allows statements like: 0153 SmartDataPtr<MCVertexVector> mcvertices (evt,"/MC/MCVertices"); 0154 bool test = ... 0155 if ( test && mcvertices ) { ... } 0156 and tests the existence of BOTH objects in the data store. 0157 @param test First boolean to test 0158 @param object Smart pointer to second object 0159 @return Boolean indicating existence of both objects 0160 */ 0161 template <class B, class LDB> 0162 bool operator&&( bool test, SmartDataStorePtr<B, LDB>& object ) { 0163 if ( test ) { // Test existence of the first object 0164 if ( 0 != object.accessTypeSafeData() ) { // Test existence of the second object 0165 return true; // Fine: Both objects exist 0166 } 0167 } 0168 return false; // Tough luck: One is missing. 0169 } 0170 0171 /** Helper to test Smart data objects efficiently 0172 This construct allows statements like: 0173 SmartDataPtr<MCVertexVector> mcvertices (evt,"/MC/MCVertices"); 0174 bool test = ... 0175 if ( test && mcvertices ) { ... } 0176 and tests the existence of BOTH objects in the data store. 0177 @param object Smart pointer to second object 0178 @param test Second boolean to test 0179 @return Boolean indicating existence of both objects 0180 */ 0181 template <class B, class LDB> 0182 bool operator&&( SmartDataStorePtr<B, LDB>& object, bool test ) { 0183 if ( test ) { // Test existence of the first object 0184 if ( 0 != object.accessTypeSafeData() ) { // Test existence of the second object 0185 return true; // Fine: Both objects exist 0186 } 0187 } 0188 return false; // Tough luck: One is missing. 0189 } 0190 0191 /** Helper to test Smart data objects efficiently 0192 This construct allows statements like: 0193 SmartDataPtr<MCVertexVector> mcvertices (evt,"/MC/MCVertices"); 0194 SmartDataPtr<MCParticleVector> mctracks (evt,"/MC/MCParticles"); 0195 if ( mctracks || mcvertices ) { ... } 0196 and tests the existence of at least one objects in the data store. 0197 The second object will then NOT be loaded. It is assumed that the second 0198 choice is only an alternative usable in case the first object 0199 cannot be retrieved. 0200 0201 @param object_1 Smart pointer to object 1 0202 @param object_2 Smart pointer to second object 0203 @return Boolean indicating existence of both objects 0204 */ 0205 template <class A, class LDA, class B, class LDB> 0206 bool operator||( SmartDataStorePtr<A, LDA>& object_1, SmartDataStorePtr<B, LDB>& object_2 ) { 0207 if ( 0 != object_1.accessTypeSafeData() ) { // Test existence of the first object 0208 return true; 0209 } 0210 if ( 0 != object_2.accessTypeSafeData() ) { // Test existence of the second object 0211 return true; 0212 } 0213 return false; // Tough luck: Both are missing. 0214 } 0215 0216 /** Helper to test Smart data objects efficiently 0217 This construct allows statements like: 0218 SmartDataPtr<MCVertexVector> mcvertices (evt,"/MC/MCVertices"); 0219 bool test = ... 0220 if ( test || mcvertices ) { ... } 0221 and tests the existence of BOTH objects in the data store. 0222 @param test First boolean to test 0223 @param object Smart pointer to second object 0224 @return Boolean indicating existence of both objects 0225 */ 0226 template <class B, class LDB> 0227 bool operator||( bool test, SmartDataStorePtr<B, LDB>& object ) { 0228 if ( test ) { // Test existence of the first object 0229 return true; 0230 } 0231 if ( 0 != object.accessTypeSafeData() ) { // Test existence of the second object 0232 return true; // Fine: Both objects exist 0233 } 0234 return false; // Tough luck: One is missing. 0235 } 0236 0237 /** Helper to test Smart data objects efficiently 0238 This construct allows statements like: 0239 SmartDataPtr<MCVertexVector> mcvertices (evt,"/MC/MCVertices"); 0240 bool test = ... 0241 if ( test && mcvertices ) { ... } 0242 and tests the existence of BOTH objects in the data store. 0243 @param object Smart pointer to second object 0244 @param test Second boolean to test 0245 @return Boolean indicating existence of both objects 0246 */ 0247 template <class B, class LDB> 0248 bool operator||( SmartDataStorePtr<B, LDB>& object, bool test ) { 0249 if ( test ) { // Test existence of the first object 0250 return true; 0251 } 0252 if ( 0 != object.accessTypeSafeData() ) { // Test existence of the second object 0253 return true; // Fine: Both objects exist 0254 } 0255 return false; // Tough luck: One is missing. 0256 } 0257 #endif // GAUDIKERNEL_SMARTDATASTOREPTR_H
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
![]() ![]() |