diff --git a/examples/AndroidMath/AndroidManifest.xml b/examples/AndroidMath/AndroidManifest.xml new file mode 100644 index 000000000..9f9f5d098 --- /dev/null +++ b/examples/AndroidMath/AndroidManifest.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + diff --git a/examples/AndroidMath/AndroidMath.cpp b/examples/AndroidMath/AndroidMath.cpp new file mode 100644 index 000000000..4a4a1d88c --- /dev/null +++ b/examples/AndroidMath/AndroidMath.cpp @@ -0,0 +1,23 @@ +#include +#include + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/* + * Class: org_upp_AndroidMath_AndroidMath + * Method: power + * Signature: (II)I + */ +JNIEXPORT jint JNICALL Java_org_upp_AndroidMath_AndroidMath_power + (JNIEnv *, jclass, jint number, jint n) +{ + return AndroidMathUtility::Power(number, n); +} + +#ifdef __cplusplus +} +#endif diff --git a/examples/AndroidMath/AndroidMath.java b/examples/AndroidMath/AndroidMath.java new file mode 100644 index 000000000..9317d0cd8 --- /dev/null +++ b/examples/AndroidMath/AndroidMath.java @@ -0,0 +1,15 @@ +package org.upp.AndroidMath; + +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"); + } +} diff --git a/examples/AndroidMath/AndroidMath.upp b/examples/AndroidMath/AndroidMath.upp new file mode 100644 index 000000000..7aebdfea6 --- /dev/null +++ b/examples/AndroidMath/AndroidMath.upp @@ -0,0 +1,21 @@ +description "Android builder juicy example\377"; + +uses + AndroidMathUtility; + +file + MemoryManager readonly separator, + MemoryManager.h, + Native readonly separator, + AndroidMath.cpp, + Vector.cpp, + Math readonly separator, + Vector.java, + Runtime readonly separator, + AndroidManifest.xml, + AndroidMath.java, + AndroidMathActivity.java; + +mainconfig + "" = ""; + diff --git a/examples/AndroidMath/AndroidMathActivity.java b/examples/AndroidMath/AndroidMathActivity.java new file mode 100644 index 000000000..9b493aa84 --- /dev/null +++ b/examples/AndroidMath/AndroidMathActivity.java @@ -0,0 +1,48 @@ +package org.upp.AndroidMath; + +import android.app.Activity; +import android.widget.TextView; +import android.widget.ScrollView; +import android.os.Bundle; + +public class AndroidMathActivity extends Activity +{ + @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); + } + + 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); + } +} diff --git a/examples/AndroidMath/MemoryManager.h b/examples/AndroidMath/MemoryManager.h new file mode 100644 index 000000000..665022119 --- /dev/null +++ b/examples/AndroidMath/MemoryManager.h @@ -0,0 +1,71 @@ +#ifndef _JniMath_MemoryManager_h_ +#define _JniMath_MemoryManager_h_ + +#include +#include +#include +#include + +/** + * Java "HashCode()" object 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); + values.push_back(obj); + } + } + + void Erase(JNIEnv *env, jobject jobj) + { + int idx = FindIdx(env, jobj); + if(idx >= 0) { + hashCodes.erase(hashCodes.begin() + idx); + values.erase(values.begin() + idx); + } + } + + T* Get(JNIEnv *env, jobject jobj) + { + int idx = FindIdx(env, jobj); + return idx != -1 ? &values[idx] : NULL; + } + +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]) { + 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(); + } + +private: + std::vector hashCodes; + std::vector values; +}; + +#endif diff --git a/examples/AndroidMath/Vector.cpp b/examples/AndroidMath/Vector.cpp new file mode 100644 index 000000000..bb197b8e5 --- /dev/null +++ b/examples/AndroidMath/Vector.cpp @@ -0,0 +1,96 @@ +#ifndef _Included_org_upp_AndroidMath_Vector +#define _Included_org_upp_AndroidMath_Vector + +#include +#include +#include +#include +#include + +#include + +#include "MemoryManager.h" + +#ifdef __cplusplus +extern "C" { +#endif + +using AndroidMathUtility::Vector; + +MemoryManager mm; + +/* + * Class: org_upp_AndroidMath_Vector + * Method: construct + * Signature: (I)V + */ +JNIEXPORT void JNICALL Java_org_upp_AndroidMath_Vector_construct + (JNIEnv *env, jobject obj, jint size) +{ + mm.Insert(env, obj, Vector(size)); +} + +/* + * Class: org_upp_AndroidMath_Vector + * Method: destruct + * Signature: ()V + */ +JNIEXPORT void JNICALL Java_org_upp_AndroidMath_Vector_destroy + (JNIEnv *env, jobject obj) +{ + mm.Erase(env, obj); +} + +/* + * Class: org_upp_AndroidMath_Vector + * Method: getSize + * Signature: ()I + */ +JNIEXPORT jint JNICALL Java_org_upp_AndroidMath_Vector_getSize + (JNIEnv *env, jobject obj) +{ + Vector* vec = mm.Get(env, obj); + + return vec->GetSize(); +} + +/* + * Class: org_upp_AndroidMath_Vector + * Method: get + * Signature: (I)F + */ +JNIEXPORT jfloat JNICALL Java_org_upp_AndroidMath_Vector_get + (JNIEnv *, jobject, jint) +{ + return 0.0f; +} + +/* + * Class: org_upp_AndroidMath_Vector + * Method: set + * Signature: (IF)V + */ +JNIEXPORT void JNICALL Java_org_upp_AndroidMath_Vector_set + (JNIEnv *, jobject, jint, jfloat) +{ + +} + +/* + * Class: org_upp_AndroidMath_Vector + * Method: toStirng + * Signature: ()Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL Java_org_upp_AndroidMath_Vector_toString + (JNIEnv *env, jobject obj) +{ + Vector* vec = mm.Get(env, obj); + + return env->NewStringUTF(vec->ToString().c_str()); +} + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/examples/AndroidMath/Vector.java b/examples/AndroidMath/Vector.java new file mode 100644 index 000000000..0047cbbf6 --- /dev/null +++ b/examples/AndroidMath/Vector.java @@ -0,0 +1,33 @@ +package org.upp.AndroidMath; + +/** + * Class witch whole implementaiton is native. + */ +public class Vector +{ + public Vector(int size) + { + construct(size); + } + + @Override + public void finalize() + { + destroy(); + } + + // Native stuff - C/C++ + public native int getSize(); + public native float get(int id); + + public native void set(int id, float data); + + public native String toString(); + + private native void construct(int size); + private native void destroy(); + + static { + System.loadLibrary("AndroidMath"); + } +} diff --git a/examples/AndroidMath/init b/examples/AndroidMath/init new file mode 100644 index 000000000..7ad08a6c5 --- /dev/null +++ b/examples/AndroidMath/init @@ -0,0 +1,4 @@ +#ifndef _AndroidMath_icpp_init_stub +#define _AndroidMath_icpp_init_stub +#include "AndroidMathUtility/init" +#endif diff --git a/examples/AndroidMathUtility/AndroidMathUtility.cpp b/examples/AndroidMathUtility/AndroidMathUtility.cpp new file mode 100644 index 000000000..c4bc1fc06 --- /dev/null +++ b/examples/AndroidMathUtility/AndroidMathUtility.cpp @@ -0,0 +1,13 @@ +#include "AndroidMathUtility.h" + +namespace AndroidMathUtility { + +int Power(int number, int n) { + int result = number; + for(int i = 0; i < n - 1; i++) { + result *= number; + } + return result; +} + +} diff --git a/examples/AndroidMathUtility/AndroidMathUtility.h b/examples/AndroidMathUtility/AndroidMathUtility.h new file mode 100644 index 000000000..c4ac82fbc --- /dev/null +++ b/examples/AndroidMathUtility/AndroidMathUtility.h @@ -0,0 +1,13 @@ +#ifndef _AndroidMathUtility_AndroidMathUtiliy_h +#define _AndroidMathUtility_AndroidMathUtiliy_h + +#include "Vector.h" + +namespace AndroidMathUtility { + +int Power(int number, int n); + +} + + +#endif diff --git a/examples/AndroidMathUtility/AndroidMathUtility.upp b/examples/AndroidMathUtility/AndroidMathUtility.upp new file mode 100644 index 000000000..de2e28482 --- /dev/null +++ b/examples/AndroidMathUtility/AndroidMathUtility.upp @@ -0,0 +1,8 @@ +description "Supportive package for AndroidMath\377"; + +file + AndroidMathUtility.h, + AndroidMathUtility.cpp, + Vector.h, + Vector.cpp; + diff --git a/examples/AndroidMathUtility/Vector.cpp b/examples/AndroidMathUtility/Vector.cpp new file mode 100644 index 000000000..c56a8c756 --- /dev/null +++ b/examples/AndroidMathUtility/Vector.cpp @@ -0,0 +1,51 @@ +#include + +#include "AndroidMathUtility.h" + +namespace AndroidMathUtility { + +Vector::Vector() +{ + this->size = 0; +} + +Vector::Vector(int size) +{ + data = new float[size]; + for(int i = 0; i < size; i++) { + data[i] = 0.0f; + } + this->size = size; +} + +Vector::Vector(const Vector& vec) +{ + if(vec.GetSize() > 0) { + size = vec.GetSize(); + data = new float[size]; + for(int i = 0; i < size; i++) { + data[i] = vec.data[i]; + } + + } +} + +Vector::~Vector() +{ + delete[] data; +} + +std::string Vector::ToString() const +{ + std::stringstream ss; + ss << "["; + for(int i = 0; i < size; i++) { + ss << data[i]; + if(i + 1 < size) + ss << ", "; + } + ss << "]"; + return ss.str(); +} + +} diff --git a/examples/AndroidMathUtility/Vector.h b/examples/AndroidMathUtility/Vector.h new file mode 100644 index 000000000..62fcf449d --- /dev/null +++ b/examples/AndroidMathUtility/Vector.h @@ -0,0 +1,26 @@ +#ifndef _AndroidMathUtility_Vector_h_ +#define _AndroidMathUtility_Vector_h_ + +#include + +namespace AndroidMathUtility { + +class Vector { +public: + Vector(); + Vector(int size); + Vector(const Vector& vec); + virtual ~Vector(); + + int GetSize() const { return this->size; } + + std::string ToString() const; + +private: + float* data; + int size; +}; + +} + +#endif diff --git a/examples/AndroidMathUtility/init b/examples/AndroidMathUtility/init new file mode 100644 index 000000000..281701ed8 --- /dev/null +++ b/examples/AndroidMathUtility/init @@ -0,0 +1,3 @@ +#ifndef _AndroidMathUtility_icpp_init_stub +#define _AndroidMathUtility_icpp_init_stub +#endif