a
    i‚j1  ã                   @   s8   d Z ddlZddlZddlmZmZ G dd„ deƒZdS )z.Base class for GLU callback-caching structuresé    N)ÚlongÚinteger_typesc                   @   sL   e Zd ZdZdd„ ZeeƒZdZdZdZ	dd„ Z
dd„ Zd	d
„ Zdd„ ZdS )Ú	GLUStructa.  Mix-in class for GLU Structures that want to retain references to callbacks
    
    Also provides original-object-return for the "datapointer" style paremters
    
    Each sub-class must override:
        CALLBACK_TYPES -- maps a "which" constant to a function type 
        CALLBACK_FUNCTION_REGISTRARS -- maps a "which" constant to the 
            registration function for functions of that type
        WRAPPER_METHODS -- maps a "which" consant to a method of the structure 
            that produces a callable around the function which takes care of 
            input/output arguments, data conversions, error handling and the 
            like.
    Creates a dictionary member dataPointers if original-object-return is used
    Creates a dictionary member callbacks if callback registration is used
    c                 C   s
   t  | ¡S )z4Gets as a ctypes pointer to the underlying structure)ÚctypesZpointer)Úself© r   úR/home/ghrups/robot-bench/.venv/lib/python3.9/site-packages/OpenGL/GLU/glustruct.pyÚ
getAsParam   s    zGLUStruct.getAsParamNc              
   C   sJ   t |ƒ}z|| j|< W n. tyD } z||i| _W Y d}~n
d}~0 0 |S )zþNote object for later retrieval as a Python object pointer
        
        This is the registration point for "original object return", returns 
        a void pointer to the Python object, though this is, effectively, an 
        opaque value.
        N)ÚidÚdataPointersÚAttributeError)r   ÚobjectÚidentityÚerrr   r   r   Ú
noteObject   s     zGLUStruct.noteObjectc              
   C   s’   t |tƒr|}nD|du rdS z
|j}W n, tyR } z|d }W Y d}~n
d}~0 0 z| j| W S  ttfyŒ } z|W  Y d}~S d}~0 0 dS )z<Given a void-pointer, try to find our original Python objectNr   )Ú
isinstancer   Úvaluer   r   ÚKeyError)r   ZvoidPointerr   r   r   r   r   ÚoriginalObject*   s    

zGLUStruct.originalObjectc              
   C   s¾   | j  |¡}|std|f ƒ‚| j |¡}|dur@t| |ƒ|ƒ}||ƒ}z| j| | ||ƒ W n: tjy˜ } z | j||f7  _‚ W Y d}~n
d}~0 0 t| ddƒdu r°i | _	|| j	|< |S )z-Register a callback for this structure objectz,Don't have a registered callback type for %rNÚ	callbacks)
ÚCALLBACK_TYPESÚgetÚ
ValueErrorÚWRAPPER_METHODSÚgetattrÚCALLBACK_FUNCTION_REGISTRARSr   ÚArgumentErrorÚargsr   )r   ÚwhichÚfunctionZcallbackTypeZwrapperMethodZ	cCallbackr   r   r   r   ÚaddCallback9   s(    ÿÿ
zGLUStruct.addCallbackc                 C   s*   |  |f¡}t|ƒD ]}|| ||< q|S )z8Copy length values from ptr into new array of given type)ZzerosÚrange)r   ZptrÚlengthÚtypeÚresultÚir   r   r   Ú
ptrAsArrayR   s    zGLUStruct.ptrAsArray)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r	   ÚpropertyZ_as_parameter_r   r   r   r   r   r    r&   r   r   r   r   r      s   r   )r*   r   ÚweakrefZOpenGL._bytesr   r   r   r   r   r   r   r   Ú<module>   s   