#
#   PyGUI - OpenGL Base - Generic
#

def context_id_generator():
    id = 1
    while 1:
        yield id
        id += 1

context_ids = context_id_generator()

current_context_id = None


class GLBase(object):
    #  Base class for objects with an OpenGL context.
    #
    #  _context_id   int   OpenGL context identifier

    def __init__(self, share):
        if share:
            self._context_id = share._context_id
        else:
            self._context_id = context_ids.next()
    
    def _with_context(self, proc):
        """The proc should be a callable object of no arguments. Calls
        the proc with the view's OpenGL context as the current context."""
        global current_context_id
        old_context_id = current_context_id
        current_context_id = self._context_id
        self._with_platform_context(proc)
        current_context_id = old_context_id
    
    def _with_platform_context(self, proc):
        raise NotImplementedError('GLBase._with_platform_context')


def current_context():
    return current_context_id
