summaryrefslogtreecommitdiff
path: root/platform/android/src/geojson/util.hpp
blob: e889931c56cbcf933a217e4b336e659db0cea784 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#pragma once

#include "point.hpp"

#include <type_traits>

namespace mbgl {
namespace android {
namespace geojson {

// Clang 3.8 fails to implicitly convert matching types, so we'll have to do it explicitly.
template <typename To, typename From>
To convertExplicit(From&& src) {
    static_assert(std::is_same<typename std::decay_t<From>::container_type,
                               typename To::container_type>::value,
                  "container types do not match");
    static_assert(std::is_rvalue_reference<From&&>::value,
                  "argument must be rvalue reference");
    return *reinterpret_cast<std::add_pointer_t<To>>(&src);
}

/**
 *  Geometry -> List<Point>
 */
template <class T>
static jni::Object<java::util::List> asPointsList(jni::JNIEnv& env, const T& pointsList) {
    auto jarray = jni::SeizeLocal(env, jni::Array<jni::Object<Point>>::New(env, pointsList.size()));

    for (jni::jsize i = 0; i < pointsList.size(); i++) {
        jarray->Set(env, i, *jni::SeizeLocal(env, Point::New(env, pointsList.at(i))));
    }

    return java::util::Arrays::asList(env, *jarray);
}

/**
 *  Geometry -> List<List<Point>>
 */
template <class SHAPE>
static jni::Object<java::util::List> asPointsListsList(JNIEnv& env, SHAPE value) {
    auto jarray = jni::SeizeLocal(env, jni::Array<jni::Object<java::util::List>>::New(env, value.size()));

    for (size_t i = 0; i < value.size(); i++) {
        jarray->Set(env, i, *jni::SeizeLocal(env, asPointsList(env, value[i])));
    }

    return java::util::Arrays::asList(env, *jarray);
}

} // namespace geojson
} // namespace android
} // namespace mbgl