aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/basetypes/MCObject.cc
blob: 1665e1eecdf79f99fbe288fe37233714855d0bce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include "MCObject.h"

#include <stdlib.h>
#include <typeinfo>
#include <cxxabi.h>

#include "MCAutoreleasePool.h"
#include "MCString.h"
#include "MCHash.h"
#include "MCLog.h"
#include "MCUtils.h"
#include "MCAssert.h"
#include "MCMainThread.h"
#include "MCLog.h"

using namespace mailcore;

bool mailcore::zombieEnabled = 0;

Object::Object()
{
    init();
}

Object::~Object()
{
}

void Object::init()
{
    pthread_mutex_init(&mLock, NULL);
    mCounter = 1;
}

int Object::retainCount()
{
    pthread_mutex_lock(&mLock);
    int value = mCounter;
    pthread_mutex_unlock(&mLock);
    
    return value;
}

Object * Object::retain()
{
    pthread_mutex_lock(&mLock);
    mCounter ++;
    pthread_mutex_unlock(&mLock);
    return this;
}

void Object::release()
{
    bool shouldRelease = false;
    
    pthread_mutex_lock(&mLock);
    mCounter --;
    if (mCounter == 0) {
        shouldRelease = true;
    }
    if (mCounter < 0) {
        MCLog("release too much %p %s", this, MCUTF8(className()));
        MCAssert(0);
    }
    pthread_mutex_unlock(&mLock);
    
    if (shouldRelease && !zombieEnabled) {
        //MCLog("dealloc %s", className()->description()->UTF8Characters());
        delete this;
    }
}

Object * Object::autorelease()
{
    AutoreleasePool::autorelease(this);
    return this;
}

String * Object::className()
{
    int status;
    char * unmangled = abi::__cxa_demangle(typeid(* this).name(), NULL, NULL, &status);
	return mailcore::String::uniquedStringWithUTF8Characters(unmangled);
}

String * Object::description()
{
    return String::stringWithUTF8Format("<%s:%p>", className()->UTF8Characters(), this);
}

bool Object::isEqual(Object * otherObject)
{
    return this == otherObject;
}

unsigned int Object::hash()
{
    return hashCompute((const char *) this, sizeof(this));
}

Object * Object::copy()
{
    MCAssert(0);
    return NULL;
}

void Object::performMethod(Object::Method method, void * context)
{
    (this->*method)(context);
}

struct mainThreadCallData {
    Object * obj;
    void * context;
    Object::Method method;
};

static void performOnMainThread(void * info)
{
    struct mainThreadCallData * data;
    void * context;
    Object * obj;
    Object::Method method;
    
    data = (struct mainThreadCallData *) info;
    obj = data->obj;
    context = data->context;
    method = data->method;
    
    (obj->*method)(context);
    
    free(data);
}

static void callAfterDelay(void * info)
{
    struct mainThreadCallData * data;
    void * context;
    Object * obj;
    Object::Method method;
    
    data = (struct mainThreadCallData *) info;
    obj = data->obj;
    context = data->context;
    method = data->method;
    
    (obj->*method)(context);
    
    free(data);
}

void Object::performMethodOnMainThread(Method method, void * context, bool waitUntilDone)
{
    struct mainThreadCallData * data;
    
    data = (struct mainThreadCallData *) calloc(sizeof(* data), 1);
    data->obj = this;
    data->context = context;
    data->method = method;
    
    if (waitUntilDone) {
        callOnMainThreadAndWait(performOnMainThread, data);
    }
    else {
        callOnMainThread(performOnMainThread, data);
    }
}

void Object::performMethodAfterDelay(Method method, void * context, double delay)
{
    struct mainThreadCallData * data;
    
    data = (struct mainThreadCallData *) calloc(sizeof(* data), 1);
    data->obj = this;
    data->context = context;
    data->method = method;
    
    callAfterDelay(performOnMainThread, data, delay);
}