Handle.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   (c) Jürgen Riegel (juergen.riegel@web.de) 2002                        *
00003  *                                                                         *
00004  *   This file is part of the FreeCAD CAx development system.              *
00005  *                                                                         *
00006  *   This program is free software; you can redistribute it and/or modify  *
00007  *   it under the terms of the GNU Library General Public License (LGPL)   *
00008  *   as published by the Free Software Foundation; either version 2 of     *
00009  *   the License, or (at your option) any later version.                   *
00010  *   for detail see the LICENCE text file.                                 *
00011  *                                                                         *
00012  *   FreeCAD is distributed in the hope that it will be useful,            *
00013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00015  *   GNU Library General Public License for more details.                  *
00016  *                                                                         *
00017  *   You should have received a copy of the GNU Library General Public     *
00018  *   License along with FreeCAD; if not, write to the Free Software        *
00019  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  *
00020  *   USA                                                                   *
00021  *                                                                         *
00022  *   Juergen Riegel 2002                                                   *
00023  ***************************************************************************/
00024 
00025 
00026 #ifndef BASE_HANDLE_H
00027 #define BASE_HANDLE_H
00028 
00029 // Std. configurations
00030 
00031 #include <string>
00032 #include <map>
00033 #include <typeinfo>
00034 
00035 class QAtomicInt;
00036 
00037 namespace Base
00038 {
00039 
00045 template <class T>
00046 class Reference
00047 {
00048 public:
00049     //**************************************************************************
00050     // construction & destruction
00051 
00053     Reference() : _toHandle(0) {
00054     }
00055 
00056     Reference(T* p) : _toHandle(p) {
00057         if (_toHandle)
00058             _toHandle->ref();
00059     }
00060 
00062     Reference(const Reference<T>& p) : _toHandle(p._toHandle) {
00063         if (_toHandle)
00064             _toHandle->ref();
00065     }
00066 
00072     ~Reference() {
00073         if (_toHandle)
00074             _toHandle->unref();
00075     }
00076 
00077     //**************************************************************************
00078     // operator implementation
00079 
00081     Reference <T>& operator=(T* p) {
00082         // check if we want to reassign the same object
00083         if (_toHandle == p)
00084             return *this;
00085         if (_toHandle)
00086             _toHandle->unref();
00087         _toHandle = p;
00088         if (_toHandle)
00089             _toHandle->ref();
00090         return *this;
00091     }
00092 
00094     Reference <T>& operator=(const Reference<T>& p) {
00095         // check if we want to reassign the same object
00096         if (_toHandle == p._toHandle)
00097             return *this;
00098         if (_toHandle)
00099             _toHandle->unref();
00100         _toHandle = p._toHandle;
00101         if (_toHandle)
00102             _toHandle->ref();
00103         return *this;
00104     }
00105 
00107     T& operator*() const {
00108         return *_toHandle;
00109     }
00110 
00112     T* operator->() const {
00113         return _toHandle;
00114     }
00115 
00116     operator T*() const {
00117         return _toHandle;
00118     }
00119 
00121     bool operator<(const Reference<T>& p) const {
00122         return _toHandle < p._toHandle;
00123     }
00124 
00126     bool operator==(const Reference<T>& p) const {
00127         return _toHandle == p._toHandle;
00128     }
00129 
00130     bool operator!=(const Reference<T>& p) const {
00131         return _toHandle != p._toHandle;
00132     }
00133 
00134 
00135     //**************************************************************************
00136     // checking on the state
00137 
00139     bool isValid(void) const {
00140         return _toHandle != 0;
00141     }
00142 
00144     bool isNull(void) const {
00145         return _toHandle == 0;
00146     }
00147 
00149     int getRefCount(void) const {
00150         if (_toHandle)
00151             return _toHandle->getRefCount();
00152         return 0;
00153     }
00154 
00155 private:
00156     T *_toHandle; 
00157 };
00158 
00162 class BaseExport Handled
00163 {
00164 public:
00165     Handled();
00166     virtual ~Handled();
00167 
00168     void ref() const;
00169     void unref() const;
00170 
00171     int getRefCount(void) const;
00172     const Handled& operator = (const Handled&);
00173 
00174 private:
00175     QAtomicInt* _lRefCount;
00176 };
00177 
00178 } // namespace Base
00179 
00180 #endif // BASE_HANDLE_H

Generated on Wed Nov 23 19:00:17 2011 for FreeCAD by  doxygen 1.6.1