OpenMesh
OpenMesh/Core/Utils/AutoPropertyHandleT.hh
00001 /*===========================================================================*\
00002  *                                                                           *
00003  *                               OpenMesh                                    *
00004  *      Copyright (C) 2001-2011 by Computer Graphics Group, RWTH Aachen      *
00005  *                           www.openmesh.org                                *
00006  *                                                                           *
00007  *---------------------------------------------------------------------------* 
00008  *  This file is part of OpenMesh.                                           *
00009  *                                                                           *
00010  *  OpenMesh is free software: you can redistribute it and/or modify         * 
00011  *  it under the terms of the GNU Lesser General Public License as           *
00012  *  published by the Free Software Foundation, either version 3 of           *
00013  *  the License, or (at your option) any later version with the              *
00014  *  following exceptions:                                                    *
00015  *                                                                           *
00016  *  If other files instantiate templates or use macros                       *
00017  *  or inline functions from this file, or you compile this file and         *
00018  *  link it with other files to produce an executable, this file does        *
00019  *  not by itself cause the resulting executable to be covered by the        *
00020  *  GNU Lesser General Public License. This exception does not however       *
00021  *  invalidate any other reasons why the executable file might be            *
00022  *  covered by the GNU Lesser General Public License.                        *
00023  *                                                                           *
00024  *  OpenMesh is distributed in the hope that it will be useful,              *
00025  *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
00026  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
00027  *  GNU Lesser General Public License for more details.                      *
00028  *                                                                           *
00029  *  You should have received a copy of the GNU LesserGeneral Public          *
00030  *  License along with OpenMesh.  If not,                                    *
00031  *  see <http://www.gnu.org/licenses/>.                                      *
00032  *                                                                           *
00033 \*===========================================================================*/ 
00034 
00035 /*===========================================================================*\
00036  *                                                                           *             
00037  *   $Revision: 362 $                                                         *
00038  *   $Date: 2011-01-26 10:21:12 +0100 (Mi, 26 Jan 2011) $                   *
00039  *                                                                           *
00040 \*===========================================================================*/
00041 
00042 #ifndef OPENMESH_AutoPropertyHandleT_HH
00043 #define OPENMESH_AutoPropertyHandleT_HH
00044 
00045 //== INCLUDES =================================================================
00046 #include <assert.h>
00047 #include <string>
00048 
00049 //== NAMESPACES ===============================================================
00050 
00051 namespace OpenMesh {
00052 
00053 //== CLASS DEFINITION =========================================================
00054 
00055 template <class Mesh_, class PropertyHandle_>
00056 class AutoPropertyHandleT : public PropertyHandle_
00057 {
00058 public:
00059   typedef Mesh_                             Mesh;
00060   typedef PropertyHandle_                   PropertyHandle;
00061   typedef PropertyHandle                    Base;
00062   typedef typename PropertyHandle::Value    Value;
00063   typedef AutoPropertyHandleT<Mesh, PropertyHandle>
00064                                             Self;
00065 protected:
00066   Mesh*                                     m_;
00067   bool                                      own_property_;//ref counting?
00068 
00069 public:
00070   AutoPropertyHandleT()
00071   : m_(NULL), own_property_(false)
00072   {}
00073   
00074   AutoPropertyHandleT(const Self& _other)
00075   : Base(_other.idx()), m_(_other.m_), own_property_(false)
00076   {}
00077   
00078   explicit AutoPropertyHandleT(Mesh& _m, const std::string& _pp_name = std::string())
00079   { add_property(_m, _pp_name); }
00080 
00081   AutoPropertyHandleT(Mesh& _m, PropertyHandle _pph)
00082   : Base(_pph.idx()), m_(&_m), own_property_(false)
00083   {}
00084 
00085   ~AutoPropertyHandleT()
00086   {
00087     if (own_property_)
00088     {
00089       m_->remove_property(*this);
00090     }
00091   }
00092 
00093   inline void                               add_property(Mesh& _m, const std::string& _pp_name = std::string())
00094   {
00095     assert(!is_valid());
00096     m_ = &_m;
00097     own_property_ = _pp_name.empty() || !m_->get_property_handle(*this, _pp_name);
00098     if (own_property_)
00099     {
00100       m_->add_property(*this, _pp_name);
00101     }
00102   }
00103   
00104   inline void                               remove_property()
00105   {
00106     assert(own_property_);//only the owner can delete the property
00107     m_->remove_property(*this);
00108     own_property_ = false;
00109     invalidate();
00110   }
00111   
00112   template <class _Handle>
00113   inline Value&                             operator [] (_Handle _hnd)
00114   { return m_->property(*this, _hnd); }
00115 
00116   template <class _Handle>
00117   inline const Value&                       operator [] (_Handle _hnd) const
00118   { return m_->property(*this, _hnd); }
00119 
00120   inline bool                               own_property() const
00121   { return own_property_; }
00122 
00123   inline void                               free_property()
00124   { own_property_ = false; }
00125 };
00126 
00127 //=============================================================================
00128 } // namespace OpenMesh
00129 //=============================================================================
00130 #endif // OPENMESH_AutoPropertyHandleT_HH defined
00131 //=============================================================================