summaryrefslogtreecommitdiff
path: root/platform/android/src/conversion/constant.hpp
blob: eefd197bc8cea04caa07382e9b897b4c96ca156e (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#pragma once

#include "conversion.hpp"

#include "../java/lang.hpp"

#include <mbgl/util/optional.hpp>

#include <string>
#include <array>
#include <vector>
#include <sstream>

namespace mbgl {
namespace android {
namespace conversion {

template <>
struct Converter<jni::jobject*, bool> {
    Result<jni::jobject*> operator()(jni::JNIEnv& env, const bool& value) const {
        static jni::jclass* javaClass = jni::NewGlobalRef(env, &jni::FindClass(env, "java/lang/Boolean")).release();
        static jni::jmethodID* constructor = &jni::GetMethodID(env, *javaClass, "<init>", "(Z)V");
        return {&jni::NewObject(env, *javaClass, *constructor, (jboolean) value)};
    }
};

template <>
struct Converter<jni::jboolean, bool> {
    Result<jni::jboolean> operator()(jni::JNIEnv&, const bool& value) const {
        return {(jni::jboolean) value};
    }
};

template <>
struct Converter<jni::jobject*, float> {
    Result<jni::jobject*> operator()(jni::JNIEnv& env, const float& value) const {
        static jni::jclass* javaClass = jni::NewGlobalRef(env, &jni::FindClass(env, "java/lang/Float")).release();
        static jni::jmethodID* constructor = &jni::GetMethodID(env, *javaClass, "<init>", "(F)V");
        return {&jni::NewObject(env, *javaClass, *constructor, (jfloat) value)};
    }
};

template <>
struct Converter<jni::jfloat, float> {
    Result<jni::jfloat> operator()(jni::JNIEnv&, const float& value) const {
        return {(jni::jfloat) value};
    }
};


template <>
struct Converter<jni::jobject*, double> {
    Result<jni::jobject*> operator()(jni::JNIEnv& env, const double& value) const {
        static jni::jclass* javaClass = jni::NewGlobalRef(env, &jni::FindClass(env, "java/lang/Double")).release();
        static jni::jmethodID* constructor = &jni::GetMethodID(env, *javaClass, "<init>", "(D)V");
        return {&jni::NewObject(env, *javaClass, *constructor, (jfloat) value)};
    }
};

template <>
struct Converter<jni::jdouble, float> {
    Result<jni::jdouble> operator()(jni::JNIEnv&, const double& value) const {
        return {(jni::jdouble) value};
    }
};

/**
 * All integrals. java is limited to 64 bit signed, so...
 * TODO: use BigDecimal for > 64 / unsigned?
 */
template<typename T>
struct Converter<jni::jobject*, T, typename std::enable_if<std::is_integral<T>::value>::type> {
    Result<jni::jobject*> operator()(jni::JNIEnv& env, const T& value) const {
        static jni::jclass* javaClass = jni::NewGlobalRef(env, &jni::FindClass(env, "java/lang/Long")).release();
        static jni::jmethodID* constructor = &jni::GetMethodID(env, *javaClass, "<init>", "(J)V");
        return {&jni::NewObject(env, *javaClass, *constructor, (jlong) value)};
    }
};

// TODO: convert integral types to primitive jni types

template <>
struct Converter<jni::jobject*, std::string> {
    Result<jni::jobject*> operator()(jni::JNIEnv& env, const std::string& value) const {
        return {jni::Make<jni::String>(env, value).Get()};
    }
};

template <>
struct Converter<jni::jstring*, std::string> {
    Result<jni::jstring*> operator()(jni::JNIEnv& env, const std::string& value) const {
        return {jni::Make<jni::String>(env, value).Get()};
    }
};

template <>
struct Converter<jni::jobject*, Color> {
    Result<jni::jobject*> operator()(jni::JNIEnv& env, const Color& value) const {
        std::stringstream sstream;
        sstream << "rgba(" << value.r << ", " << value.g << ", " << value.b << ", "  << value.a << ")";
        std::string result = sstream.str();
        return convert<jni::jobject*, std::string>(env, result);
    }
};

template <std::size_t N>
struct Converter<jni::jobject*, std::array<float, N>> {
    Result<jni::jobject*> operator()(jni::JNIEnv& env, const std::array<float, N>& value) const {
        std::vector<float> v;
        for (const float& id : value) {
            v.push_back(id);
        }
        return convert<jni::jobject*, std::vector<float>>(env, v);
    }
};

template <>
struct Converter<jni::jobject*, std::vector<std::string>> {
    Result<jni::jobject*> operator()(jni::JNIEnv& env, const std::vector<std::string>& value) const {
        static jni::jclass* stringCass = jni::NewGlobalRef(env, &jni::FindClass(env, "java/lang/String")).release();
        jni::jarray<jni::jobject>& jarray = jni::NewObjectArray(env, value.size(), *stringCass);

        for(size_t i = 0; i < value.size(); i = i + 1) {
            Result<jni::jobject*> converted = convert<jni::jobject*, std::string>(env, value.at(i));
            jni::SetObjectArrayElement(env, jarray, i, *converted);
        }

        return &jarray;
    }
};

template <>
struct Converter<jni::jobject*, std::vector<float>> {
    Result<jni::jobject*> operator()(jni::JNIEnv& env, const std::vector<float>& value) const {
        static jni::jclass* floatClass = jni::NewGlobalRef(env, &jni::FindClass(env, "java/lang/Float")).release();
        jni::jarray<jni::jobject>& jarray = jni::NewObjectArray(env, value.size(), *floatClass);

        for(size_t i = 0; i < value.size(); i = i + 1) {
            Result<jni::jobject*> converted = convert<jni::jobject*, float>(env, value.at(i));
            jni::SetObjectArrayElement(env, jarray, i, *converted);
        }

        return &jarray;
    }
};

// Java -> C++

template <>
struct Converter<std::string, jni::String> {
    Result<std::string> operator()(jni::JNIEnv& env, const jni::String& value) const {
        return { jni::Make<std::string>(env, value) };
    }
};

} // namespace conversion
} // namespace style
} // namespace mbgl