BaseObj.java revision a9e7a05b84470257637c97d65f6562aa832c66ef
1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.renderscript;
18
19import android.util.Log;
20
21/**
22 * @hide
23 *
24 **/
25class BaseObj {
26
27    BaseObj(RenderScript rs) {
28        mRS = rs;
29        mID = 0;
30        mDestroyed = false;
31    }
32
33    public int getID() {
34        return mID;
35    }
36
37    int mID;
38    boolean mDestroyed;
39    String mName;
40    RenderScript mRS;
41
42    public void setName(String s) throws IllegalStateException, IllegalArgumentException
43    {
44        if(s.length() < 1) {
45            throw new IllegalArgumentException("setName does not accept a zero length string.");
46        }
47        if(mName != null) {
48            throw new IllegalArgumentException("setName object already has a name.");
49        }
50
51        try {
52            byte[] bytes = s.getBytes("UTF-8");
53            mRS.nAssignName(mID, bytes);
54            mName = s;
55        } catch (java.io.UnsupportedEncodingException e) {
56            throw new RuntimeException(e);
57        }
58    }
59
60    protected void finalize() throws Throwable
61    {
62        if (!mDestroyed) {
63            if(mID != 0 && mRS.isAlive()) {
64                mRS.nObjDestroyOOB(mID);
65            }
66            mRS = null;
67            mID = 0;
68            mDestroyed = true;
69            Log.v(RenderScript.LOG_TAG,
70                  getClass() + " auto finalizing object without having released the RS reference.");
71        }
72        super.finalize();
73    }
74
75    public void destroy() {
76        if(mDestroyed) {
77            throw new IllegalStateException("Object already destroyed.");
78        }
79        mDestroyed = true;
80        mRS.nObjDestroy(mID);
81    }
82
83}
84
85