Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[android] #352 - bring over concepts from runtime styling (value.cpp …
Browse files Browse the repository at this point in the history
…/ java_types.cpp)
  • Loading branch information
tobrun committed Jul 26, 2016
1 parent 73fe2ce commit b51592e
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 0 deletions.
66 changes: 66 additions & 0 deletions platform/android/src/feature/value.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include "value.hpp"

#include "../java_types.hpp"

namespace mbgl {
namespace android {

//Instance

Value::Value(jni::JNIEnv& env, jni::jobject* _value) : jenv(env), value(_value) {}

Value::~Value() {}

bool Value::isNull() const {
return value == nullptr;
}

bool Value::isArray() const {
return jni::IsInstanceOf(jenv, value, *java::ObjectArray::jclass);
}

bool Value::isObject() const {
return jni::IsInstanceOf(jenv, value, *java::Map::jclass);;
}

bool Value::isString() const {
return jni::IsInstanceOf(jenv, value, *java::String::jclass);
}

bool Value::isBool() const {
return jni::IsInstanceOf(jenv, value, *java::Boolean::jclass);
}

bool Value::isNumber() const {
return jni::IsInstanceOf(jenv, value, *java::Number::jclass);
}

std::string Value::toString() const {
jni::jstring* string = reinterpret_cast<jni::jstring*>(value);
return jni::Make<std::string>(jenv, jni::String(string));
}

float Value::toNumber() const {
return jni::CallMethod<jni::jfloat>(jenv, value, *java::Number::floatValueMethodId);
}

bool Value::toBool() const {
return jni::CallMethod<jni::jboolean>(jenv, value, *java::Boolean::booleanValueMethodId);
}

Value Value::get(const char* key) const {
jni::jobject* member = jni::CallMethod<jni::jobject*>(jenv, value, *java::Map::getMethodId, jni::Make<jni::String>(jenv, std::string(key)).Get());
return Value(jenv, member);
}

int Value::getLength() const {
auto array = (jni::jarray<jni::jobject>*) value;
return jni::GetArrayLength(jenv, *array);
}

Value Value::get(const int index ) const {
auto array = (jni::jarray<jni::jobject>*) value;
return Value(jenv, jni::GetObjectArrayElement(jenv, *array, index));
}
}
}
35 changes: 35 additions & 0 deletions platform/android/src/feature/value.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#pragma once

#include <jni/jni.hpp>

#include <string>

namespace mbgl {
namespace android {

class Value {
public:

Value(jni::JNIEnv&, jni::jobject*);
virtual ~Value();

bool isNull() const;
bool isArray() const;
bool isObject() const;
bool isString() const;
bool isBool() const;
bool isNumber() const;

std::string toString() const;
float toNumber() const;
bool toBool() const;
Value get(const char* key) const;
int getLength() const;
Value get(const int index ) const;

jni::JNIEnv& jenv;
jni::jobject* value;
};

}
}
37 changes: 37 additions & 0 deletions platform/android/src/java_types.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include "java_types.hpp"

namespace mbgl {
namespace android {
namespace java {

jni::jclass* ObjectArray::jclass;

jni::jclass* String::jclass;

jni::jclass* Boolean::jclass;
jni::jmethodID* Boolean::booleanValueMethodId;

jni::jclass* Number::jclass;
jni::jmethodID* Number::floatValueMethodId;

jni::jclass* Map::jclass;
jni::jmethodID* Map::getMethodId;

void registerNatives(JNIEnv& env) {
ObjectArray::jclass = jni::NewGlobalRef(env, &jni::FindClass(env, "[Ljava/lang/Object;")).release();

String::jclass = jni::NewGlobalRef(env, &jni::FindClass(env, "java/lang/String")).release();

Boolean::jclass = jni::NewGlobalRef(env, &jni::FindClass(env, "java/lang/Boolean")).release();
Boolean::booleanValueMethodId = &jni::GetMethodID(env, *Boolean::jclass, "booleanValue", "()Z");

Number::jclass = jni::NewGlobalRef(env, &jni::FindClass(env, "java/lang/Number")).release();
Number::floatValueMethodId = &jni::GetMethodID(env, *Number::jclass, "floatValue", "()F");

Map::jclass = jni::NewGlobalRef(env, &jni::FindClass(env, "java/util/Map")).release();
Map::getMethodId = &jni::GetMethodID(env, *Map::jclass, "get", "(Ljava/lang/Object;)Ljava/lang/Object;");
}

}
}
}
35 changes: 35 additions & 0 deletions platform/android/src/java_types.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#pragma once

#include <jni/jni.hpp>

namespace mbgl {
namespace android {
namespace java {

struct ObjectArray {
static jni::jclass* jclass;
};

struct String {
static jni::jclass* jclass;
};

struct Boolean {
static jni::jclass* jclass;
static jni::jmethodID* booleanValueMethodId;
};

struct Number {
static jni::jclass* jclass;
static jni::jmethodID* floatValueMethodId;
};

struct Map {
static jni::jclass* jclass;
static jni::jmethodID* getMethodId;
};

void registerNatives(JNIEnv&);
}
}
}

0 comments on commit b51592e

Please sign in to comment.