OpenLexocad  27.1
Handle.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (c) 2002 Jürgen Riegel <juergen.riegel@web.de> *
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  ***************************************************************************/
23 
24 
25 #ifndef BASE_HANDLE_H
26 #define BASE_HANDLE_H
27 
28 // Std. configurations
29 
30 #include <string>
31 #include <map>
32 #include <typeinfo>
33 
34 class QAtomicInt;
35 
36 namespace Base
37 {
38 
44 template <class T>
45 class Reference
46 {
47 public:
48  //**************************************************************************
49  // construction & destruction
50 
52  Reference() : _toHandle(0) {
53  }
54 
55  Reference(T* p) : _toHandle(p) {
56  if (_toHandle)
57  _toHandle->ref();
58  }
59 
61  Reference(const Reference<T>& p) : _toHandle(p._toHandle) {
62  if (_toHandle)
63  _toHandle->ref();
64  }
65 
72  if (_toHandle)
73  _toHandle->unref();
74  }
75 
76  //**************************************************************************
77  // operator implementation
78 
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 
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 
106  T& operator*() const {
107  return *_toHandle;
108  }
109 
111  T* operator->() const {
112  return _toHandle;
113  }
114 
115  operator T*() const {
116  return _toHandle;
117  }
118 
120  bool operator<(const Reference<T>& p) const {
121  return _toHandle < p._toHandle;
122  }
123 
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 
138  bool isValid(void) const {
139  return _toHandle != 0;
140  }
141 
143  bool isNull(void) const {
144  return _toHandle == 0;
145  }
146 
148  int getRefCount(void) const {
149  if (_toHandle)
150  return _toHandle->getRefCount();
151  return 0;
152  }
153 
154 private:
155  T *_toHandle;
156 };
157 
161 class LX_BASE_EXPORT Handled
162 {
163 public:
164  Handled();
165  virtual ~Handled();
166 
167  void ref() const;
168  void unref() const;
169 
170  int getRefCount(void) const;
171  const Handled& operator = (const Handled&);
172 
173 private:
174  Handled(const Handled&);
175 
176 private:
177  QAtomicInt* _lRefCount;
178 };
179 
180 } // namespace Base
181 
182 #endif // BASE_HANDLE_H
Reference(const Reference< T > &p)
Definition: Handle.h:61
int getRefCount(void) const
Get number of references on the object, including this one.
Definition: Handle.h:148
Reference< T > & operator=(T *p)
Definition: Handle.h:80
bool operator<(const Reference< T > &p) const
Definition: Handle.h:120
Reference(T *p)
Definition: Handle.h:55
Reference< T > & operator=(const Reference< T > &p)
Definition: Handle.h:93
Reference()
Definition: Handle.h:52
bool isNull(void) const
Test if it does not handle anything.
Definition: Handle.h:143
Definition: Handle.h:161
bool isValid(void) const
Test if it handles something.
Definition: Handle.h:138
~Reference()
Definition: Handle.h:71
bool operator==(const Reference< T > &p) const
Definition: Handle.h:125
bool operator!=(const Reference< T > &p) const
Definition: Handle.h:129
Definition: AbstractXMLReader.h:5
T * operator->() const
Definition: Handle.h:111
T & operator *() const
Definition: Handle.h:106
Definition: Handle.h:45