]> git.saurik.com Git - cydia.git/blame - Menes/ObjectHandle.h
Objective-C seriously didn't notice my mistake :/.
[cydia.git] / Menes / ObjectHandle.h
CommitLineData
86cf87e2 1/* Cydia - iPhone UIKit Front-End for Debian APT
4c66fad9 2 * Copyright (C) 2008-2015 Jay Freeman (saurik)
86cf87e2
JF
3*/
4
6d9696a5 5/* GNU General Public License, Version 3 {{{ */
86cf87e2 6/*
6d9696a5
JF
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.
86cf87e2 11 *
6d9696a5
JF
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.
86cf87e2 16 *
6d9696a5
JF
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**/
86cf87e2
JF
20/* }}} */
21
22#ifndef Menes_ObjectHandle_H
23#define Menes_ObjectHandle_H
24
7aa82ca2
JF
25#include <CoreFoundation/CoreFoundation.h>
26#include <Foundation/Foundation.h>
27
bf7c998c
JF
28template <typename Type_, unsigned Delegate_>
29struct MenesObjectHandle_;
30
86cf87e2 31template <typename Type_>
bf7c998c
JF
32struct MenesObjectHandle_<Type_, 0> {
33 static _finline void Execute(Type_ *value) {
34 }
35};
36
37template <typename Type_>
38struct MenesObjectHandle_<Type_, 1> {
39 static _finline void Execute(Type_ *value) {
40 [value setDelegate:nil];
41 }
42};
43
44template <typename Type_>
45struct MenesObjectHandle_<Type_, 2> {
46 static _finline void Execute(Type_ *value) {
47 [value setDelegate:nil];
48 [value setDataSource:nil];
49 }
50};
51
52template <typename Type_, unsigned Delegate_ = 0>
86cf87e2
JF
53class MenesObjectHandle {
54 private:
55 Type_ *value_;
56
57 _finline void Retain_() {
58 if (value_ != nil)
59 CFRetain((CFTypeRef) value_);
60 }
61
9599dd2f
JF
62 _finline void Release_(Type_ *value) {
63 if (value != nil) {
64 MenesObjectHandle_<Type_, Delegate_>::Execute(value);
65 CFRelease((CFTypeRef) value);
bf7c998c 66 }
86cf87e2
JF
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() {
9599dd2f 83 Release_(value_);
86cf87e2
JF
84 }
85
86 _finline operator Type_ *() const {
87 return value_;
88 }
89
fac0ed79
JF
90 _finline Type_ *operator ->() const {
91 return value_;
92 }
93
86cf87e2
JF
94 _finline MenesObjectHandle &operator =(Type_ *value) {
95 if (value_ != value) {
96 Type_ *old(value_);
97 value_ = value;
98 Retain_();
9599dd2f 99 Release_(old);
86cf87e2
JF
100 } return *this;
101 }
dfdb9ae0
JF
102
103 _finline MenesObjectHandle &operator =(const MenesObjectHandle &value) {
104 return this->operator =(value.operator Type_ *());
105 }
86cf87e2
JF
106};
107
108#define _H MenesObjectHandle
109
fac0ed79
JF
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) \
122rproperty_(Class, field) \
123@end
124
125#define rwproperty(Class, field, Field) \
126@implementation Class (Menes_##field) \
127rproperty_(Class, field) \
128wproperty_(Class, field, Field) \
129@end
130
68df8c0b
JF
131// XXX: I hate clang. Apple: please get over your petty hatred of GPL and fix your gcc fork
132#define synchronized(lock) \
133 synchronized(static_cast<NSObject *>(lock))
134
86cf87e2 135#endif//Menes_ObjectHandle_H