1"""Late-bound base-class (with acceleration)"""
2from OpenGL import acceleratesupport
3LateBind = Curry = None
4if acceleratesupport.ACCELERATE_AVAILABLE:
5    try:
6        from OpenGL_accelerate.latebind import LateBind, Curry
7    except ImportError as err:
8        pass
9if LateBind is None:
10    class LateBind(object):
11        """Provides a __call__ which dispatches to self._finalCall
12
13        When called without self._finalCall() makes a call to
14        self.finalise() and then calls self._finalCall()
15        """
16        _finalCall = None
17        def setFinalCall( self, finalCall ):
18            """Set our finalCall to the callable object given"""
19            self._finalCall = finalCall
20        def getFinalCall( self ):
21            """Retrieve and/or bind and retrieve final call"""
22            if not self._finalCall:
23                self._finalCall = self.finalise()
24            return self._finalCall
25
26
27        def finalise( self ):
28            """Finalise our target to our final callable object
29
30            return final callable
31            """
32
33        def __call__( self, *args, **named ):
34            """Call self._finalCall, calling finalise() first if not already called
35
36            There's actually *no* reason to unpack and repack the arguments,
37            but unfortunately I don't know of a Cython syntax to specify
38            that.
39            """
40            try:
41                return self._finalCall( *args, **named )
42            except (TypeError,AttributeError) as err:
43                if self._finalCall is None:
44                    self._finalCall = self.finalise()
45                return self._finalCall( *args, **named )
46if Curry is None:
47    class Curry(object):
48        """Provides a simple Curry which can bind (only) the first element
49
50        This is used by lazywrapper, which explains the weird naming
51        of the two attributes...
52        """
53        wrapperFunction = None
54        baseFunction = None
55        def __init__( self, wrapperFunction, baseFunction ):
56            """Stores self.wrapperFunction and self.baseFunction"""
57            self.baseFunction = baseFunction
58            self.wrapperFunction = wrapperFunction
59        def __call__( self, *args, **named ):
60            """returns self.wrapperFunction( self.baseFunction, *args, **named )"""
61            return self.wrapperFunction( self.baseFunction, *args, **named )
62