PyExport.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) Jürgen Riegel          (juergen.riegel@web.de) 2002     *
00003  *                                                                         *
00004  *   This file is part of the FreeCAD CAx development system.              *
00005  *                                                                         *
00006  *   This library is free software; you can redistribute it and/or         *
00007  *   modify it under the terms of the GNU Library General Public           *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2 of the License, or (at your option) any later version.      *
00010  *                                                                         *
00011  *   This library  is distributed in the hope that it will be useful,      *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00014  *   GNU Library General Public License for more details.                  *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Library General Public     *
00017  *   License along with this library; see the file COPYING.LIB. If not,    *
00018  *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
00019  *   Suite 330, Boston, MA  02111-1307, USA                                *
00020  *                                                                         *
00021  ***************************************************************************/
00022 
00023 
00031 #ifndef BASE_PYEXPORT_H
00032 #define BASE_PYEXPORT_H
00033 
00034 // (re-)defined in pyconfig.h
00035 #if defined (_POSIX_C_SOURCE)
00036 #   undef    _POSIX_C_SOURCE
00037 #endif
00038 #if defined (_XOPEN_SOURCE)
00039 #   undef    _XOPEN_SOURCE
00040 #endif
00041 
00042 #include <Python.h>
00043 
00044 namespace Base
00045 {
00046 class PyObjectBase;
00047 
00081 //class BaseExport PyHandler
00082 //{
00083 //public:
00084 //      void IncRef(void);
00085 //      void DecRef(void);
00086 //  virtual ~PyHandler();
00087 //      virtual PyObjectBase *GetPyObject(void)=0;
00088 //
00089 //};
00090 
00091 
00103 template <class HandledType>
00104 class PyHandle
00105 {
00106 public:
00107     //**************************************************************************
00108     // construction destruction
00109 
00115     PyHandle(HandledType *ToHandel=0L)
00116             :_pHandels(ToHandel) {
00117         if (_pHandels)
00118             _pHandels->IncRef();
00119     }
00120 
00122     PyHandle(const PyHandle <HandledType> &ToHandel)
00123             :_pHandels(ToHandel._pHandels) {
00124         if (_pHandels)
00125             _pHandels->IncRef();
00126     }
00127 
00133     ~PyHandle() {
00134         if (_pHandels)
00135             _pHandels->DecRef();
00136     }
00137 
00138     //**************************************************************************
00139     // operator implementation
00140 
00141     // assign operator from a pointer
00142     PyHandle <HandledType>  &operator=(/*const*/ HandledType* other) {
00143         if (_pHandels)
00144             _pHandels->DecRef();
00145         // FIXME: Should be without "->_pHandels", shouldn't it? (Werner)
00146         _pHandels = other;//_pHandels = other->_pHandels;
00147         if (_pHandels)
00148             _pHandels->IncRef();
00149         return *this;
00150     }
00151 
00152     // assign operator from a handle
00153     PyHandle <HandledType>  &operator=(const PyHandle <HandledType> &other) {
00154         if (_pHandels)
00155             _pHandels->DecRef();
00156         _pHandels = other._pHandels;
00157         if (_pHandels)
00158             _pHandels->IncRef();
00159         return *this;
00160     }
00161 
00163     HandledType &operator*() {
00164         return *_pHandels;
00165     }
00166 
00168     HandledType *operator->() {
00169         return _pHandels;
00170     }
00171 
00173     const HandledType &operator*() const {
00174         return _pHandels;
00175     }
00176 
00178     const HandledType *operator->() const {
00179         return _pHandels;
00180     }
00181 
00185     bool operator<(const PyHandle<HandledType> &other) const {
00186         //return _pHandels<&other;
00187         // FIXME: Shouldn't we compare both pointers?. (Werner)
00188         return _pHandels<other._pHandels;
00189     }
00190 
00192     bool operator==(const PyHandle<HandledType> &other) const {
00193         //return _pHandels==&other;
00194         // FIXME: Shouldn't we compare both pointers?. (Werner)
00195         return _pHandels==other._pHandels;
00196     }
00197 
00199     PyObject* getPyObject(void) const {
00200         // return (PyObject*) _pHandels;
00201         // FIXME: Shouldn't we return the pointer's object?. (Werner)
00202         return const_cast<HandledType*>(_pHandels)->getPyObject();
00203     }
00204     //**************************************************************************
00205     // checking on the state
00206 
00208     bool IsValid(void) const {
00209         return _pHandels!=0;
00210     }
00211 
00213     bool IsNull(void) const {
00214         return _pHandels==0;
00215     }
00216 
00217 private:
00219     HandledType *_pHandels;
00220 
00221 };
00222 
00223 
00224 } // namespace Base
00225 
00226 #endif // BASE_PYEXPORT_H

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