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
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 #ifndef OPENMESH_AutoPropertyHandleT_HH
00043 #define OPENMESH_AutoPropertyHandleT_HH
00044
00045
00046 #include <assert.h>
00047 #include <string>
00048
00049
00050
00051 namespace OpenMesh {
00052
00053
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_;
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_);
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 }
00129
00130 #endif // OPENMESH_AutoPropertyHandleT_HH defined
00131