summaryrefslogtreecommitdiff
path: root/cpp/include/IceUtil/ObjectBase.h
blob: 52467486793bf198dd1a6d2cd649cacc3864b5f0 (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
// **********************************************************************
//
// Copyright (c) 2003
// ZeroC, Inc.
// Billerica, MA, USA
//
// All Rights Reserved.
//
// Ice is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License version 2 as published by
// the Free Software Foundation.
//
// **********************************************************************

#ifndef ICE_UTIL_OBJECTBASE_H
#define ICE_UTIL_OBJECTBASE_H

#include <IceUtil/StaticRecMutex.h>
#include <set>

namespace IceUtil
{

extern ICE_UTIL_API StaticRecMutex gcMutex;

class GC;
class ObjectBase;

typedef std::set<ObjectBase*> ObjectSet;
extern ICE_UTIL_API ObjectSet objects; // Set of pointers to all existing classes.

typedef std::multiset<ObjectBase*> ObjectMultiSet;

class ICE_UTIL_API ObjectBase : public noncopyable
{
public:

    ObjectBase();
    virtual ~ObjectBase();
    void __incRef();
    void __decRef();
    int __getRef() const;
    void __setNoDelete(bool);
    void __decRefUnsafe();

protected:

    static void __addObject(ObjectMultiSet&, ObjectBase*);
    virtual void __gcReachable(ObjectMultiSet&) const = 0;
    virtual void __gcClear() = 0;

private:

    int _ref;
    bool _noDelete;
    bool _adopted;

    friend class IceUtil::GC;
};

inline
ObjectBase::ObjectBase()
{
    gcMutex.lock();
    _ref = 0;
    _noDelete = false;
    _adopted = false;
    gcMutex.unlock();
}

inline
ObjectBase::~ObjectBase()
{
    gcMutex.lock();
    ObjectSet::size_type num = objects.erase(this);
    assert(num == 1);
    gcMutex.unlock();
}

inline void
ObjectBase::__incRef()
{
    gcMutex.lock();
    assert(_ref >= 0);
    if(!_adopted && _ref == 0)
    {
        _adopted = true;
	std::pair<ObjectSet::iterator, bool> rc = objects.insert(this);
	assert(rc.second);
    }
    ++_ref;
    gcMutex.unlock();
}

inline void
ObjectBase::__decRef()
{
    gcMutex.lock();
    bool doDelete = false;
    assert(_ref > 0);
    if(--_ref == 0)
    {
	doDelete = !_noDelete;
	_noDelete = true;
    }
    if(doDelete)
    {
	delete this;
    }
    gcMutex.unlock();
}

inline int
ObjectBase::__getRef() const
{
    gcMutex.lock();
    int ref = _ref;
    gcMutex.unlock();
    return ref;
}

inline void
ObjectBase::__setNoDelete(bool b)
{
    gcMutex.lock();
    _noDelete = b;
    gcMutex.unlock();
}

inline void
ObjectBase::__decRefUnsafe()
{
    --_ref;
}

inline void
ObjectBase::__addObject(ObjectMultiSet& c, ObjectBase* p)
{
    gcMutex.lock();
    if(p)
    {
	c.insert(p);
    }
    gcMutex.unlock();
}

}

#endif