00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef OPENMESH_AutoPropertyHandleT_HH
00027 #define OPENMESH_AutoPropertyHandleT_HH
00028
00029
00030 #include <assert.h>
00031 #include <string>
00032
00033
00034
00035 namespace OpenMesh {
00036
00037
00038
00039 template <class Mesh_, class PropertyHandle_>
00040 class AutoPropertyHandleT : public PropertyHandle_
00041 {
00042 public:
00043 typedef Mesh_ Mesh;
00044 typedef PropertyHandle_ PropertyHandle;
00045 typedef PropertyHandle Base;
00046 typedef typename PropertyHandle::Value Value;
00047 typedef AutoPropertyHandleT<Mesh, PropertyHandle>
00048 Self;
00049 protected:
00050 Mesh* m_;
00051 bool own_property_;
00052
00053 public:
00054 AutoPropertyHandleT()
00055 : m_(NULL), own_property_(false)
00056 {}
00057
00058 AutoPropertyHandleT(const Self& _other)
00059 : Base(_other.idx()), m_(_other.m_), own_property_(false)
00060 {}
00061
00062 explicit AutoPropertyHandleT(Mesh& _m, const std::string& _pp_name = std::string())
00063 { add_property(_m, _pp_name); }
00064
00065 AutoPropertyHandleT(Mesh& _m, PropertyHandle _pph)
00066 : Base(_pph.idx()), m_(&_m), own_property_(false)
00067 {}
00068
00069 ~AutoPropertyHandleT()
00070 {
00071 if (own_property_)
00072 {
00073 m_->remove_property(*this);
00074 }
00075 }
00076
00077 inline void add_property(Mesh& _m, const std::string& _pp_name = std::string())
00078 {
00079 assert(!is_valid());
00080 m_ = &_m;
00081 own_property_ = _pp_name.empty() || !m_->get_property_handle(*this, _pp_name);
00082 if (own_property_)
00083 {
00084 m_->add_property(*this, _pp_name);
00085 }
00086 }
00087
00088 inline void remove_property()
00089 {
00090 assert(own_property_);
00091 m_->remove_property(*this);
00092 own_property_ = false;
00093 invalidate();
00094 }
00095
00096 template <class _Handle>
00097 inline Value& operator [] (_Handle _hnd)
00098 { return m_->property(*this, _hnd); }
00099
00100 template <class _Handle>
00101 inline const Value& operator [] (_Handle _hnd) const
00102 { return m_->property(*this, _hnd); }
00103
00104 inline bool own_property() const
00105 { return own_property_; }
00106
00107 inline void free_property()
00108 { own_property_ = false; }
00109 };
00110
00111
00112 }
00113
00114 #endif // OPENMESH_AutoPropertyHandleT_HH defined
00115