From f415cf2d78cb214b0a661e0f03905b4279656ea8 Mon Sep 17 00:00:00 2001 From: Joe Onorato Date: Mon, 10 Aug 2009 15:15:52 -0700 Subject: Add two addDefines methods to ScriptC.Builder. They add #defines from the fileds on a class or object that you pass in. --- graphics/java/android/renderscript/ScriptC.java | 40 +++++++++++++++++++++++++ 1 file changed, 40 insertions(+) (limited to 'graphics/java') diff --git a/graphics/java/android/renderscript/ScriptC.java b/graphics/java/android/renderscript/ScriptC.java index 0f231d6..bb99e23 100644 --- a/graphics/java/android/renderscript/ScriptC.java +++ b/graphics/java/android/renderscript/ScriptC.java @@ -24,10 +24,15 @@ import java.io.InputStream; import java.util.Map.Entry; import java.util.HashMap; +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; + /** * @hide **/ public class ScriptC extends Script { + private static final String TAG = "ScriptC"; + ScriptC(int id, RenderScript rs) { super(id, rs); } @@ -113,6 +118,41 @@ public class ScriptC extends Script { mFloatDefines.put(name, value); } + /** + * Takes the all public static final fields for a class, and adds defines + * for them, using the name of the field as the name of the define. + */ + public void addDefines(Class cl) { + addDefines(cl.getFields(), (Modifier.STATIC | Modifier.FINAL | Modifier.PUBLIC), null); + } + + /** + * Takes the all public fields for an object, and adds defines + * for them, using the name of the field as the name of the define. + */ + public void addDefines(Object o) { + addDefines(o.getClass().getFields(), Modifier.PUBLIC, o); + } + + void addDefines(Field[] fields, int mask, Object o) { + for (Field f: fields) { + try { + if ((f.getModifiers() & mask) == mask) { + Class t = f.getType(); + if (t == int.class) { + mIntDefines.put(f.getName(), f.getInt(o)); + } + else if (t == float.class) { + mFloatDefines.put(f.getName(), f.getFloat(o)); + } + } + } catch (IllegalAccessException ex) { + // TODO: Do we want this log? + Log.d(TAG, "addDefines skipping field " + f.getName()); + } + } + } + public ScriptC create() { return internalCreate(this); } -- cgit v1.1