~ubuntu-branches/ubuntu/maverick/freecad/maverick

« back to all changes in this revision

Viewing changes to src/Base/Handle.h

  • Committer: Bazaar Package Importer
  • Author(s): Teemu Ikonen
  • Date: 2009-07-16 18:37:41 UTC
  • Revision ID: james.westby@ubuntu.com-20090716183741-oww9kcxqrk991i1n
Tags: upstream-0.8.2237
Import upstream version 0.8.2237

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *   (c) J�rgen Riegel (juergen.riegel@web.de) 2002                        *
 
3
 *                                                                         *
 
4
 *   This file is part of the FreeCAD CAx development system.              *
 
5
 *                                                                         *
 
6
 *   This program is free software; you can redistribute it and/or modify  *
 
7
 *   it under the terms of the GNU Library General Public License (LGPL)   *
 
8
 *   as published by the Free Software Foundation; either version 2 of     *
 
9
 *   the License, or (at your option) any later version.                   *
 
10
 *   for detail see the LICENCE text file.                                 *
 
11
 *                                                                         *
 
12
 *   FreeCAD is distributed in the hope that it will be useful,            *
 
13
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 
14
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 
15
 *   GNU Library General Public License for more details.                  *
 
16
 *                                                                         *
 
17
 *   You should have received a copy of the GNU Library General Public     *
 
18
 *   License along with FreeCAD; if not, write to the Free Software        *
 
19
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  *
 
20
 *   USA                                                                   *
 
21
 *                                                                         *
 
22
 *   Juergen Riegel 2002                                                   *
 
23
 ***************************************************************************/
 
24
 
 
25
 
 
26
#ifndef BASE_HANDLE_H
 
27
#define BASE_HANDLE_H
 
28
 
 
29
// Std. configurations
 
30
 
 
31
#include <string>
 
32
#include <map>
 
33
#include <typeinfo>
 
34
 
 
35
 
 
36
namespace Base
 
37
{
 
38
 
 
39
/** Reference class
 
40
 *  Implementation of the reference counting pattern.
 
41
 *  Only able to instantiate with a class inheriting
 
42
 *  Base::Handled.
 
43
 */
 
44
template <class T>
 
45
class Reference
 
46
{
 
47
public:
 
48
    //**************************************************************************
 
49
    // construction & destruction
 
50
 
 
51
    /** Pointer and default constructor */
 
52
    Reference() : _toHandle(0) {
 
53
    }
 
54
 
 
55
    Reference(T* p) : _toHandle(p) {
 
56
        if (_toHandle)
 
57
            _toHandle->ref();
 
58
    }
 
59
 
 
60
    /** Copy constructor */
 
61
    Reference(const Reference<T>& p) : _toHandle(p._toHandle) {
 
62
        if (_toHandle)
 
63
            _toHandle->ref();
 
64
    }
 
65
 
 
66
    /** destructor
 
67
     *  Release the reference counter which causes,
 
68
     *  in case of the last one, the referenced object to
 
69
     *  be destructed!
 
70
     */
 
71
    ~Reference() {
 
72
        if (_toHandle)
 
73
            _toHandle->unref();
 
74
    }
 
75
 
 
76
    //**************************************************************************
 
77
    // operator implementation
 
78
 
 
79
    /** Assign operator from a pointer */
 
80
    Reference <T>& operator=(T* p) {
 
81
        // check if we want to reassign the same object
 
82
        if (_toHandle == p)
 
83
            return *this;
 
84
        if (_toHandle)
 
85
            _toHandle->unref();
 
86
        _toHandle = p;
 
87
        if (_toHandle)
 
88
            _toHandle->ref();
 
89
        return *this;
 
90
    }
 
91
 
 
92
    /** Assign operator from a handle */
 
93
    Reference <T>& operator=(const Reference<T>& p) {
 
94
        // check if we want to reassign the same object
 
95
        if (_toHandle == p._toHandle)
 
96
            return *this;
 
97
        if (_toHandle)
 
98
            _toHandle->unref();
 
99
        _toHandle = p._toHandle;
 
100
        if (_toHandle)
 
101
            _toHandle->ref();
 
102
        return *this;
 
103
    }
 
104
 
 
105
    /** Dereference operator */
 
106
    T& operator*() const {
 
107
        return *_toHandle;
 
108
    }
 
109
 
 
110
    /** Dereference operator */
 
111
    T* operator->() const {
 
112
        return _toHandle;
 
113
    }
 
114
 
 
115
    operator T*() const {
 
116
        return _toHandle;
 
117
    }
 
118
 
 
119
    /** Lower operator, needed for sorting in maps and sets */
 
120
    bool operator<(const Reference<T>& p) const {
 
121
        return _toHandle < p._toHandle;
 
122
    }
 
123
 
 
124
    /** Equal operator */
 
125
    bool operator==(const Reference<T>& p) const {
 
126
        return _toHandle == p._toHandle;
 
127
    }
 
128
 
 
129
    bool operator!=(const Reference<T>& p) const {
 
130
        return _toHandle != p._toHandle;
 
131
    }
 
132
 
 
133
 
 
134
    //**************************************************************************
 
135
    // checking on the state
 
136
 
 
137
    /// Test if it handles something
 
138
    bool isValid(void) const {
 
139
        return _toHandle != 0;
 
140
    }
 
141
 
 
142
    /// Test if it does not handle anything
 
143
    bool isNull(void) const {
 
144
        return _toHandle == 0;
 
145
    }
 
146
 
 
147
    /// Get number of references on the object, including this one
 
148
    long getRefCount(void) const {
 
149
        if (_toHandle)
 
150
            return _toHandle->getRefCount();
 
151
        return 0;
 
152
    }
 
153
 
 
154
private:
 
155
    T *_toHandle; /** the pointer to the handled object */
 
156
};
 
157
 
 
158
 
 
159
/** Handled class
 
160
 *  Implementation of the reference counting pattern.
 
161
 */
 
162
class BaseExport Handled
 
163
{
 
164
public:
 
165
    Handled();
 
166
    virtual ~Handled();
 
167
 
 
168
    void  ref() const;
 
169
    void  unref() const;
 
170
 
 
171
    long getRefCount(void) const {
 
172
        return _lRefCount;
 
173
    }
 
174
    const Handled& operator = (const Handled&);
 
175
 
 
176
private:
 
177
    long _lRefCount;
 
178
};
 
179
 
 
180
} // namespace Base
 
181
 
 
182
#endif // BASE_HANDLE_H