BaseObj.java revision 718cd1f322ee5b62b6a49cb36195bcb18a5ab711
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        rs.validate();
29        mRS = rs;
30        mID = 0;
31        mDestroyed = false;
32    }
33
34    public int getID() {
35        return mID;
36    }
37
38    int mID;
39    boolean mDestroyed;
40    String mName;
41    RenderScript mRS;
42
43    public void setName(String s) throws IllegalStateException, IllegalArgumentException
44    {
45        if(s.length() < 1) {
46            throw new IllegalArgumentException("setName does not accept a zero length string.");
47        }
48        if(mName != null) {
49            throw new IllegalArgumentException("setName object already has a name.");
50        }
51
52        try {
53            byte[] bytes = s.getBytes("UTF-8");
54            mRS.nAssignName(mID, bytes);
55            mName = s;
56        } catch (java.io.UnsupportedEncodingException e) {
57            throw new RuntimeException(e);
58        }
59    }
60
61    protected void finalize() throws Throwable
62    {
63        if (!mDestroyed) {
64            if(mID != 0 && mRS.isAlive()) {
65                mRS.nObjDestroyOOB(mID);
66            }
67            mRS = null;
68            mID = 0;
69            mDestroyed = true;
70            //Log.v(RenderScript.LOG_TAG, getClass() +
71            // " auto finalizing object without having released the RS reference.");
72        }
73        super.finalize();
74    }
75
76    public void destroy() {
77        if(mDestroyed) {
78            throw new IllegalStateException("Object already destroyed.");
79        }
80        mDestroyed = true;
81        mRS.nObjDestroy(mID);
82    }
83
84}
85
86