web/lib/django/contrib/gis/gdal/base.py
changeset 0 0d40e90630ef
child 29 cc9b7e14412b
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/web/lib/django/contrib/gis/gdal/base.py	Wed Jan 20 00:34:04 2010 +0100
@@ -0,0 +1,35 @@
+from ctypes import c_void_p
+from types import NoneType
+from django.contrib.gis.gdal.error import GDALException
+
+class GDALBase(object):
+    """
+    Base object for GDAL objects that has a pointer access property
+    that controls access to the underlying C pointer.
+    """
+    # Initially the pointer is NULL.
+    _ptr = None
+
+    # Default allowed pointer type.
+    ptr_type = c_void_p
+
+    # Pointer access property.
+    def _get_ptr(self):
+        # Raise an exception if the pointer isn't valid don't
+        # want to be passing NULL pointers to routines --
+        # that's very bad.
+        if self._ptr: return self._ptr
+        else: raise GDALException('GDAL %s pointer no longer valid.' % self.__class__.__name__)
+
+    def _set_ptr(self, ptr):
+        # Only allow the pointer to be set with pointers of the
+        # compatible type or None (NULL).
+        if isinstance(ptr, int):
+            self._ptr = self.ptr_type(ptr)
+        elif isinstance(ptr, (self.ptr_type, NoneType)):
+            self._ptr = ptr
+        else:
+            raise TypeError('Incompatible pointer type')
+
+    ptr = property(_get_ptr, _set_ptr)
+