This repository has been archived by the owner on Aug 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[android] #352 - bring over concepts from runtime styling (value.cpp …
…/ java_types.cpp)
- Loading branch information
Showing
4 changed files
with
173 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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;"); | ||
} | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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&); | ||
} | ||
} | ||
} |