From 1375f25de98bac0c40bdb85d6ca92b565bceeefb Mon Sep 17 00:00:00 2001 From: Thomas Krijnen Date: Sat, 24 Jul 2021 11:19:07 +0200 Subject: [PATCH] #1564 typemap for optional string --- src/ifcwrap/utils/typemaps_in.i | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/src/ifcwrap/utils/typemaps_in.i b/src/ifcwrap/utils/typemaps_in.i index 62f05ee390..a5ef36bcb6 100644 --- a/src/ifcwrap/utils/typemaps_in.i +++ b/src/ifcwrap/utils/typemaps_in.i @@ -239,4 +239,34 @@ CREATE_VECTOR_TYPEMAP_IN(std::string, STRING, str) %typemap(typecheck, precedence=SWIG_TYPECHECK_INTEGER) const Bnd_Box& { $1 = check_aggregate_of_aggregate_of_type($input, get_python_type()); -} \ No newline at end of file +} + +%define CREATE_OPTIONAL_TYPEMAP_IN(template_type, express_name, python_name) + + %typemap(in) const boost::optional& { + if ($input == Py_None) { + (*$1) = boost::none; + } else if ($input->ob_type != get_python_type()) { + SWIG_exception(SWIG_TypeError, "Optional " #express_name " needs a " #python_name " or None"); + } else { + (*$1) = cast_pyobject($input); + } + } + + %typemap(typecheck,precedence=SWIG_TYPECHECK_INTEGER) const boost::optional& { + $1 = ($input == Py_None || $input->ob_type == get_python_type()) ? 1 : 0; + } + + %typemap(arginit) const boost::optional& { + $1 = new boost::optional(); + } + + %typemap(freearg) const boost::optional& { + delete $1; + } + +%enddef + +CREATE_OPTIONAL_TYPEMAP_IN(int, integer, int) +CREATE_OPTIONAL_TYPEMAP_IN(double, real, float) +CREATE_OPTIONAL_TYPEMAP_IN(std::string, string, str) \ No newline at end of file