]> git.saurik.com Git - cydia.git/blob - Menes/ObjectHandle.h
Try to protect firmware.sh against reentrant bugs.
[cydia.git] / Menes / ObjectHandle.h
1 /* Cydia - iPhone UIKit Front-End for Debian APT
2 * Copyright (C) 2008-2015 Jay Freeman (saurik)
3 */
4
5 /* GNU General Public License, Version 3 {{{ */
6 /*
7 * Cydia is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation, either version 3 of the License,
10 * or (at your option) any later version.
11 *
12 * Cydia is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with Cydia. If not, see <http://www.gnu.org/licenses/>.
19 **/
20 /* }}} */
21
22 #ifndef Menes_ObjectHandle_H
23 #define Menes_ObjectHandle_H
24
25 #include "CyteKit/UCPlatform.h"
26
27 #include <CoreFoundation/CoreFoundation.h>
28 #include <Foundation/Foundation.h>
29
30 template <typename Type_, unsigned Delegate_>
31 struct MenesObjectHandle_;
32
33 template <typename Type_>
34 struct MenesObjectHandle_<Type_, 0> {
35 static _finline void Execute(Type_ *value) {
36 }
37 };
38
39 template <typename Type_>
40 struct MenesObjectHandle_<Type_, 1> {
41 static _finline void Execute(Type_ *value) {
42 [value setDelegate:nil];
43 }
44 };
45
46 template <typename Type_>
47 struct MenesObjectHandle_<Type_, 2> {
48 static _finline void Execute(Type_ *value) {
49 [value setDelegate:nil];
50 [value setDataSource:nil];
51 }
52 };
53
54 template <typename Type_, unsigned Delegate_ = 0>
55 class MenesObjectHandle {
56 private:
57 Type_ *value_;
58
59 _finline void Retain_() {
60 if (value_ != nil)
61 CFRetain((CFTypeRef) value_);
62 }
63
64 _finline void Release_(Type_ *value) {
65 if (value != nil) {
66 MenesObjectHandle_<Type_, Delegate_>::Execute(value);
67 CFRelease((CFTypeRef) value);
68 }
69 }
70
71 public:
72 _finline MenesObjectHandle(const MenesObjectHandle &rhs) :
73 value_(rhs.value_ == nil ? nil : (Type_ *) CFRetain((CFTypeRef) rhs.value_))
74 {
75 }
76
77 _finline MenesObjectHandle(Type_ *value = NULL, bool mended = false) :
78 value_(value)
79 {
80 if (!mended)
81 Retain_();
82 }
83
84 _finline ~MenesObjectHandle() {
85 Release_(value_);
86 }
87
88 _finline operator Type_ *() const {
89 return value_;
90 }
91
92 _finline Type_ *operator ->() const {
93 return value_;
94 }
95
96 _finline MenesObjectHandle &operator =(Type_ *value) {
97 if (value_ != value) {
98 Type_ *old(value_);
99 value_ = value;
100 Retain_();
101 Release_(old);
102 } return *this;
103 }
104
105 _finline MenesObjectHandle &operator =(const MenesObjectHandle &value) {
106 return this->operator =(value.operator Type_ *());
107 }
108 };
109
110 #define _H MenesObjectHandle
111
112 #define rproperty_(Class, field) \
113 - (typeof(((Class*)nil)->_##field.operator->())) field { \
114 return _##field; \
115 }
116
117 #define wproperty_(Class, field, Field) \
118 - (void) set##Field:(typeof(((Class*)nil)->_##field.operator->()))field { \
119 _##field = field; \
120 }
121
122 #define roproperty(Class, field) \
123 @implementation Class (Menes_##field) \
124 rproperty_(Class, field) \
125 @end
126
127 #define rwproperty(Class, field, Field) \
128 @implementation Class (Menes_##field) \
129 rproperty_(Class, field) \
130 wproperty_(Class, field, Field) \
131 @end
132
133 #endif//Menes_ObjectHandle_H