OpenLexocad  28.0
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 #pragma once
26 
27 // Std. configurations
28 
29 
30 
31 class QAtomicInt;
32 
33 namespace Base
34 {
35 
41 template <class T>
42 class Reference
43 {
44 public:
45  //**************************************************************************
46  // construction & destruction
47 
49  Reference() : _toHandle(0) {
50  }
51 
52  Reference(T* p) : _toHandle(p) {
53  if (_toHandle)
54  _toHandle->ref();
55  }
56 
58  Reference(const Reference<T>& p) : _toHandle(p._toHandle) {
59  if (_toHandle)
60  _toHandle->ref();
61  }
62 
69  if (_toHandle)
70  _toHandle->unref();
71  }
72 
73  //**************************************************************************
74  // operator implementation
75 
78  // check if we want to reassign the same object
79  if (_toHandle == p)
80  return *this;
81  if (_toHandle)
82  _toHandle->unref();
83  _toHandle = p;
84  if (_toHandle)
85  _toHandle->ref();
86  return *this;
87  }
88 
91  // check if we want to reassign the same object
92  if (_toHandle == p._toHandle)
93  return *this;
94  if (_toHandle)
95  _toHandle->unref();
96  _toHandle = p._toHandle;
97  if (_toHandle)
98  _toHandle->ref();
99  return *this;
100  }
101 
103  T& operator*() const {
104  return *_toHandle;
105  }
106 
108  T* operator->() const {
109  return _toHandle;
110  }
111 
112  operator T*() const {
113  return _toHandle;
114  }
115 
117  bool operator<(const Reference<T>& p) const {
118  return _toHandle < p._toHandle;
119  }
120 
122  bool operator==(const Reference<T>& p) const {
123  return _toHandle == p._toHandle;
124  }
125 
126  bool operator!=(const Reference<T>& p) const {
127  return _toHandle != p._toHandle;
128  }
129 
130 
131  //**************************************************************************
132  // checking on the state
133 
135  bool isValid(void) const {
136  return _toHandle != 0;
137  }
138 
140  bool isNull(void) const {
141  return _toHandle == 0;
142  }
143 
145  int getRefCount(void) const {
146  if (_toHandle)
147  return _toHandle->getRefCount();
148  return 0;
149  }
150 
151 private:
152  T *_toHandle;
153 };
154 
158 class LX_BASE_EXPORT Handled
159 {
160 public:
162  virtual ~Handled();
163 
164  void ref() const;
165  void unref() const;
166 
167  int getRefCount(void) const;
168  const Handled& operator = (const Handled&);
169 
170 private:
171  Handled(const Handled&);
172 
173 private:
174  QAtomicInt* _lRefCount;
175 };
176 
177 } // namespace Base
Base::Handled::Handled
Handled()
Base::Handled::getRefCount
int getRefCount(void) const
Base::Handled::ref
void ref() const
Base::Handled
Definition: Handle.h:159
Base::Reference::operator=
Reference< T > & operator=(const Reference< T > &p)
Definition: Handle.h:90
Base::Reference::operator*
T & operator*() const
Definition: Handle.h:103
Base::Reference::operator=
Reference< T > & operator=(T *p)
Definition: Handle.h:77
Base::Handled::unref
void unref() const
Base::Reference::~Reference
~Reference()
Definition: Handle.h:68
Base::Reference::Reference
Reference(T *p)
Definition: Handle.h:52
Base::Reference::getRefCount
int getRefCount(void) const
Get number of references on the object, including this one.
Definition: Handle.h:145
Base::Reference::operator==
bool operator==(const Reference< T > &p) const
Definition: Handle.h:122
Base::Handled::~Handled
virtual ~Handled()
Base::Reference
Definition: Handle.h:43
Base::Reference::isValid
bool isValid(void) const
Test if it handles something.
Definition: Handle.h:135
Base::Reference::operator!=
bool operator!=(const Reference< T > &p) const
Definition: Handle.h:126
Base::Reference::Reference
Reference()
Definition: Handle.h:49
Base::Reference::operator<
bool operator<(const Reference< T > &p) const
Definition: Handle.h:117
Base::Reference::operator->
T * operator->() const
Definition: Handle.h:108
Base::Reference::isNull
bool isNull(void) const
Test if it does not handle anything.
Definition: Handle.h:140
Base::Reference::Reference
Reference(const Reference< T > &p)
Definition: Handle.h:58
Base
Definition: AbstractXMLReader.h:5