OpenMesh

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
AutoPropertyHandleT.hh
1 /*===========================================================================*\
2 * *
3 * OpenMesh *
4 * Copyright (C) 2001-2012 by Computer Graphics Group, RWTH Aachen *
5 * www.openmesh.org *
6 * *
7 *---------------------------------------------------------------------------*
8 * This file is part of OpenMesh. *
9 * *
10 * OpenMesh is free software: you can redistribute it and/or modify *
11 * it under the terms of the GNU Lesser General Public License as *
12 * published by the Free Software Foundation, either version 3 of *
13 * the License, or (at your option) any later version with the *
14 * following exceptions: *
15 * *
16 * If other files instantiate templates or use macros *
17 * or inline functions from this file, or you compile this file and *
18 * link it with other files to produce an executable, this file does *
19 * not by itself cause the resulting executable to be covered by the *
20 * GNU Lesser General Public License. This exception does not however *
21 * invalidate any other reasons why the executable file might be *
22 * covered by the GNU Lesser General Public License. *
23 * *
24 * OpenMesh is distributed in the hope that it will be useful, *
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
27 * GNU Lesser General Public License for more details. *
28 * *
29 * You should have received a copy of the GNU LesserGeneral Public *
30 * License along with OpenMesh. If not, *
31 * see <http://www.gnu.org/licenses/>. *
32 * *
33 \*===========================================================================*/
34
35 /*===========================================================================*\
36 * *
37 * $Revision: 736 $ *
38 * $Date: 2012-10-08 09:30:49 +0200 (Mo, 08 Okt 2012) $ *
39 * *
40 \*===========================================================================*/
41
42 #ifndef OPENMESH_AutoPropertyHandleT_HH
43 #define OPENMESH_AutoPropertyHandleT_HH
44
45 //== INCLUDES =================================================================
46 #include <assert.h>
47 #include <string>
48
49 //== NAMESPACES ===============================================================
50
51 namespace OpenMesh {
52
53 //== CLASS DEFINITION =========================================================
54
55 template <class Mesh_, class PropertyHandle_>
56 class AutoPropertyHandleT : public PropertyHandle_
57 {
58 public:
59 typedef Mesh_ Mesh;
60 typedef PropertyHandle_ PropertyHandle;
61 typedef PropertyHandle Base;
62 typedef typename PropertyHandle::Value Value;
63 typedef AutoPropertyHandleT<Mesh, PropertyHandle>
64 Self;
65 protected:
66 Mesh* m_;
67 bool own_property_;//ref counting?
68
69 public:
70 AutoPropertyHandleT()
71 : m_(NULL), own_property_(false)
72 {}
73
74 AutoPropertyHandleT(const Self& _other)
75 : Base(_other.idx()), m_(_other.m_), own_property_(false)
76 {}
77
78 explicit AutoPropertyHandleT(Mesh& _m, const std::string& _pp_name = std::string())
79 { add_property(_m, _pp_name); }
80
81 AutoPropertyHandleT(Mesh& _m, PropertyHandle _pph)
82 : Base(_pph.idx()), m_(&_m), own_property_(false)
83 {}
84
85 ~AutoPropertyHandleT()
86 {
87 if (own_property_)
88 {
89 m_->remove_property(*this);
90 }
91 }
92
93 inline void add_property(Mesh& _m, const std::string& _pp_name = std::string())
94 {
95 assert(!is_valid());
96 m_ = &_m;
97 own_property_ = _pp_name.empty() || !m_->get_property_handle(*this, _pp_name);
98 if (own_property_)
99 {
100 m_->add_property(*this, _pp_name);
101 }
102 }
103
104 inline void remove_property()
105 {
106 assert(own_property_);//only the owner can delete the property
107 m_->remove_property(*this);
108 own_property_ = false;
109 invalidate();
110 }
111
112 template <class _Handle>
113 inline Value& operator [] (_Handle _hnd)
114 { return m_->property(*this, _hnd); }
115
116 template <class _Handle>
117 inline const Value& operator [] (_Handle _hnd) const
118 { return m_->property(*this, _hnd); }
119
120 inline bool own_property() const
121 { return own_property_; }
122
123 inline void free_property()
124 { own_property_ = false; }
125 };
126
127 //=============================================================================
128 } // namespace OpenMesh
129 //=============================================================================
130 #endif // OPENMESH_AutoPropertyHandleT_HH defined
131 //=============================================================================