o
    Û¾iT	  ã                   @   sV   U d Z ddlZddlmZ ej d¡dkrddlmZ ndZee	d	< G d
d„ dƒZ
dS )zAThis module provides classes to handle C++ objects from nanobind.é    N)ÚAnyÚXGRAMMAR_BUILD_DOCSÚ1é   )Úxgrammar_bindingszdummy namespaceÚ_corec                   @   sD   e Zd ZdZeddd„ƒZdd„ Zedd„ ƒZd	e	de
fd
d„ZdS )Ú	XGRObjectaŽ  The base class for all objects in XGrammar. This class provides methods to handle the
    C++ handle from nanobind.

    In subclasses, the handle should be initialized via the the _create_from_handle, or via
    the _init_handle method called within the __init__ method, and should not be modified
    afterwards. Subclasses should use the _handle property to access the handle. When comparing
    two objects, the equality is checked by comparing the C++ handles.

    For performance considerations, objects in XGrammar should be lightweight and only maintain
    a handle to the C++ objects. Heavy operations should be performed on the C++ side.
    Úreturnc                 C   s   |   | ¡}||_|S )a  Construct an object of the class from a C++ handle.

        Parameters
        ----------
        cls
            The class of the object.

        handle
            The C++ handle.

        Returns
        -------
        obj : XGRObject
            An object of type cls.
        )Ú__new__Ú_XGRObject__handle)ÚclsÚhandleÚobj© r   úA/home/ubuntu/.local/lib/python3.10/site-packages/xgrammar/base.pyÚ_create_from_handle   s   
zXGRObject._create_from_handlec                 C   s
   || _ dS )z÷Initialize an object with a handle. This method should be called in the __init__
        method of the subclasses of XGRObject to initialize the C++ handle.

        Parameters
        ----------
        handle
            The C++ handle.
        N©r   )Úselfr   r   r   r   Ú_init_handle.   s   
	zXGRObject._init_handlec                 C   s   | j S )zvGet the C++ handle of the object.

        Returns
        -------
        handle
            The C++ handle.
        r   )r   r   r   r   Ú_handle9   s   	zXGRObject._handleÚotherc                 C   s   t |tƒstS | j|jkS )a%  Compare two XGrammar objects by comparing their C++ handles.

        Parameters
        ----------
        other : object
            The other object to compare with.

        Returns
        -------
        equal : bool
            Whether the two objects have the same C++ handle.
        )Ú
isinstancer   ÚNotImplementedr   )r   r   r   r   r   Ú__eq__D   s   
zXGRObject.__eq__N)r	   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úclassmethodr   r   Úpropertyr   ÚobjectÚboolr   r   r   r   r   r      s    

r   )r   ÚosÚtypingr   ÚenvironÚgetÚ r   r   Ú__annotations__r   r   r   r   r   Ú<module>   s    