home *** CD-ROM | disk | FTP | other *** search
/ Hackers Magazine 57 / CdHackersMagazineNr57.iso / Software / Multimedia / k3d-setup-0.7.11.0.exe / lib / site-packages / OpenGL / GLU / glustruct.py < prev    next >
Encoding:
Python Source  |  2008-12-07  |  3.0 KB  |  87 lines

  1. """Base class for GLU callback-caching structures"""
  2. import ctypes
  3. import weakref
  4.  
  5. class GLUStruct( object ):
  6.     """Mix-in class for GLU Structures that want to retain references to callbacks
  7.     
  8.     Also provides original-object-return for the "datapointer" style paremters
  9.     
  10.     Each sub-class must override:
  11.         CALLBACK_TYPES -- maps a "which" constant to a function type 
  12.         CALLBACK_FUNCTION_REGISTRARS -- maps a "which" constant to the 
  13.             registration function for functions of that type
  14.         WRAPPER_METHODS -- maps a "which" consant to a method of the structure 
  15.             that produces a callable around the function which takes care of 
  16.             input/output arguments, data conversions, error handling and the 
  17.             like.
  18.     Creates a dictionary member dataPointers if original-object-return is used
  19.     Creates a dictionary member callbacks if callback registration is used
  20.     """
  21.     def getAsParam( self ):
  22.         """Gets as a ctypes pointer to the underlying structure"""
  23.         return ctypes.pointer( self )
  24.     _as_parameter_ = property( getAsParam )
  25.     CALLBACK_TYPES = None
  26.     CALLBACK_FUNCTION_REGISTRARS = None
  27.     WRAPPER_METHODS = None
  28.     def noteObject( self, object ):
  29.         """Note object for later retrieval as a Python object pointer
  30.         
  31.         This is the registration point for "original object return", returns 
  32.         a void pointer to the Python object, though this is, effectively, an 
  33.         opaque value.
  34.         """
  35.         identity = id(object)
  36.         try:
  37.             self.dataPointers[ identity ] = object
  38.         except AttributeError, err:
  39.             self.dataPointers = { identity: object }
  40.         return identity
  41.     def originalObject( self, voidPointer ):
  42.         """Given a void-pointer, try to find our original Python object"""
  43.         if isinstance( voidPointer, (int,long)):
  44.             identity = voidPointer
  45.         elif voidPointer is None:
  46.             return None
  47.         else:
  48.             try:
  49.                 identity = voidPointer.value 
  50.             except AttributeError, err:
  51.                 identity = voidPointer[0]
  52.         try:
  53.             return self.dataPointers[ identity ]
  54.         except (KeyError,AttributeError), err:
  55.             return voidPointer
  56.     def addCallback( self, which, function ):
  57.         """Register a callback for this structure object"""
  58.         callbackType = self.CALLBACK_TYPES.get( which )
  59.         if not callbackType:
  60.             raise ValueError(
  61.                 """Don't have a registered callback type for %r"""%(
  62.                     which,
  63.                 )
  64.             )
  65.         wrapperMethod = self.WRAPPER_METHODS.get( which )
  66.         if wrapperMethod is not None:
  67.             function = getattr(self,wrapperMethod)( function )
  68.         cCallback = callbackType( function )
  69.         # XXX this is ugly, query to ctypes list on how to fix it...
  70.         try:
  71.             self.CALLBACK_FUNCTION_REGISTRARS[which]( self, which, cCallback )
  72.         except ctypes.ArgumentError, err:
  73.             err.args += (which,cCallback)
  74.             raise
  75.         #gluTessCallbackBase( self, which, cCallback)
  76.         # XXX catch errors!
  77.         if getattr( self, 'callbacks', None ) is None:
  78.             self.callbacks = {}
  79.         self.callbacks[ which ] = cCallback
  80.         return cCallback
  81.     def ptrAsArray( self, ptr, length, type ):
  82.         """Copy length values from ptr into new array of given type"""
  83.         result = type.zeros( (length,) )
  84.         for i in range(length):
  85.             result[i] = ptr[i]
  86.         return result
  87.