forked from rcedgar/syncmer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobj.h
82 lines (64 loc) · 1.42 KB
/
obj.h
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
#ifndef obj_h
#define obj_h
#define TRACK_OBJ_THREAD 0
#include "objtype.h"
/***
Objects are created and deleted only by ObjMgr.
Objects can contain other objects.
Getting new object:
ptr = ObjMgr::GetXxx()
New object has ref count = 1.
Adding reference:
Convention: one variable per counted reference.
assert(var == 0)
var = ptr
OM->Up(var)
Up() is similar to IUnknown::AddRef().
Releasing reference:
ObjMgr::Downvar)
var = 0
Down() is similar to IUknown::Release().
When objects contain references to other objects:
In Init/Create()-like function, m_xxx = OM->GetXxx().
In OnZeroRefCount(), call ObjMgr::Downm_xxx).
There is one ObjMgr per thread, avoids need for mutexes etc.
No Clear() member, this is confusing.
Memory allocation convention (not enforced or expressed):
Objects use grow-only memory strategy.
D'tor frees memory.
D'tor is the only place memory is freed (except growing).
***/
class ObjMgr;
class Obj
{
friend class ObjMgr;
#if TRACK_OBJ_THREAD
public:
int m_OMPThreadIndex;
#endif
protected:
ObjType m_Type;
unsigned m_RefCount;
Obj *m_Fwd;
Obj *m_Bwd;
protected:
Obj(ObjType Type)
{
m_Type = Type;
m_RefCount = 0;
m_Fwd = 0;
m_Bwd = 0;
}
virtual ~Obj()
{
}
public:
unsigned GetRefCount() const
{
return m_RefCount;
}
// Override if contains objects, must Down() them.
virtual void OnZeroRefCount() {}
virtual unsigned GetMemBytes() const = 0;
};
#endif // obj_h