]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_utilities/lib/transactions.h
Security-57337.40.85.tar.gz
[apple/security.git] / OSX / libsecurity_utilities / lib / transactions.h
1 /*
2 * Copyright (c) 2004,2011,2014 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24
25 //
26 // transactions - generic transaction frame support
27 //
28 #ifndef _H_TRANSACTIONS
29 #define _H_TRANSACTIONS
30
31 #include <security_utilities/utilities.h>
32 #include <security_utilities/debugging.h>
33
34
35 namespace Security {
36
37
38 //
39 // Implementation base class. Do not use directly.
40 //
41 class TransactionBase {
42 public:
43 // what happens if this object gets destroyed?
44 enum Outcome {
45 successful, // succeeds as set
46 cancelled, // cancelled (rolled back)
47 conditional // succeeds normally, cancelled on exception
48 };
49
50 public:
51 virtual ~TransactionBase();
52
53 void outcome(Outcome oc) { mOutcome = oc; }
54 Outcome outcome() const { return mOutcome; }
55
56 protected:
57 TransactionBase(Outcome outcome) : mOutcome(outcome) { }
58
59 Outcome finalOutcome() const;
60
61 private:
62 Outcome mOutcome; // current outcome setting
63 };
64
65
66 //
67 // A ManagedTransaction will call methods begin() and end() on the Carrier object
68 // it belongs to, and manage the "outcome" state and semantics automatically.
69 // You would usually subclass this, though the class is complete in itself if you
70 // need nothing else out of your transaction objects.
71 //
72 template <class Carrier>
73 class ManagedTransaction : public TransactionBase {
74 public:
75 ManagedTransaction(Carrier &carrier, Outcome outcome = conditional)
76 : TransactionBase(outcome), mCarrier(carrier)
77 {
78 carrier.begin();
79 }
80
81 ~ManagedTransaction()
82 {
83 switch (finalOutcome()) {
84 case successful:
85 this->commitAction();
86 break;
87 case cancelled:
88 this->cancelAction();
89 break;
90 default:
91 assert(false);
92 break;
93 }
94 }
95
96 protected:
97 virtual void commitAction() { mCarrier.end(); }
98 virtual void cancelAction() { mCarrier.cancel(); }
99
100 Carrier &mCarrier;
101 };
102
103
104 } // end namespace Security
105
106
107 #endif //_H_TRANSACTIONS