diff --git a/examples/AndroidMath/AndroidMath.java b/examples/AndroidMath/AndroidMath.java index 9317d0cd8..fef2b0526 100644 --- a/examples/AndroidMath/AndroidMath.java +++ b/examples/AndroidMath/AndroidMath.java @@ -4,12 +4,12 @@ public class AndroidMath { private AndroidMath() {} - // Native stuff - C/C++ - public static native int power(int number, int n); - - static { - // In this place we are loading native libraries. - // Native library always has upp package name! - System.loadLibrary("AndroidMath"); - } + // Native stuff - C/C++ + public static native int power(int number, int n); + + static { + // In this place we are loading native libraries. + // Native library always has upp package name! + System.loadLibrary("AndroidMath"); + } } diff --git a/examples/AndroidMath/AndroidMathActivity.java b/examples/AndroidMath/AndroidMathActivity.java index 9b493aa84..894b07e7f 100644 --- a/examples/AndroidMath/AndroidMathActivity.java +++ b/examples/AndroidMath/AndroidMathActivity.java @@ -7,42 +7,56 @@ import android.os.Bundle; public class AndroidMathActivity extends Activity { - @Override - public void onCreate(Bundle savedInstanceState) - { - super.onCreate(savedInstanceState); + @Override + public void onCreate(Bundle savedInstanceState) + { + super.onCreate(savedInstanceState); ScrollView scroller = new ScrollView(this); TextView tv = new TextView(this); - - calculatePowerOperations(tv, 20); - calculateVectorOperations(tv); - - scroller.addView(tv); - setContentView(scroller); - } + + calculatePowerOperations(tv, 20); + calculateVectorOperations(tv); + + scroller.addView(tv); + setContentView(scroller); + } - private void calculatePowerOperations(TextView tv, int maxPower) - { - String text = "Power:\n"; - int maxNumber = maxPower; - for(int i = 1; i <= maxNumber; i++) { - int pow = AndroidMath.power(i, 2); - text += Integer.toString(i) + "^2 = " + Integer.toString(pow) + "\n"; - } - - tv.append(text); - } - - private void calculateVectorOperations(TextView tv) - { - String text = "Vector operations:\n"; - - Vector vec1 = new Vector(3); - - text += "Vec1 Size: " + Integer.toString(vec1.getSize()) + "\n"; - text += "Vec1: " + vec1.toString() + "\n"; - - tv.append(text); - } + private void calculatePowerOperations(TextView tv, int maxPower) + { + String text = "Power:\n"; + int maxNumber = maxPower; + for(int i = 1; i <= maxNumber; i++) { + int pow = AndroidMath.power(i, 2); + text += Integer.toString(i) + "^2 = " + Integer.toString(pow) + "\n"; + } + + tv.append(text); + } + + private void calculateVectorOperations(TextView tv) + { + String text = "Vector operations:\n"; + + Vector vec1 = new Vector(3); + vec1.set(0, 3.0f); + vec1.set(1, -1.0f); + vec1.set(2, 5.0f); + + text += "Vec1: " + vec1.toString() + "\n"; + + Vector vec1Copy = new Vector(vec1); + vec1Copy.multipleByScalar(5.0f); + text += "3 * Vec1: " + vec1Copy.toString() + "\n"; + + Vector vec2 = new Vector(4); + vec2.set(0, -2.0f); + vec2.set(1, 4.0f); + vec2.set(2, 7.0f); + vec2.set(3, -9.0f); + + text += "Vec2: " + vec2.toString() + "\n"; + + tv.append(text); + } } diff --git a/examples/AndroidMath/MemoryManager.h b/examples/AndroidMath/MemoryManager.h index 665022119..7040bcabe 100644 --- a/examples/AndroidMath/MemoryManager.h +++ b/examples/AndroidMath/MemoryManager.h @@ -2,30 +2,36 @@ #define _JniMath_MemoryManager_h_ #include -#include -#include #include /** - * Java "HashCode()" object memory manager. + * Java simply memory manager. */ template class MemoryManager { public: void Insert(JNIEnv *env, jobject jobj, const T& obj) { - std::string hashCode = GetJobjectHasCode(env, jobj); - if(!hashCode.empty()) { - hashCodes.push_back(hashCode); + jobject weakGlobalRef = env->NewWeakGlobalRef(jobj); + if(!env->IsSameObject(weakGlobalRef, NULL)) { + objs.push_back(env->NewWeakGlobalRef(weakGlobalRef)); values.push_back(obj); } } + void MakeCopy(JNIEnv *env, jobject jobjSrc, jobject jobjDst) + { + if(!env->IsSameObject(jobjSrc, jobjDst)) { + T t(*Get(env, jobjSrc)); + Insert(env, jobjSrc, t); + } + } + void Erase(JNIEnv *env, jobject jobj) { int idx = FindIdx(env, jobj); if(idx >= 0) { - hashCodes.erase(hashCodes.begin() + idx); + objs.erase(objs.begin() + idx); values.erase(values.begin() + idx); } } @@ -33,38 +39,27 @@ public: T* Get(JNIEnv *env, jobject jobj) { int idx = FindIdx(env, jobj); - return idx != -1 ? &values[idx] : NULL; + return idx >= 0 ? &values[idx] : NULL; + } + + int GetCount() + { + return static_cast(values.size()); } private: int FindIdx(JNIEnv *env, jobject jobj) { - std::string hashCode = GetJobjectHasCode(env, jobj); - for(int i = 0; i < hashCodes.size(); i++) { - if(hashCode == hashCodes[i]) { + for(int i = 0; i < objs.size(); i++) { + if(env->IsSameObject(objs[i], jobj)) { return i; } } - return -1; - } - - std::string GetJobjectHasCode(JNIEnv *env, jobject jobj) - { - jclass cls = env->GetObjectClass(jobj); - jmethodID mid = env->GetMethodID(cls, "hashCode", "()I"); - if(mid == 0) { - return ""; - } - int ret = env->CallIntMethod(jobj, mid); - - std::stringstream ss; - ss << ret; - - return ss.str(); + return 0; } private: - std::vector hashCodes; + std::vector objs; std::vector values; }; diff --git a/examples/AndroidMath/Vector.cpp b/examples/AndroidMath/Vector.cpp index bb197b8e5..e08887d54 100644 --- a/examples/AndroidMath/Vector.cpp +++ b/examples/AndroidMath/Vector.cpp @@ -30,6 +30,17 @@ JNIEXPORT void JNICALL Java_org_upp_AndroidMath_Vector_construct mm.Insert(env, obj, Vector(size)); } +/* + * Class: org_upp_AndroidMath_Vector + * Method: copyConstruct + * Signature: (Lorg/upp/AndroidMath/Vector;)V + */ +JNIEXPORT void JNICALL Java_org_upp_AndroidMath_Vector_copyConstruct + (JNIEnv *env, jobject objSrc, jobject objDst) +{ + mm.MakeCopy(env, objSrc, objDst); +} + /* * Class: org_upp_AndroidMath_Vector * Method: destruct @@ -60,9 +71,11 @@ JNIEXPORT jint JNICALL Java_org_upp_AndroidMath_Vector_getSize * Signature: (I)F */ JNIEXPORT jfloat JNICALL Java_org_upp_AndroidMath_Vector_get - (JNIEnv *, jobject, jint) + (JNIEnv *env, jobject obj, jint id) { - return 0.0f; + Vector* vec = mm.Get(env, obj); + + return vec->Get(id); } /* @@ -71,9 +84,22 @@ JNIEXPORT jfloat JNICALL Java_org_upp_AndroidMath_Vector_get * Signature: (IF)V */ JNIEXPORT void JNICALL Java_org_upp_AndroidMath_Vector_set - (JNIEnv *, jobject, jint, jfloat) + (JNIEnv *env, jobject obj, jint id, jfloat value) { - + Vector* vec = mm.Get(env, obj); + vec->Set(id, value); +} + +/* + * Class: org_upp_AndroidMath_Vector + * Method: multipleByScalar + * Signature: (F)V + */ +JNIEXPORT void JNICALL Java_org_upp_AndroidMath_Vector_multipleByScalar + (JNIEnv *env, jobject obj, jfloat scalar) +{ + Vector* vec = mm.Get(env, obj); + vec->MultipleByScalar(scalar); } /* @@ -86,7 +112,7 @@ JNIEXPORT jstring JNICALL Java_org_upp_AndroidMath_Vector_toString { Vector* vec = mm.Get(env, obj); - return env->NewStringUTF(vec->ToString().c_str()); + return env->NewStringUTF((vec->ToString()).c_str()); } #ifdef __cplusplus diff --git a/examples/AndroidMath/Vector.java b/examples/AndroidMath/Vector.java index 0047cbbf6..bf97dbbcd 100644 --- a/examples/AndroidMath/Vector.java +++ b/examples/AndroidMath/Vector.java @@ -1,7 +1,7 @@ package org.upp.AndroidMath; /** - * Class witch whole implementaiton is native. + * Class which whole implementaiton is native. */ public class Vector { @@ -10,21 +10,30 @@ public class Vector construct(size); } + public Vector(Vector vec) + { + copyConstruct(vec); + } + @Override - public void finalize() + protected void finalize() throws Throwable { destroy(); + super.finalize(); } // Native stuff - C/C++ public native int getSize(); public native float get(int id); - public native void set(int id, float data); + public native void set(int id, float value); + + public native void multipleByScalar(float scalar); public native String toString(); private native void construct(int size); + private native void copyConstruct(Vector vec); private native void destroy(); static { diff --git a/examples/AndroidMathUtility/Vector.cpp b/examples/AndroidMathUtility/Vector.cpp index c56a8c756..2fa9e3a73 100644 --- a/examples/AndroidMathUtility/Vector.cpp +++ b/examples/AndroidMathUtility/Vector.cpp @@ -26,7 +26,6 @@ Vector::Vector(const Vector& vec) for(int i = 0; i < size; i++) { data[i] = vec.data[i]; } - } } @@ -35,6 +34,28 @@ Vector::~Vector() delete[] data; } +float Vector::Get(int id) const +{ + return this->data[id]; +} + +int Vector::GetSize() const +{ + return this->size; +} + +void Vector::Set(int id, float value) +{ + this->data[id] = value; +} + +void Vector::MultipleByScalar(float scalar) +{ + for(int i = 0; i < size; i++) { + this->data[i] *= scalar; + } +} + std::string Vector::ToString() const { std::stringstream ss; diff --git a/examples/AndroidMathUtility/Vector.h b/examples/AndroidMathUtility/Vector.h index 62fcf449d..51da3fef5 100644 --- a/examples/AndroidMathUtility/Vector.h +++ b/examples/AndroidMathUtility/Vector.h @@ -12,7 +12,12 @@ public: Vector(const Vector& vec); virtual ~Vector(); - int GetSize() const { return this->size; } + float Get(int id) const; + int GetSize() const; + + void Set(int id, float value); + + void MultipleByScalar(float scalar); std::string ToString() const;