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

Commit

Permalink
[android] #352 - attempt to create a visitor pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
tobrun committed Jul 27, 2016
1 parent da58545 commit 4ff2ee7
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 63 deletions.
117 changes: 54 additions & 63 deletions platform/android/src/feature/android_feature.hpp
Original file line number Diff line number Diff line change
@@ -1,95 +1,86 @@
#pragma once

#include "value.hpp"
#include "../java_types.hpp"

#include <mbgl/platform/log.hpp>
#include <mbgl/geometry/feature.hpp>
#include <mbgl/util/feature.hpp>
//#include <mbgl/util/feature.hpp>
#include <mbgl/util/optional.hpp>

//#include <mapbox/geometry/feature.hpp>

#include <jni/jni.hpp>

//XXX
#pragma GCC diagnostic ignored "-Wunused-parameter"

namespace mbgl {
namespace geometry {
namespace feature {

class PropertyValueEvaluator {

//XXX
#pragma GCC diagnostic ignored "-Wunused-parameter"

inline bool isUndefined(const mbgl::android::Value& value) {
return value.isNull();
}

inline bool isArray(const mbgl::android::Value& value) {
return value.isArray();
}

inline bool isObject(const mbgl::android::Value& value) {
return value.isObject();
}
public:

inline std::size_t arrayLength(const mbgl::android::Value& value) {
return value.getLength();;
}
PropertyValueEvaluator(jni::JNIEnv&);
virtual ~PropertyValueEvaluator();

inline mbgl::android::Value arrayMember(const mbgl::android::Value& value, std::size_t i) {
return value.get(i);
}
std::nullptr_t operator()(const std::nullptr_t &property) const {
mbgl::Log::Warning(mbgl::Event::Android, "PropertyEvaluator: returning null");
return nullptr;
}

inline optional<mbgl::android::Value> objectMember(const mbgl::android::Value& value, const char* key) {
mbgl::android::Value member = value.get(key);
jboolean operator()(const bool &property) const {
mbgl::Log::Warning(mbgl::Event::Android, "PropertyEvaluator: converting bool to jboolean");
return value ? JNI_TRUE : JNI_FALSE;
}

if (!member.isNull()) {
return member;
} else {
return {};
jlong operator()(const uint64_t &property) const {
mbgl::Log::Warning(mbgl::Event::Android, "PropertyEvaluator: converting uint64_t to jlong");
return static_cast<jlong>(property);
}
}

template <class Fn>
optional<Error> eachMember(const mbgl::android::Value& value, Fn&& fn) {
//TODO
mbgl::Log::Warning(mbgl::Event::Android, "eachMember not implemented");
return {};
}
jint operator()(const int64_t &property) const {
mbgl::Log::Warning(mbgl::Event::Android, "PropertyEvaluator: converting int64_t to jint");
return property;
}

inline optional<bool> toBool(const mbgl::android::Value& value) {
if (value.isBool()) {
return value.toBool();
} else {
return {};
jdouble operator()(const double &property) const {
mbgl::Log::Warning(mbgl::Event::Android, "PropertyEvaluator: converting double to jdouble");
return property;
}
}

inline optional<float> toNumber(const mbgl::android::Value& value) {
if (value.isNumber()) {
return value.toNumber();
} else {
return {};
jni::jstring* operator()(const std::string &property) const {
mbgl::Log::Warning(mbgl::Event::Android, "PropertyEvaluator: converting std::string to jstring");
return jni::Make<jni::String>(jenv, property).Get();
}
}

inline optional<std::string> toString(const mbgl::android::Value& value) {
if (value.isString()) {
return value.toString();
} else {
return {};
jobject operator()(const std::vector<mbgl::Value> &values) const {
mbgl::Log::Warning(mbgl::Event::Android, "PropertyEvaluator not implemented for list");
// create java list
//for (const auto &v : values) {
// add item to list while visiting this to evaluate its properties
//[objects addObject:mbgl::Value::visit(v, *this)];
//}
// change return with list
return nullptr;
}
}

inline optional<Value> toValue(const mbgl::android::Value& value) {
if (value.isBool()) {
return { value.toBool() };
} else if (value.isString()) {
return { value.toString() };
} else if (value.isNumber()) {
return { value.toNumber() };
} else {
return {};
jobject operator()(const std::unordered_map<std::string, mbgl::Value> &items) const {
mbgl::Log::Warning(mbgl::Event::Android, "PropertyEvaluator not implemented for map");
// create java map
//for (auto &item : items) {
// add item to map while recursively visiting its properties
//attributes[@(item.first.c_str())] = mbgl::Value::visit(item.second, *this);
//}
return nullptr;
}
}

} // namespace feature
jni::JNIEnv& jenv;
jni::jobject* value;
};

}
} // namespace geometry
} // namespace mbgl
8 changes: 8 additions & 0 deletions platform/android/src/jni.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include <mbgl/util/string.hpp>
#include <mbgl/util/run_loop.hpp>

#include "feature/android_feature.hpp"

#include <jni/jni.hpp>

#pragma clang diagnostic ignored "-Wunused-parameter"
Expand Down Expand Up @@ -1160,6 +1162,12 @@ jni::jobject* nativeGetVisibleFeatures(JNIEnv *env, jni::jobject* obj, jlong na
}else{
mbgl::Log::Debug(mbgl::Event::JNI, "Unsupported feature type found!");
}

for (auto &pair : feature.properties) {
auto &value = pair.second;
mbgl::geometry::feature::PropertyValueEvaluator evaluator(*env);
mbgl::Value::visit(value, evaluator);
}
}
return joutput;
}
Expand Down

0 comments on commit 4ff2ee7

Please sign in to comment.