UCommon
Main Page
Namespaces
Data Structures
Files
Examples
File List
Globals
ucommon
object.h
Go to the documentation of this file.
1
// Copyright (C) 2006-2010 David Sugar, Tycho Softworks.
2
//
3
// This file is part of GNU uCommon C++.
4
//
5
// GNU uCommon C++ is free software: you can redistribute it and/or modify
6
// it under the terms of the GNU Lesser General Public License as published
7
// by the Free Software Foundation, either version 3 of the License, or
8
// (at your option) any later version.
9
//
10
// GNU uCommon C++ is distributed in the hope that it will be useful,
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
// GNU Lesser General Public License for more details.
14
//
15
// You should have received a copy of the GNU Lesser General Public License
16
// along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>.
17
29
#ifndef _UCOMMON_OBJECT_H_
30
#define _UCOMMON_OBJECT_H_
31
32
#ifndef _UCOMMON_CPR_H_
33
#include <
ucommon/cpr.h
>
34
#endif
35
36
#ifndef _UCOMMON_GENERICS_H_
37
#include <
ucommon/generics.h
>
38
#endif
39
40
#include <stdlib.h>
41
42
NAMESPACE_UCOMMON
43
51
class
__EXPORT
ObjectProtocol
52
{
53
public
:
57
virtual
void
retain
(
void
) = 0;
58
62
virtual
void
release
(
void
) = 0;
63
67
virtual
~
ObjectProtocol
();
68
72
ObjectProtocol
*
copy
(
void
);
73
77
inline
void
operator++(
void
)
78
{
retain
();};
79
83
inline
void
operator--(
void
)
84
{
release
();};
85
};
86
94
class
__EXPORT
CountedObject
:
public
ObjectProtocol
95
{
96
private
:
97
volatile
unsigned
count;
98
99
protected
:
103
CountedObject
();
104
111
CountedObject
(
const
ObjectProtocol
&ref);
112
118
virtual
void
dealloc(
void
);
119
120
public
:
126
inline
bool
isCopied(
void
)
127
{
return
count > 1;};
128
133
inline
bool
isRetained(
void
)
134
{
return
count > 0;};
135
140
inline
unsigned
copied(
void
)
141
{
return
count;};
142
146
void
retain
(
void
);
147
152
void
release
(
void
);
153
};
154
165
class
__EXPORT
auto_object
166
{
167
protected
:
168
ObjectProtocol
*object;
169
170
auto_object
();
171
172
public
:
177
auto_object
(
ObjectProtocol
*
object
);
178
184
auto_object
(
const
auto_object
&
pointer
);
185
191
~
auto_object
();
192
197
void
release
(
void
);
198
203
bool
operator!()
const
;
204
209
operator
bool()
const
;
210
216
bool
operator==(
ObjectProtocol
*
object
)
const
;
217
223
bool
operator!=(
ObjectProtocol
*
object
)
const
;
224
231
void
operator=(
ObjectProtocol
*
object
);
232
};
233
245
class
__EXPORT
sparse_array
246
{
247
private
:
248
ObjectProtocol
**vector;
249
unsigned
max
;
250
251
protected
:
257
virtual
ObjectProtocol
*create(
void
) = 0;
258
262
void
purge(
void
);
263
269
ObjectProtocol
*
get
(
unsigned
offset);
270
276
sparse_array
(
unsigned
size);
277
278
public
:
282
virtual
~sparse_array();
283
288
unsigned
count(
void
);
289
};
290
300
template
<
class
T>
301
class
sarray
:
public
sparse_array
302
{
303
public
:
308
inline
sarray
(
unsigned
size) :
sparse_array
(size) {};
309
316
inline
T *
get
(
unsigned
offset)
317
{
static_cast<
T*
>
(sparse_array::get(offset));};
318
325
inline
T& operator[](
unsigned
offset)
326
{
return
get
(offset);};
327
328
private
:
329
__LOCAL
ObjectProtocol
*create(
void
)
330
{
return
new
T;};
331
};
332
342
template
<
typename
T,
class
O = CountedObject>
343
class
object_value
:
public
O
344
{
345
protected
:
350
inline
void
set
(
const
T& object)
351
{value = object;};
352
353
public
:
354
T value;
359
inline
object_value
() : O() {};
360
365
inline
object_value
(T& existing) : O()
366
{value = existing;};
367
372
inline
T& operator*()
373
{
return
value;};
374
379
inline
void
operator=(
const
T& data)
380
{value = data;};
381
386
inline
operator
T&()
387
{
return
value;};
388
389
inline
T& operator()()
390
{
return
value;};
391
396
inline
void
operator()(T& data)
397
{value = data;};
398
};
399
412
template
<
class
T,
class
P = auto_
object
>
413
class
object_pointer
:
public
P
414
{
415
public
:
419
inline
object_pointer
() : P() {};
420
425
inline
object_pointer
(T*
object
) : P(object) {};
426
431
inline
T* operator*()
const
432
{
return
static_cast<
T*
>
(P::object);};
433
438
inline
T& operator()()
const
439
{
return
*(
static_cast<
T*
>
(P::object));};
440
445
inline
T* operator->()
const
446
{
return
static_cast<
T*
>
(P::object);};
447
452
inline
T*
get
(void)
const
453
{
return
static_cast<
T*
>
(P::object);};
454
459
inline
T* operator++()
460
{P::operator++();
return
get
();};
461
466
inline
void
operator--()
467
{P::operator--();
return
get
();};
468
473
inline
void
operator=(T *typed)
474
{P::operator=((
ObjectProtocol
*)typed);};
475
479
inline
operator
bool()
480
{
return
P::object != NULL;};
481
485
inline
bool
operator!()
486
{
return
P::object == NULL;};
487
};
488
493
inline
void
retain
(
ObjectProtocol
*
object
)
494
{
object
->retain();}
495
500
inline
void
release
(
ObjectProtocol
*
object
)
501
{
object
->release();}
502
507
inline
ObjectProtocol
*
copy
(
ObjectProtocol
*
object
)
508
{
return
object
->
copy
();}
509
510
END_NAMESPACE
511
512
#endif
Generated on Tue Jul 10 2012 19:47:35 for UCommon by
1.8.1.1