summaryrefslogtreecommitdiff
path: root/platform/android/src/conversion/constant.hpp
blob: 298fb7faa1f3874a48cf1ff30f4315af74f13782 (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
#pragma once

#include "conversion.hpp"

#include <mbgl/util/color.hpp>
#include <mbgl/util/enum.hpp>

#include <mbgl/style/expression/formatted.hpp>

#include <jni/jni.hpp>

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

namespace mbgl {
namespace android {
namespace conversion {

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

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

template <>
struct Converter<jni::Local<jni::Object<>>, double> {
    Result<jni::Local<jni::Object<>>> operator()(jni::JNIEnv& env, const double& value) const;
};

/**
 * All integrals. java is limited to 64 bit signed, so...
 * TODO: use BigDecimal for > 64 / unsigned?
 */
template<typename T>
struct Converter<jni::Local<jni::Object<>>, T, typename std::enable_if<std::is_integral<T>::value>::type> {
    Result<jni::Local<jni::Object<>>> operator()(jni::JNIEnv& env, const T& value) const {
        return jni::Box(env, jni::jlong(value));
    }
};

// TODO: convert integral types to primitive jni types

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

template <>
struct Converter<jni::Local<jni::Object<>>, Color> {
    Result<jni::Local<jni::Object<>>> operator()(jni::JNIEnv& env, const Color& value) const;
};

template <>
struct Converter<jni::Local<jni::Object<>>, style::expression::Formatted> {
    Result<jni::Local<jni::Object<>>> operator()(jni::JNIEnv& env, const style::expression::Formatted& value) const;
};

template <std::size_t N>
struct Converter<jni::Local<jni::Object<>>, std::array<float, N>> {
    Result<jni::Local<jni::Object<>>> 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::Local<jni::Object<>>, std::vector<float>>(env, v);
    }
};

template <>
struct Converter<jni::Local<jni::Object<>>, std::vector<std::string>> {
    Result<jni::Local<jni::Object<>>> operator()(jni::JNIEnv& env, const std::vector<std::string>& value) const;
};

template <>
struct Converter<jni::Local<jni::Object<>>, std::vector<float>> {
    Result<jni::Local<jni::Object<>>> operator()(jni::JNIEnv& env, const std::vector<float>& value) const;
};

template <>
struct Converter<jni::Local<jni::Object<>>, style::RadialOffsetType> : public Converter<jni::Local<jni::Object<>>, std::vector<float>> {
};

template <class T>
struct Converter<jni::Local<jni::Object<>>, T, typename std::enable_if_t<std::is_enum<T>::value>> {
    Result<jni::Local<jni::Object<>>> operator()(jni::JNIEnv& env, const T& value) const {
        return convert<jni::Local<jni::Object<>>, std::string>(env, Enum<T>::toString(value));
    }
};

template <class T>
struct Converter<jni::Local<jni::Object<>>, std::vector<T>, typename std::enable_if_t<std::is_enum<T>::value>> {
    Result<jni::Local<jni::Object<>>> operator()(jni::JNIEnv& env, const std::vector<T>& value) const {
        auto result = jni::Array<jni::String>::New(env, value.size());
        for (std::size_t i = 0; i < value.size(); ++i) {
            result.Set(env, i, jni::Make<jni::String>(env, Enum<T>::toString(value.at(i))));
        }
        return result;
    }
};

} // namespace conversion
} // namespace android
} // namespace mbgl