xref: /trunk/main/framework/inc/threadhelp/transactionguard.hxx (revision 772835b774ba4c34b8de932fc3929f72593722f7)
1f8e07b45SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3f8e07b45SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4f8e07b45SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5f8e07b45SAndrew Rist  * distributed with this work for additional information
6f8e07b45SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7f8e07b45SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8f8e07b45SAndrew Rist  * "License"); you may not use this file except in compliance
9f8e07b45SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11f8e07b45SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13f8e07b45SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14f8e07b45SAndrew Rist  * software distributed under the License is distributed on an
15f8e07b45SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16f8e07b45SAndrew Rist  * KIND, either express or implied.  See the License for the
17f8e07b45SAndrew Rist  * specific language governing permissions and limitations
18f8e07b45SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20f8e07b45SAndrew Rist  *************************************************************/
21f8e07b45SAndrew Rist 
22f8e07b45SAndrew 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!
6107a3d7f1SPedro Giffuni                     Your "transaction" is registered in ctor and automatically released in dtor.
62cdf0e10cSrcweir                     Use set/get of working mode to enable/disable further transactions.
6307a3d7f1SPedro Giffuni                     It's possible too, to enable automatically throwing of some exceptions for illegal
64cdf0e10cSrcweir                     transaction requests ... e.g. interface call for already disposed objects.
65cdf0e10cSrcweir 
6630acf5e8Spfg     @attention      To prevent us against wrong using, the default ctor, copy ctor and the =operator are marked 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.
8307a3d7f1SPedro Giffuni                         Given reference must be valid - otherwise crashes could occur!
84cdf0e10cSrcweir 
8507a3d7f1SPedro Giffuni             @attention  It's not necessary 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         {
100*772835b7SJohn Bampton             // If exception mode is set to E_HARDEXCEPTIONS 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!
16107a3d7f1SPedro Giffuni             @descr      It's not allowed to use this methods. Different problems can occur otherwise.
162cfd52e18Smseidel                         That's why we disable it by making 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