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