-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathmapnik_value_converter.hpp
More file actions
204 lines (178 loc) · 5.63 KB
/
mapnik_value_converter.hpp
File metadata and controls
204 lines (178 loc) · 5.63 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2024 Artem Pavlenko
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*****************************************************************************/
#ifndef MAPNIK_PYTHON_BINDING_VALUE_CONVERTER_INCLUDED
#define MAPNIK_PYTHON_BINDING_VALUE_CONVERTER_INCLUDED
// mapnik
#include <mapnik/value.hpp>
#include <mapnik/params.hpp>
#include <mapnik/unicode.hpp>
//pybind11
#include <pybind11/pybind11.h>
namespace {
struct value_converter
{
PyObject * operator() (mapnik::value_integer val) const
{
return ::PyLong_FromLongLong(val);
}
PyObject * operator() (mapnik::value_double val) const
{
return ::PyFloat_FromDouble(val);
}
PyObject * operator() (mapnik::value_bool val) const
{
return ::PyBool_FromLong(val);
}
PyObject * operator() (std::string const& s) const
{
return ::PyUnicode_DecodeUTF8(s.c_str(), static_cast<ssize_t>(s.length()),0);
}
PyObject * operator() (mapnik::value_unicode_string const& s) const
{
const char* data = reinterpret_cast<const char*>(s.getBuffer());
Py_ssize_t size = static_cast<Py_ssize_t>(s.length() * sizeof(s[0]));
return ::PyUnicode_DecodeUTF16(data, size, nullptr, nullptr);
}
PyObject * operator() (mapnik::value_null const& /*s*/) const
{
Py_RETURN_NONE;
}
};
} // namespace
struct mapnik_value_to_python
{
static PyObject* convert(mapnik::value const& v)
{
return mapnik::util::apply_visitor(value_converter(),v);
}
};
struct mapnik_param_to_python
{
static PyObject* convert(mapnik::value_holder const& v)
{
return mapnik::util::apply_visitor(value_converter(),v);
}
};
namespace PYBIND11_NAMESPACE { namespace detail {
template <>
struct type_caster<mapnik::value>
{
mapnik::transcoder const tr_{"utf8"};
public:
PYBIND11_TYPE_CASTER(mapnik::value, const_name("Value"));
bool load(handle src, bool)
{
PyObject *source = src.ptr();
if (PyUnicode_Check(source))
{
PyObject* tmp = PyUnicode_AsUTF8String(source);
if (!tmp) return false;
char* c_str = PyBytes_AsString(tmp);
value = tr_.transcode(c_str);
Py_DecRef(tmp);
return !PyErr_Occurred();
}
else if (PyBool_Check(source))
{
value = (source == Py_True) ? true : false;
return !PyErr_Occurred();
}
else if (PyFloat_Check(source))
{
PyObject *tmp = PyNumber_Float(source);
if (!tmp) return false;
value = PyFloat_AsDouble(tmp);
Py_DecRef(tmp);
return !PyErr_Occurred();
}
else if(PyLong_Check(source))
{
PyObject *tmp = PyNumber_Long(source);
if (!tmp) return false;
value = PyLong_AsLong(tmp);
Py_DecRef(tmp);
return !PyErr_Occurred();
}
else if (source == Py_None)
{
value = mapnik::value_null{};
return true;
}
return false;
}
static handle cast(mapnik::value src, return_value_policy /*policy*/, handle /*parent*/)
{
return mapnik_value_to_python::convert(src);
}
};
template <>
struct type_caster<mapnik::value_holder>
{
public:
PYBIND11_TYPE_CASTER(mapnik::value_holder, const_name("ValueHolder"));
bool load(handle src, bool)
{
PyObject *source = src.ptr();
if (PyUnicode_Check(source))
{
PyObject* tmp = PyUnicode_AsUTF8String(source);
if (!tmp) return false;
char* c_str = PyBytes_AsString(tmp);
value = std::string(c_str);
Py_DecRef(tmp);
return !PyErr_Occurred();
}
else if (PyBool_Check(source))
{
value = (source == Py_True) ? true : false;
return !PyErr_Occurred();
}
else if (PyFloat_Check(source))
{
PyObject *tmp = PyNumber_Float(source);
if (!tmp) return false;
value = PyFloat_AsDouble(tmp);
Py_DecRef(tmp);
return !PyErr_Occurred();
}
else if(PyLong_Check(source))
{
PyObject *tmp = PyNumber_Long(source);
if (!tmp) return false;
value = static_cast<mapnik::value_integer>(PyLong_AsLong(tmp));
Py_DecRef(tmp);
return !PyErr_Occurred();
}
else if (source == Py_None)
{
value = mapnik::value_null{};
return true;
}
return false;
}
static handle cast(mapnik::value_holder src, return_value_policy /*policy*/, handle /*parent*/)
{
return mapnik_param_to_python::convert(src);
}
};
}} // namespace PYBIND11_NAMESPACE::detail
#endif // MAPNIK_PYTHON_BINDING_VALUE_CONVERTER_INCLUDED