|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | +# pylint: disable=invalid-name |
| 18 | +"""Runtime Object api""" |
| 19 | +from __future__ import absolute_import |
| 20 | + |
| 21 | +import ctypes |
| 22 | +from ..base import _LIB, check_call |
| 23 | +from .types import TypeCode, RETURN_SWITCH, C_TO_PY_ARG_SWITCH, _wrap_arg_func |
| 24 | + |
| 25 | + |
| 26 | +ObjectHandle = ctypes.c_void_p |
| 27 | +__init_by_constructor__ = None |
| 28 | + |
| 29 | +"""Maps object type to its constructor""" |
| 30 | +OBJECT_TYPE = {} |
| 31 | + |
| 32 | +def _register_object(index, cls): |
| 33 | + """register object class""" |
| 34 | + OBJECT_TYPE[index] = cls |
| 35 | + |
| 36 | + |
| 37 | +def _return_object(x): |
| 38 | + handle = x.v_handle |
| 39 | + if not isinstance(handle, ObjectHandle): |
| 40 | + handle = ObjectHandle(handle) |
| 41 | + tindex = ctypes.c_uint() |
| 42 | + check_call(_LIB.TVMObjectGetTypeIndex(handle, ctypes.byref(tindex))) |
| 43 | + cls = OBJECT_TYPE.get(tindex.value, ObjectBase) |
| 44 | + # Avoid calling __init__ of cls, instead directly call __new__ |
| 45 | + # This allows child class to implement their own __init__ |
| 46 | + obj = cls.__new__(cls) |
| 47 | + obj.handle = handle |
| 48 | + return obj |
| 49 | + |
| 50 | +RETURN_SWITCH[TypeCode.OBJECT_HANDLE] = _return_object |
| 51 | +C_TO_PY_ARG_SWITCH[TypeCode.OBJECT_HANDLE] = _wrap_arg_func( |
| 52 | + _return_object, TypeCode.OBJECT_HANDLE) |
| 53 | + |
| 54 | + |
| 55 | +class ObjectBase(object): |
| 56 | + """Base object for all object types""" |
| 57 | + __slots__ = ["handle"] |
| 58 | + |
| 59 | + def __del__(self): |
| 60 | + if _LIB is not None: |
| 61 | + check_call(_LIB.TVMObjectFree(self.handle)) |
| 62 | + |
| 63 | + def __init_handle_by_constructor__(self, fconstructor, *args): |
| 64 | + """Initialize the handle by calling constructor function. |
| 65 | +
|
| 66 | + Parameters |
| 67 | + ---------- |
| 68 | + fconstructor : Function |
| 69 | + Constructor function. |
| 70 | +
|
| 71 | + args: list of objects |
| 72 | + The arguments to the constructor |
| 73 | +
|
| 74 | + Note |
| 75 | + ---- |
| 76 | + We have a special calling convention to call constructor functions. |
| 77 | + So the return handle is directly set into the Node object |
| 78 | + instead of creating a new Node. |
| 79 | + """ |
| 80 | + # assign handle first to avoid error raising |
| 81 | + self.handle = None |
| 82 | + handle = __init_by_constructor__(fconstructor, args) |
| 83 | + if not isinstance(handle, ObjectHandle): |
| 84 | + handle = ObjectHandle(handle) |
| 85 | + self.handle = handle |
0 commit comments