]> git.saurik.com Git - cydia.git/blob - Menes/ObjectHandle.h
Move more of sysroot's sadness into iPhonePrivate.
[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 <CoreFoundation/CoreFoundation.h>
26 #include <Foundation/Foundation.h>
27
28 template <typename Type_, unsigned Delegate_>
29 struct MenesObjectHandle_;
30
31 template <typename Type_>
32 struct MenesObjectHandle_<Type_, 0> {
33 static _finline void Execute(Type_ *value) {
34 }
35 };
36
37 template <typename Type_>
38 struct MenesObjectHandle_<Type_, 1> {
39 static _finline void Execute(Type_ *value) {
40 [value setDelegate:nil];
41 }
42 };
43
44 template <typename Type_>
45 struct MenesObjectHandle_<Type_, 2> {
46 static _finline void Execute(Type_ *value) {
47 [value setDelegate:nil];
48 [value setDataSource:nil];
49 }
50 };
51
52 template <typename Type_, unsigned Delegate_ = 0>
53 class MenesObjectHandle {
54 private:
55 Type_ *value_;
56
57 _finline void Retain_() {
58 if (value_ != nil)
59 CFRetain((CFTypeRef) value_);
60 }
61
62 _finline void Release_(Type_ *value) {
63 if (value != nil) {
64 MenesObjectHandle_<Type_, Delegate_>::Execute(value);
65 CFRelease((CFTypeRef) value);
66 }
67 }
68
69 public:
70 _finline MenesObjectHandle(const MenesObjectHandle &rhs) :
71 value_(rhs.value_ == nil ? nil : (Type_ *) CFRetain((CFTypeRef) rhs.value_))
72 {
73 }
74
75 _finline MenesObjectHandle(Type_ *value = NULL, bool mended = false) :
76 value_(value)
77 {
78 if (!mended)
79 Retain_();
80 }
81
82 _finline ~MenesObjectHandle() {
83 Release_(value_);
84 }
85
86 _finline operator Type_ *() const {
87 return value_;
88 }
89
90 _finline Type_ *operator ->() const {
91 return value_;
92 }
93
94 _finline MenesObjectHandle &operator =(Type_ *value) {
95 if (value_ != value) {
96 Type_ *old(value_);
97 value_ = value;
98 Retain_();
99 Release_(old);
100 } return *this;
101 }
102
103 _finline MenesObjectHandle &operator =(const MenesObjectHandle &value) {
104 return this->operator =(value.operator Type_ *());
105 }
106 };
107
108 #define _H MenesObjectHandle
109
110 #define rproperty_(Class, field) \
111 - (typeof(((Class*)nil)->_##field.operator->())) field { \
112 return _##field; \
113 }
114
115 #define wproperty_(Class, field, Field) \
116 - (void) set##Field:(typeof(((Class*)nil)->_##field.operator->()))field { \
117 _##field = field; \
118 }
119
120 #define roproperty(Class, field) \
121 @implementation Class (Menes_##field) \
122 rproperty_(Class, field) \
123 @end
124
125 #define rwproperty(Class, field, Field) \
126 @implementation Class (Menes_##field) \
127 rproperty_(Class, field) \
128 wproperty_(Class, field, Field) \
129 @end
130
131 #endif//Menes_ObjectHandle_H