Developer Documentation
PropertyManager.hh
1 #ifndef OPENMESH_PYTHON_PROPERTYMANAGER_HH
2 #define OPENMESH_PYTHON_PROPERTYMANAGER_HH
3 
4 #include "Python/Bindings.hh"
5 #include "OpenMesh/Core/Utils/PropertyManager.hh"
6 
7 namespace OpenMesh {
8 namespace Python {
9 
10 BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(retain_overloads, retain, 0, 1)
11 
12 
23 template <class PropertyManager, class IndexHandle>
24 object propman_get_item(PropertyManager& _self, IndexHandle _handle) {
25  return _self[_handle];
26 }
27 
38 template <class PropertyManager, class IndexHandle>
39 void propman_set_item(PropertyManager& _self, IndexHandle _handle, object _value) {
40  _self[_handle] = _value;
41 }
42 
54 template <class PropertyManager, class Iterator>
55 void propman_set_range(PropertyManager& _self, Iterator _it, object _value) {
56  try {
57  while (true) {
58  _self[_it.next()] = _value;
59  }
60  }
61  catch (error_already_set exception) {
62  // This is expected behavior
63  PyErr_Clear();
64  }
65 }
66 
76 template <class PropertyManager, class Mesh>
77 bool property_exists(Mesh& _mesh, const char *_propname) {
78  return PropertyManager::propertyExists(_mesh, _propname);
79 }
80 
96 template <class PropHandle, class IndexHandle, class Iterator>
97 void expose_property_manager(const char *_name) {
98  // Convenience typedef
100 
101  // Function pointers
102  void (PropertyManager::*retain)(bool) = &PropertyManager::retain;
103 
104  object (*getitem)(PropertyManager&, IndexHandle ) = &propman_get_item;
105  void (*setitem)(PropertyManager&, IndexHandle, object) = &propman_set_item;
106 
107  void (*set_range)(PropertyManager&, Iterator, object) = &propman_set_range;
108 
109  bool (*property_exists_poly)(PolyMesh&, const char *) = &property_exists<PropertyManager, PolyMesh>;
110  bool (*property_exists_tri )(TriMesh&, const char *) = &property_exists<PropertyManager, TriMesh >;
111 
112  // Expose property manager
113  class_<PropertyManager, boost::noncopyable>(_name)
114  .def(init<PolyMesh&, const char *, optional<bool> >()[with_custodian_and_ward<1,2>()])
115  .def(init<TriMesh&, const char *, optional<bool> >()[with_custodian_and_ward<1,2>()])
116 
117  .def("swap", &PropertyManager::swap)
118  .def("is_valid", &PropertyManager::isValid)
119 
120  .def("__bool__", &PropertyManager::operator bool)
121  .def("__nonzero__", &PropertyManager::operator bool)
122 
123  .def("get_raw_property", &PropertyManager::getRawProperty, return_value_policy<copy_const_reference>())
124  .def("get_name", &PropertyManager::getName, return_value_policy<copy_const_reference>())
125  .def("get_mesh", &PropertyManager::getMesh, return_value_policy<reference_existing_object>())
126 
127  .def("retain", retain, retain_overloads())
128 
129  .def("__getitem__", getitem)
130  .def("__setitem__", setitem)
131 
132  .def("set_range", set_range)
133 
134  .def("property_exists", property_exists_poly)
135  .def("property_exists", property_exists_tri)
136  .staticmethod("property_exists")
137  ;
138 }
139 
140 } // namespace OpenMesh
141 } // namespace Python
142 
143 #endif
void propman_set_range(PropertyManager &_self, Iterator _it, object _value)
bool property_exists(Mesh &_mesh, const char *_propname)
object propman_get_item(PropertyManager &_self, IndexHandle _handle)
void expose_property_manager(const char *_name)
void retain(bool doRetain=true)
Disable lifecycle management for this property.
void propman_set_item(PropertyManager &_self, IndexHandle _handle, object _value)