1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 28 #ifndef __FRAMEWORK_THREADHELP_TRANSACTIONGUARD_HXX_ 29 #define __FRAMEWORK_THREADHELP_TRANSACTIONGUARD_HXX_ 30 31 //_________________________________________________________________________________________________________________ 32 // my own includes 33 //_________________________________________________________________________________________________________________ 34 35 #include <threadhelp/inoncopyable.h> 36 #include <threadhelp/itransactionmanager.h> 37 38 //_________________________________________________________________________________________________________________ 39 // interface includes 40 //_________________________________________________________________________________________________________________ 41 42 //_________________________________________________________________________________________________________________ 43 // other includes 44 //_________________________________________________________________________________________________________________ 45 46 //_________________________________________________________________________________________________________________ 47 // namespace 48 //_________________________________________________________________________________________________________________ 49 50 namespace framework{ 51 52 //_________________________________________________________________________________________________________________ 53 // const 54 //_________________________________________________________________________________________________________________ 55 56 //_________________________________________________________________________________________________________________ 57 // declarations 58 //_________________________________________________________________________________________________________________ 59 60 /*-************************************************************************************************************//** 61 @short implement a guard to support non breakable transactions 62 @descr If you whish to support non breakable method calls without lockingf any mutex, rw-lock or 63 something like that - you should use this guard implementation. 64 Initialize it at first in your method and don't release it till end of your function! 65 Your "transaction" is registered in ctor and automaticly released in dtor. 66 Use set/get of working mode to enable/disable further transactions. 67 It's possible too, to enable automaticly throwing of some exceptions for illegal 68 transaction requests ... e.g. interface call for already disposed objects. 69 70 @attention To prevent us against wrong using, the default ctor, copy ctor and the =operator are maked private! 71 72 @implements - 73 @base INonCopyable 74 75 @devstatus draft 76 *//*-*************************************************************************************************************/ 77 class TransactionGuard : private INonCopyable 78 { 79 //------------------------------------------------------------------------------------------------------------- 80 // public methods 81 //------------------------------------------------------------------------------------------------------------- 82 public: 83 84 /*-****************************************************************************************************//** 85 @short ctors 86 @descr Use these ctor methods to initialize the guard right. 87 Given reference must be valid - otherwise crashes could occure! 88 89 @attention It's not neccessary to lock any mutex here! Because a ctor should not be called 90 from different threads at the same time ... this class use no refcount mechanism! 91 92 @seealso - 93 94 @param "rManager" reference to transaction manager for using to register a request 95 @param "eMode" enable/disable throwing of exceptions for rejected calls 96 @param "eReason" returns reason for rejected calls if "eMode=E_NOEXCEPTIONS"! 97 @return - 98 99 @onerror - 100 *//*-*****************************************************************************************************/ 101 inline TransactionGuard( ITransactionManager& rManager, EExceptionMode eMode, ERejectReason* eReason = NULL ) 102 : m_pManager( &rManager ) 103 { 104 // If exception mode is set to E_HARDEXCETIONS we don't need a buffer to return reason! 105 // We handle it private. If a call is rejected, our manager throw some exceptions ... and the reason 106 // could be ignorable ... 107 if( eReason == NULL ) 108 { 109 ERejectReason eMyReason; 110 m_pManager->registerTransaction( eMode, eMyReason ); 111 } 112 else 113 { 114 m_pManager->registerTransaction( eMode, *eReason ); 115 } 116 } 117 118 /*-************************************************************************************************************//** 119 @short dtor 120 @descr We must release the transaction manager and can forget his pointer. 121 122 @seealso - 123 124 @param - 125 @return - 126 127 @onerror - 128 *//*-*************************************************************************************************************/ 129 inline ~TransactionGuard() 130 { 131 stop(); 132 } 133 134 /*-************************************************************************************************************//** 135 @short stop current transaction 136 @descr We must release the transaction manager and can forget his pointer. 137 138 @attention We don't support any start() method here - because it is not easy to 139 detect if a transaction already started or not! 140 (combination of EExceptionMode and ERejectReason) 141 142 @seealso - 143 144 @param - 145 @return - 146 147 @onerror - 148 *//*-*************************************************************************************************************/ 149 inline void stop() 150 { 151 if( m_pManager != NULL ) 152 { 153 m_pManager->unregisterTransaction(); 154 m_pManager = NULL; 155 } 156 } 157 158 //------------------------------------------------------------------------------------------------------------- 159 // private methods 160 //------------------------------------------------------------------------------------------------------------- 161 private: 162 163 /*-****************************************************************************************************//** 164 @short disable using of these functions! 165 @descr It's not allowed to use this methods. Different problem can occure otherwise. 166 Thats why we disable it by make it private. 167 168 @seealso other ctor 169 170 @param - 171 @return - 172 173 @onerror - 174 *//*-*****************************************************************************************************/ 175 TransactionGuard(); 176 177 //------------------------------------------------------------------------------------------------------------- 178 // private member 179 //------------------------------------------------------------------------------------------------------------- 180 private: 181 182 ITransactionManager* m_pManager ; /// pointer to safed transaction manager 183 184 }; // class TransactionGuard 185 186 } // namespace framework 187 188 #endif // #ifndef __FRAMEWORK_THREADHELP_TRANSACTIONGUARD_HXX_ 189