object.pxi 3.46 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

"""Maps object type to its constructor"""
19
cdef list OBJECT_TYPE = []
20 21

def _register_object(int index, object cls):
22
    """register object class"""
23 24 25 26 27
    if issubclass(cls, NDArrayBase):
        _register_ndarray(index, cls)
        return

    global OBJECT_TYPE
28 29 30 31 32 33 34
    while len(OBJECT_TYPE) <= index:
        OBJECT_TYPE.append(None)
    OBJECT_TYPE[index] = cls


cdef inline object make_ret_object(void* chandle):
    global OBJECT_TYPE
35
    global _CLASS_OBJECT
36
    cdef unsigned tindex
37 38 39 40
    cdef object cls
    cdef object handle
    object_type = OBJECT_TYPE
    handle = ctypes_handle(chandle)
41
    CALL(TVMObjectGetTypeIndex(chandle, &tindex))
42 43
    if tindex < len(OBJECT_TYPE):
        cls = OBJECT_TYPE[tindex]
44
        if cls is not None:
45
            obj = cls.__new__(cls)
46
        else:
47
            obj = _CLASS_OBJECT.__new__(_CLASS_OBJECT)
48
    else:
49
        obj = _CLASS_OBJECT.__new__(_CLASS_OBJECT)
50
    (<ObjectBase>obj).chandle = chandle
51 52 53 54
    return obj


cdef class ObjectBase:
55
    cdef void* chandle
56 57

    cdef inline _set_handle(self, handle):
58
        cdef unsigned long long ptr
59 60 61
        if handle is None:
            self.chandle = NULL
        else:
62 63
            ptr = handle.value
            self.chandle = <void*>(ptr)
64 65 66 67 68 69

    property handle:
        def __get__(self):
            if self.chandle == NULL:
                return None
            else:
70 71
                return ctypes_handle(self.chandle)

72 73 74
        def __set__(self, value):
            self._set_handle(value)

75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
    def __dealloc__(self):
        CALL(TVMObjectFree(self.chandle))

    def __init_handle_by_constructor__(self, fconstructor, *args):
        """Initialize the handle by calling constructor function.

        Parameters
        ----------
        fconstructor : Function
            Constructor function.

        args: list of objects
            The arguments to the constructor

        Note
        ----
        We have a special calling convention to call constructor functions.
        So the return handle is directly set into the Node object
        instead of creating a new Node.
        """
        # avoid error raised during construction.
        self.chandle = NULL
        cdef void* chandle
        ConstructorCall(
99
            (<PackedFuncBase>fconstructor).chandle,
100
            kTVMObjectHandle, args, &chandle)
101
        self.chandle = chandle
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118

    def same_as(self, other):
        """Check object identity.

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

        Returns
        -------
        result : bool
             The comparison result.
        """
        if not isinstance(other, ObjectBase):
            return False
        return self.chandle == (<ObjectBase>other).chandle