Settings rework for plan/model/context and interfacing with python

This commit is contained in:
Thomas Krijnen
2024-04-18 21:04:02 +02:00
parent b62b328f1d
commit 0edfb0e791
8 changed files with 164 additions and 44 deletions
+31 -6
View File
@@ -85,18 +85,40 @@
return static_cast<IfcUtil::IfcBaseClass*>(SWIG_IsOK(res) ? arg : 0);
}
template <typename T>
std::vector<T> python_sequence_as_vector(PyObject* aggregate) {
std::vector<T> result_vector;
result_vector.reserve(PySequence_Size(aggregate));
template<typename T>
void add_to_container(std::vector<T>& container, const T& element) {
container.push_back(element);
}
template<typename T>
void add_to_container(std::set<T>& container, const T& element) {
container.insert(element);
}
template <typename T, template<typename> typename U>
U<T> python_sequence_as_cpp_container(PyObject* aggregate) {
U<T> result_vector;
if constexpr (std::is_same_v<U<T>, std::vector<T>>) {
result_vector.reserve(PySequence_Size(aggregate));
}
for(Py_ssize_t i = 0; i < PySequence_Size(aggregate); ++i) {
PyObject* element = PySequence_GetItem(aggregate, i);
T t = cast_pyobject<T>(element);
result_vector.push_back(t);
add_to_container(result_vector, t);
}
return result_vector;
}
template <typename T>
std::vector<T> python_sequence_as_vector(PyObject* aggregate) {
return python_sequence_as_cpp_container<T, std::vector>(aggregate);
}
template <typename T>
std::set<T> python_sequence_as_set(PyObject* aggregate) {
return python_sequence_as_cpp_container<T, std::set>(aggregate);
}
template <typename T>
std::vector< std::vector<T> > python_sequence_as_vector_of_vector(PyObject* aggregate) {
std::vector< std::vector<T> > result_vector;
@@ -196,7 +218,10 @@
if constexpr (std::is_same_v<std::remove_cv_t<std::remove_reference_t<T>>, std::set<int>>) {
std::vector<int> vs(t.begin(), t.end());
return pythonize_vector(vs);
} else {
} else if constexpr (std::is_same_v<std::remove_cv_t<std::remove_reference_t<T>>, std::set<std::string>>) {
std::vector<std::string> vs(t.begin(), t.end());
return pythonize_vector(vs);
} else {
return pythonize(t);
}
}
+34
View File
@@ -372,3 +372,37 @@ CREATE_OPTIONAL_TYPEMAP_IN(std::string, string, str)
%typemap(freearg) const std::vector<IfcGeom::OpaqueCoordinate<4>>& {
delete $1;
}
%define CREATE_SET_TYPEMAP_IN(template_type)
%typemap(in) std::set<template_type> {
if (!check_aggregate_of_type($input, get_python_type<template_type>())) {
SWIG_exception(SWIG_TypeError, "Invalid");
}
$1 = python_sequence_as_set<template_type>($input);
}
%typemap(typecheck,precedence=SWIG_TYPECHECK_INTEGER) std::set<template_type> {
$1 = check_aggregate_of_type($input, get_python_type<template_type>()) ? 1 : 0;
}
%typemap(typecheck,precedence=SWIG_TYPECHECK_INTEGER) const std::set<template_type>& {
$1 = check_aggregate_of_type($input, get_python_type<template_type>()) ? 1 : 0;
}
%typemap(arginit) const std::set<template_type>& {
$1 = new std::set<template_type>();
}
%typemap(in) const std::set<template_type>& {
if (!check_aggregate_of_type($input, get_python_type<template_type>())) {
SWIG_exception(SWIG_TypeError, "Invalid");
}
*$1 = python_sequence_as_set<template_type>($input);
}
%typemap(freearg) const std::set<template_type>& {
delete $1;
}
%enddef
CREATE_SET_TYPEMAP_IN(int)
CREATE_SET_TYPEMAP_IN(std::string)