Skip to content

Commit 4277e59

Browse files
committed
Add 'caster' for mapnik::value_holder (TODO: consider using mapnik::value instead)
1 parent e9f88a9 commit 4277e59

1 file changed

Lines changed: 54 additions & 0 deletions

File tree

src/mapnik_value_converter.hpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,60 @@ struct type_caster<mapnik::value>
144144
}
145145
};
146146

147+
template <>
148+
struct type_caster<mapnik::value_holder>
149+
{
150+
public:
151+
152+
PYBIND11_TYPE_CASTER(mapnik::value_holder, const_name("ValueHolder"));
153+
154+
bool load(handle src, bool)
155+
{
156+
PyObject *source = src.ptr();
157+
if (PyUnicode_Check(source))
158+
{
159+
PyObject* tmp = PyUnicode_AsUTF8String(source);
160+
if (!tmp) return false;
161+
char* c_str = PyBytes_AsString(tmp);
162+
value = std::string(c_str);
163+
Py_DecRef(tmp);
164+
return !PyErr_Occurred();
165+
}
166+
else if (PyBool_Check(source))
167+
{
168+
value = (source == Py_True) ? true : false;
169+
return !PyErr_Occurred();
170+
}
171+
else if (PyFloat_Check(source))
172+
{
173+
PyObject *tmp = PyNumber_Float(source);
174+
if (!tmp) return false;
175+
value = PyFloat_AsDouble(tmp);
176+
Py_DecRef(tmp);
177+
return !PyErr_Occurred();
178+
}
179+
else if(PyLong_Check(source))
180+
{
181+
PyObject *tmp = PyNumber_Long(source);
182+
if (!tmp) return false;
183+
value = PyLong_AsLongLong(tmp);
184+
Py_DecRef(tmp);
185+
return !PyErr_Occurred();
186+
}
187+
else if (source == Py_None)
188+
{
189+
value = mapnik::value_null{};
190+
return true;
191+
}
192+
return false;
193+
}
194+
195+
static handle cast(mapnik::value_holder src, return_value_policy /*policy*/, handle /*parent*/)
196+
{
197+
return mapnik_param_to_python::convert(src);
198+
}
199+
};
200+
147201
}} // namespace PYBIND11_NAMESPACE::detail
148202

149203

0 commit comments

Comments
 (0)