1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 #ifndef __FRAMEWORK_THREADHELP_TRANSACTIONGUARD_HXX_ 25 #define __FRAMEWORK_THREADHELP_TRANSACTIONGUARD_HXX_ 26 27 //_________________________________________________________________________________________________________________ 28 // my own includes 29 //_________________________________________________________________________________________________________________ 30 31 #include <threadhelp/inoncopyable.h> 32 #include <threadhelp/itransactionmanager.h> 33 34 //_________________________________________________________________________________________________________________ 35 // interface includes 36 //_________________________________________________________________________________________________________________ 37 38 //_________________________________________________________________________________________________________________ 39 // other includes 40 //_________________________________________________________________________________________________________________ 41 42 //_________________________________________________________________________________________________________________ 43 // namespace 44 //_________________________________________________________________________________________________________________ 45 46 namespace framework{ 47 48 //_________________________________________________________________________________________________________________ 49 // const 50 //_________________________________________________________________________________________________________________ 51 52 //_________________________________________________________________________________________________________________ 53 // declarations 54 //_________________________________________________________________________________________________________________ 55 56 /*-************************************************************************************************************//** 57 @short implement a guard to support non breakable transactions 58 @descr If you whish to support non breakable method calls without lockingf any mutex, rw-lock or 59 something like that - you should use this guard implementation. 60 Initialize it at first in your method and don't release it till end of your function! 61 Your "transaction" is registered in ctor and automatically released in dtor. 62 Use set/get of working mode to enable/disable further transactions. 63 It's possible too, to enable automatically throwing of some exceptions for illegal 64 transaction requests ... e.g. interface call for already disposed objects. 65 66 @attention To prevent us against wrong using, the default ctor, copy ctor and the =operator are marked private! 67 68 @implements - 69 @base INonCopyable 70 71 @devstatus draft 72 *//*-*************************************************************************************************************/ 73 class TransactionGuard : private INonCopyable 74 { 75 //------------------------------------------------------------------------------------------------------------- 76 // public methods 77 //------------------------------------------------------------------------------------------------------------- 78 public: 79 80 /*-****************************************************************************************************//** 81 @short ctors 82 @descr Use these ctor methods to initialize the guard right. 83 Given reference must be valid - otherwise crashes could occur! 84 85 @attention It's not necessary to lock any mutex here! Because a ctor should not be called 86 from different threads at the same time ... this class use no refcount mechanism! 87 88 @seealso - 89 90 @param "rManager" reference to transaction manager for using to register a request 91 @param "eMode" enable/disable throwing of exceptions for rejected calls 92 @param "eReason" returns reason for rejected calls if "eMode=E_NOEXCEPTIONS"! 93 @return - 94 95 @onerror - 96 *//*-*****************************************************************************************************/ TransactionGuard(ITransactionManager & rManager,EExceptionMode eMode,ERejectReason * eReason=NULL)97 inline TransactionGuard( ITransactionManager& rManager, EExceptionMode eMode, ERejectReason* eReason = NULL ) 98 : m_pManager( &rManager ) 99 { 100 // If exception mode is set to E_HARDEXCETIONS we don't need a buffer to return reason! 101 // We handle it private. If a call is rejected, our manager throw some exceptions ... and the reason 102 // could be ignorable ... 103 if( eReason == NULL ) 104 { 105 ERejectReason eMyReason; 106 m_pManager->registerTransaction( eMode, eMyReason ); 107 } 108 else 109 { 110 m_pManager->registerTransaction( eMode, *eReason ); 111 } 112 } 113 114 /*-************************************************************************************************************//** 115 @short dtor 116 @descr We must release the transaction manager and can forget his pointer. 117 118 @seealso - 119 120 @param - 121 @return - 122 123 @onerror - 124 *//*-*************************************************************************************************************/ ~TransactionGuard()125 inline ~TransactionGuard() 126 { 127 stop(); 128 } 129 130 /*-************************************************************************************************************//** 131 @short stop current transaction 132 @descr We must release the transaction manager and can forget his pointer. 133 134 @attention We don't support any start() method here - because it is not easy to 135 detect if a transaction already started or not! 136 (combination of EExceptionMode and ERejectReason) 137 138 @seealso - 139 140 @param - 141 @return - 142 143 @onerror - 144 *//*-*************************************************************************************************************/ stop()145 inline void stop() 146 { 147 if( m_pManager != NULL ) 148 { 149 m_pManager->unregisterTransaction(); 150 m_pManager = NULL; 151 } 152 } 153 154 //------------------------------------------------------------------------------------------------------------- 155 // private methods 156 //------------------------------------------------------------------------------------------------------------- 157 private: 158 159 /*-****************************************************************************************************//** 160 @short disable using of these functions! 161 @descr It's not allowed to use this methods. Different problems can occur otherwise. 162 That's why we disable it by making it private. 163 164 @seealso other ctor 165 166 @param - 167 @return - 168 169 @onerror - 170 *//*-*****************************************************************************************************/ 171 TransactionGuard(); 172 173 //------------------------------------------------------------------------------------------------------------- 174 // private member 175 //------------------------------------------------------------------------------------------------------------- 176 private: 177 178 ITransactionManager* m_pManager ; /// pointer to safed transaction manager 179 180 }; // class TransactionGuard 181 182 } // namespace framework 183 184 #endif // #ifndef __FRAMEWORK_THREADHELP_TRANSACTIONGUARD_HXX_ 185