-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContainers.cpp
102 lines (92 loc) · 3.17 KB
/
Containers.cpp
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
// Copyright (c) 2013-2014 Josh Blum
// SPDX-License-Identifier: BSL-1.0
#include <Pothos/Plugin.hpp>
#include <Pothos/Proxy.hpp>
/***********************************************************************
* Vector
**********************************************************************/
static Pothos::Proxy convertVectorToJVector(Pothos::ProxyEnvironment::Sptr env, const Pothos::ProxyVector &vec)
{
auto jVector = env->findProxy("java.util.Vector").call("new");
jVector.call("setSize", vec.size());
for (size_t i = 0; i < vec.size(); i++)
{
jVector.call("set", i, vec[i]);
}
return jVector;
}
static Pothos::ProxyVector convertJVectorToVector(const Pothos::Proxy &proxy)
{
Pothos::ProxyVector vec(proxy.call<size_t>("size"));
for (size_t i = 0; i < vec.size(); i++)
{
vec[i] = proxy.call("get", i);
}
return vec;
}
pothos_static_block(pothosRegisterJavaVectorConversions)
{
Pothos::PluginRegistry::addCall("/proxy/converters/java/vector_to_jvector",
&convertVectorToJVector);
Pothos::PluginRegistry::add("/proxy/converters/java/jvector_to_vector",
Pothos::ProxyConvertPair("java.util.Vector", &convertJVectorToVector));
}
/***********************************************************************
* Set
**********************************************************************/
static Pothos::Proxy convertSetToJSet(Pothos::ProxyEnvironment::Sptr env, const Pothos::ProxySet &set)
{
auto jSet = env->findProxy("java.util.HashSet").call("new");
for (const auto &entry : set)
{
jSet.call("add", entry);
}
return jSet;
}
static Pothos::ProxySet convertJSetToSet(const Pothos::Proxy &proxy)
{
Pothos::ProxySet set;
auto it = proxy.call("iterator");
while (it.call<bool>("hasNext"))
{
set.insert(it.call("next"));
}
return set;
}
pothos_static_block(pothosRegisterJavaSetConversions)
{
Pothos::PluginRegistry::addCall("/proxy/converters/java/set_to_jset",
&convertSetToJSet);
Pothos::PluginRegistry::add("/proxy/converters/java/jset_to_set",
Pothos::ProxyConvertPair("java.util.HashSet", &convertJSetToSet));
}
/***********************************************************************
* Map
**********************************************************************/
static Pothos::Proxy convertMapToJMap(Pothos::ProxyEnvironment::Sptr env, const Pothos::ProxyMap &map)
{
auto jMap = env->findProxy("java.util.HashMap").call("new");
for (const auto &entry : map)
{
jMap.call("put", entry.first, entry.second);
}
return jMap;
}
static Pothos::ProxyMap convertJMapToMap(const Pothos::Proxy &proxy)
{
Pothos::ProxyMap map;
auto it = proxy.call("entrySet").call("iterator");
while (it.call<bool>("hasNext"))
{
auto entry = it.call("next");
map[entry.call("getKey")] = entry.call("getValue");
}
return map;
}
pothos_static_block(pothosRegisterJavaMapConversions)
{
Pothos::PluginRegistry::addCall("/proxy/converters/java/map_to_jmap",
&convertMapToJMap);
Pothos::PluginRegistry::add("/proxy/converters/java/jmap_to_map",
Pothos::ProxyConvertPair("java.util.HashMap", &convertJMapToMap));
}