]> git.saurik.com Git - cydia.git/blame - Menes/ObjectHandle.h
Just in case, I want to initialize this variable.
[cydia.git] / Menes / ObjectHandle.h
CommitLineData
86cf87e2 1/* Cydia - iPhone UIKit Front-End for Debian APT
6fa0bb60 2 * Copyright (C) 2008-2014 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 "CyteKit/UCPlatform.h"
26
27#include <CoreFoundation/CoreFoundation.h>
28#include <Foundation/Foundation.h>
29
bf7c998c
JF
30template <typename Type_, unsigned Delegate_>
31struct MenesObjectHandle_;
32
86cf87e2 33template <typename Type_>
bf7c998c
JF
34struct MenesObjectHandle_<Type_, 0> {
35 static _finline void Execute(Type_ *value) {
36 }
37};
38
39template <typename Type_>
40struct MenesObjectHandle_<Type_, 1> {
41 static _finline void Execute(Type_ *value) {
42 [value setDelegate:nil];
43 }
44};
45
46template <typename Type_>
47struct MenesObjectHandle_<Type_, 2> {
48 static _finline void Execute(Type_ *value) {
49 [value setDelegate:nil];
50 [value setDataSource:nil];
51 }
52};
53
54template <typename Type_, unsigned Delegate_ = 0>
86cf87e2
JF
55class MenesObjectHandle {
56 private:
57 Type_ *value_;
58
59 _finline void Retain_() {
60 if (value_ != nil)
61 CFRetain((CFTypeRef) value_);
62 }
63
9599dd2f
JF
64 _finline void Release_(Type_ *value) {
65 if (value != nil) {
66 MenesObjectHandle_<Type_, Delegate_>::Execute(value);
67 CFRelease((CFTypeRef) value);
bf7c998c 68 }
86cf87e2
JF
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() {
9599dd2f 85 Release_(value_);
86cf87e2
JF
86 }
87
88 _finline operator Type_ *() const {
89 return value_;
90 }
91
fac0ed79
JF
92 _finline Type_ *operator ->() const {
93 return value_;
94 }
95
86cf87e2
JF
96 _finline MenesObjectHandle &operator =(Type_ *value) {
97 if (value_ != value) {
98 Type_ *old(value_);
99 value_ = value;
100 Retain_();
9599dd2f 101 Release_(old);
86cf87e2
JF
102 } return *this;
103 }
dfdb9ae0
JF
104
105 _finline MenesObjectHandle &operator =(const MenesObjectHandle &value) {
106 return this->operator =(value.operator Type_ *());
107 }
86cf87e2
JF
108};
109
110#define _H MenesObjectHandle
111
fac0ed79
JF
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) \
124rproperty_(Class, field) \
125@end
126
127#define rwproperty(Class, field, Field) \
128@implementation Class (Menes_##field) \
129rproperty_(Class, field) \
130wproperty_(Class, field, Field) \
131@end
132
86cf87e2 133#endif//Menes_ObjectHandle_H