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