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
|
/* Copyright (c) 2008 PrimeBase Technologies GmbH, Germany
*
* PrimeBase Media Stream for MySQL
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* Original author: Paul McCullagh (H&G2JCtL)
* Continued development: Barry Leslie
*
* 2007-05-20
*
*
*/
#ifndef __CSOBJECT_H__
#define __CSOBJECT_H__
#include "CSDefs.h"
#include "CSMemory.h"
#include "CSMutex.h"
// The RETAIN() macro is useful when passing object references to functions
// that you want to retain. For example istead of:
// x->retain();
// foo(x);
// You can do:
// foo(RETAIN(x));
//
// After 20 years of programming I have finally found a use for the 'C' comma
// operator out side of a 'for' loop. :)
#define RETAIN(x) ((x)->retain(),(x))
// A back reference is a reference stored by an object pointing to it's owner.
// Since the owner will free all of it's object when it is released the object
// itself cannot hold a reference to its owner.
// Use this macro to make it clear that this is what you intended.
#define BACK_REFERENCE(x) (x)
class CSObject {
public:
CSObject() { }
virtual ~CSObject() { finalize(); }
#ifdef DEBUG
virtual void retain(const char *func, const char *file, uint32_t line);
virtual void release(const char *func, const char *file, uint32_t line);
#else
virtual void retain();
virtual void release();
#endif
/*
* All objects are sortable, hashable and linkable,
* as long as these methods are implemented:
*/
virtual void finalize() { }
virtual CSObject *getKey();
virtual int compareKey(CSObject *);
virtual uint32_t hashKey();
virtual CSObject *getHashLink();
virtual void setHashLink(CSObject *);
virtual CSObject *getNextLink();
virtual CSObject *getPrevLink();
virtual void setNextLink(CSObject *);
virtual void setPrevLink(CSObject *);
#ifdef DEBUG
static void *operator new(size_t size, const char *func, const char *file, uint32_t line) {
return cs_mm_malloc(func, file, line, size);
}
static void operator delete(void *ptr, size_t size) {
UNUSED(size);
cs_mm_free(ptr);
}
#endif
};
class CSStaticObject : public CSObject {
#ifdef DEBUG
virtual void retain(const char *func, const char *file, uint32_t line);
virtual void release(const char *func, const char *file, uint32_t line);
int iTrackMe;
#else
virtual void retain();
virtual void release();
#endif
};
class CSRefObject : public CSObject {
public:
CSRefObject();
virtual ~CSRefObject();
#ifdef DEBUG
virtual void startTracking();
virtual void retain(const char *func, const char *file, uint32_t line);
virtual void release(const char *func, const char *file, uint32_t line);
int iTrackMe;
#else
virtual void retain();
virtual void release();
#endif
uint32_t getRefCount() { return iRefCount;}
private:
uint32_t iRefCount;
};
class CSSharedRefObject : public CSObject, public CSSync {
public:
CSSharedRefObject();
virtual ~CSSharedRefObject();
#ifdef DEBUG
virtual void startTracking();
virtual void retain(const char *func, const char *file, uint32_t line);
virtual void release(const char *func, const char *file, uint32_t line);
int iTrackMe;
#else
virtual void retain();
virtual void release();
#endif
uint32_t getRefCount() { return iRefCount;}
private:
uint32_t iRefCount;
};
#ifdef DEBUG
#define new new(__FUNC__, __FILE__, __LINE__)
#define retain() retain(__FUNC__, __FILE__, __LINE__)
#define release() release(__FUNC__, __FILE__, __LINE__)
#endif
class CSPooled {
public:
virtual ~CSPooled() {}
virtual void returnToPool() = 0;
};
#endif
|