1. Johannes Kolb
  2. pyosmium

Commits

Johannes Kolb  committed ce7a531

use non-const pointers internally in OSM objects

  • Participants
  • Parent commits b1902e4
  • Branches default

Comments (0)

Files changed (6)

File c_osmium/pointers.pxi

View file
 # This hack is needed until we have good template support in Cython
 
 cdef extern from *:
-    cdef cppclass ObjectPtr "shared_ptr<const Osmium::OSM::Object>":
+    cdef cppclass ObjectPtr "shared_ptr<Osmium::OSM::Object>":
         Object * get()
         void reset()
 
-    cdef cppclass NodePtr "shared_ptr<const Osmium::OSM::Node>"(ObjectPtr):
+    cdef cppclass NodePtr "shared_ptr<Osmium::OSM::Node>"(ObjectPtr):
         pass
 
-    cdef cppclass WayPtr "shared_ptr<const Osmium::OSM::Way>"(ObjectPtr):
+    cdef cppclass WayPtr "shared_ptr<Osmium::OSM::Way>"(ObjectPtr):
         pass
 
-    cdef cppclass RelationPtr "shared_ptr<const Osmium::OSM::Relation>"(ObjectPtr):
+    cdef cppclass RelationPtr "shared_ptr<Osmium::OSM::Relation>"(ObjectPtr):
         pass
 
-    cdef cppclass AreaPtr "shared_ptr<const Osmium::OSM::Area>"(ObjectPtr):
+    cdef cppclass AreaPtr "shared_ptr<Osmium::OSM::Area>"(ObjectPtr):
         pass
 
     ctypedef ObjectPtr ConstObjectPtr "const shared_ptr<const Osmium::OSM::Object>"
     ctypedef RelationPtr ConstRelationPtr "const shared_ptr<const Osmium::OSM::Relation>"
     ctypedef AreaPtr ConstAreaPtr "const shared_ptr<const Osmium::OSM::Area>"
 
-    NodePtr &castNodePtr "boost::static_pointer_cast<const Osmium::OSM::Node>"(ObjectPtr&)
-    WayPtr &castWayPtr "boost::static_pointer_cast<const Osmium::OSM::Way>"(ObjectPtr&)
-    RelationPtr &castRelationPtr "boost::static_pointer_cast<const Osmium::OSM::Relation>"(ObjectPtr&)
-    AreaPtr &castAreaPtr "boost::static_pointer_cast<const Osmium::OSM::Area>"(ObjectPtr&)
+    NodePtr &castNodePtr "boost::static_pointer_cast<Osmium::OSM::Node>"(ObjectPtr&)
+    WayPtr &castWayPtr "boost::static_pointer_cast<Osmium::OSM::Way>"(ObjectPtr&)
+    RelationPtr &castRelationPtr "boost::static_pointer_cast<Osmium::OSM::Relation>"(ObjectPtr&)
+    AreaPtr &castAreaPtr "boost::static_pointer_cast<Osmium::OSM::Area>"(ObjectPtr&)
+
+    ConstObjectPtr &castConstObjectPtr "boost::static_pointer_cast<const Osmium::OSM::Object>"(ConstObjectPtr&)
+    ObjectPtr &castConstAwayObjectPtr "boost::const_pointer_cast<Osmium::OSM::Object>"(ConstObjectPtr&)

File osmium/handler.pyx

View file
 from _callback_handler cimport CallbackHandler
 
 from c_osmium.handler cimport Debug as c_Debug, Progress as c_Progress
-from c_osmium.osm cimport NodePtr, castNodePtr, castWayPtr, castRelationPtr, Meta as c_Meta
+from c_osmium.osm cimport ConstObjectPtr, ObjectPtr, NodePtr, castNodePtr, castWayPtr, castRelationPtr, Meta as c_Meta, castConstObjectPtr, castConstAwayObjectPtr
 from osmium.osm cimport _cache_node, _cache_taglist, _cache_way, _cache_waynodelist, _cache_relation, _cache_relationmemberlist, _create_ro_node, _create_ro_way, _create_ro_relation
 
 

File osmium/handler_callback_impl.pxi

View file
 cdef public inline int c_node_callback(Base handler, ConstNodePtr &node) except -1:
     global _cache_node, _cache_taglist
 
-    _cache_node.c_object[0] = node
+    # deliberately cast the const away, _RONode prevents changing anyway
+    _cache_node.c_object[0] = castConstAwayObjectPtr(castConstObjectPtr(node))
     handler.node(_cache_node)
 
     if (<PyObject*>_cache_node).ob_refcnt == 1:
 cdef public inline int c_way_callback(Base handler, ConstWayPtr &way) except -1:
     global _cache_way, _cache_taglist, _cache_waynodelist
 
-    _cache_way.c_object[0] = way
+    # deliberately cast the const away, _ROWay prevents changing anyway
+    _cache_way.c_object[0] = castConstAwayObjectPtr(castConstObjectPtr(way))
     handler.way(_cache_way)
 
     if (<PyObject*>_cache_way).ob_refcnt == 1:
 cdef public inline int c_relation_callback(Base handler, ConstRelationPtr &rel) except -1:
     global _cache_relation, _cache_taglist, _cache_relationmemberlist
 
-    _cache_relation.c_object[0] = rel
+    # deliberately cast the const away, _RORelation prevents changing anyway
+    _cache_relation.c_object[0] = castConstAwayObjectPtr(castConstObjectPtr(rel))
     handler.relation(_cache_relation)
 
     if (<PyObject*>_cache_relation).ob_refcnt == 1:

File osmium/osm.pyx

View file
 """
 
 from c_osmium.osm cimport ConstObjectPtr, ConstRelationPtr, Object as c_Object, Node as c_Node, TagList as c_TagList, TagListIterator as c_TagListIterator, Way as c_Way, Relation as c_Relation, WayNodeList as c_WayNodeList, WayNodeListIterator as c_WayNodeListIterator, RelationMemberList as c_RelationMemberList, RelationMemberListIterator as c_RelationMemberListIterator, castWayPtr, castRelationPtr
+cimport cython
 from cpython.ref cimport PyObject
 
 from cpython cimport PyTypeObject    

File osmium/osm_obj_ro.pxi

View file
 # Creation of these objects from Python is prohibited. They are only useful
 # from C++ where we can access the contained OSM object directly.
 
+@cython.final
 cdef class _RONode(Node):
     def __init__(self):
         raise TypeError("Class _RONode is not instanceable from Python")
     def __setattr__(self, name, value):
         raise AttributeError("attribute '"+name+"' of 'osmium.osm._RONode' objects is not writeable")
 
+@cython.final
 cdef class _ROWay(Way):
     def __init__(self):
         raise TypeError("Class _ROWay is not instanceable from Python")
     def __setattr__(self, name, value):
         raise AttributeError("attribute '"+name+"' of 'osmium.osm._ROWay' objects is not writeable")
 
+@cython.final
 cdef class _RORelation(Relation):
     def __init__(self):
         raise TypeError("Class _RORelation is not instanceable from Python")

File osmium/osm_ptr.pxi

View file
     ctypedef c_TagList * TagListCPtr "Osmium::OSM::TagList const*"
 
 
-cdef inline ObjectCPtr _getOsmObject(ConstObjectPtr *ptr) except NULL:
+cdef inline c_Object* _getOsmObject(ObjectPtr *ptr) except NULL:
     """Get object pointer from smart pointer and ensure it is not null
     """
-    cdef ObjectCPtr retval = ptr.get()
+    cdef c_Object* retval = ptr.get()
     assert retval is not NULL, u"invalid ObjectPtr"
     return retval
 
-cdef inline c_TagList* _getOsmTagList(ConstObjectPtr *ptr) except NULL:
+cdef inline c_TagList* _getOsmTagList(ObjectPtr *ptr) except NULL:
     """Get TagList from object pointer
     """
-    cdef ObjectCPtr obj = ptr.get()
+    cdef c_Object* obj = ptr.get()
     assert obj is not NULL, u"invalid ObjectPtr"
     return <c_TagList*>(&obj.tags())
 
-cdef inline NodeCPtr _getOsmNode(ConstObjectPtr *ptr) except NULL:
+cdef inline c_Node* _getOsmNode(ObjectPtr *ptr) except NULL:
     """Get object pointer from smart pointer and cast to node pointer
     """
-    cdef NodeCPtr retval = <NodeCPtr>ptr.get()
+    cdef c_Node* retval = <c_Node*>ptr.get()
     assert retval is not NULL, u"invalid ObjectPtr"
     return retval
 
-cdef inline WayCPtr _getOsmWay(ConstObjectPtr *ptr) except NULL:
+cdef inline c_Way* _getOsmWay(ObjectPtr *ptr) except NULL:
     """Get object pointer from smart pointer and cast to way pointer
     """
-    cdef WayCPtr retval = <WayCPtr>ptr.get()
+    cdef c_Way* retval = <c_Way*>ptr.get()
     assert retval is not NULL, u"invalid ObjectPtr"
     return retval
 
 cdef inline c_WayNodeList* _getOsmWayNodeList(WayPtr *ptr) except NULL:
     """Get TagList from object pointer
     """
-    cdef WayCPtr obj = <WayCPtr>ptr.get()
+    cdef c_Way* obj = <c_Way*>ptr.get()
     assert obj is not NULL, u"invalid ObjectPtr"
     return <c_WayNodeList*>(&obj.nodes())
 
-cdef inline c_RelationMemberList* _getOsmRelMemList(ConstRelationPtr *ptr) except NULL:
+cdef inline c_RelationMemberList* _getOsmRelMemList(RelationPtr *ptr) except NULL:
     """Get object pointer from smart pointer and cast to relation pointer
     """
-    cdef RelCPtr obj = <RelCPtr>ptr.get()
+    cdef c_Relation* obj = <c_Relation*>ptr.get()
     assert obj is not NULL, u"invalid ObjectPtr"
     return <c_RelationMemberList*>(&obj.members())