Skip to content

Commit

Permalink
Merge pull request #174 from OpenMS/jpfeuffer-patch-1
Browse files Browse the repository at this point in the history
Try to add real BooleanConverter
  • Loading branch information
jpfeuffer authored Dec 21, 2022
2 parents 9bfd55e + 7a62e8f commit d4fd150
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 6 deletions.
5 changes: 3 additions & 2 deletions autowrap/CodeGenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2100,8 +2100,9 @@ def create_default_cimports(self):
|#Generated with autowrap %s and Cython (Parser) %s
|#cython: c_string_encoding=ascii
|#cython: embedsignature=False
|from enum import Enum as _PyEnum
|from cpython cimport Py_buffer
|from enum import Enum as _PyEnum
|from cpython cimport Py_buffer
|from cpython cimport bool as pybool_t
|from libcpp.string cimport string as libcpp_string
|from libcpp.string cimport string as libcpp_utf8_string
|from libcpp.string cimport string as libcpp_utf8_output_string
Expand Down
38 changes: 37 additions & 1 deletion autowrap/ConversionProvider.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ class IntegerConverter(TypeConverterBase):
def get_base_types(self) -> List[str]:
return [
"int",
"bool",
"bint", # C boolean type
"long",
"int32_t",
"ptrdiff_t",
Expand Down Expand Up @@ -255,6 +255,41 @@ def output_conversion(
return "%s = <%s>%s" % (output_py_var, cpp_type, input_cpp_var)


class BooleanConverter(TypeConverterBase):
"""
Wraps a C++ bool. Bools are automatically imported in the beginning of a file with
'from libcpp import bool'.
"""

def get_base_types(self) -> List[str]:
return [
"bool",
]

def matches(self, cpp_type: CppType) -> bool:
return not cpp_type.is_ptr

def matching_python_type(self, cpp_type: CppType) -> str:
return "bool"

def matching_python_type_full(self, cpp_type: CppType) -> str:
return "bool"

def type_check_expression(self, cpp_type: CppType, argument_var: str) -> str:
return "isinstance(%s, pybool_t)" % (argument_var,)

def input_conversion(self, cpp_type, argument_var, arg_num) -> Tuple[str, str, str]:
code = ""
call_as = "(<%s>%s)" % (cpp_type, argument_var)
cleanup = ""
return code, call_as, cleanup

def output_conversion(
self, cpp_type: CppType, input_cpp_var: str, output_py_var: str
) -> Optional[str]:
return "%s = <%s>%s" % (output_py_var, cpp_type, input_cpp_var)


class UnsignedIntegerConverter(TypeConverterBase):
"""
wraps unsigned long and int. "long" base_type is converted to "int" by the
Expand Down Expand Up @@ -2234,6 +2269,7 @@ def setup_converter_registry(classes_to_wrap, enums_to_wrap, instance_map):
)

converters.register(IntegerConverter())
converters.register(BooleanConverter())
converters.register(UnsignedIntegerConverter())
converters.register(FloatConverter())
converters.register(DoubleConverter())
Expand Down
4 changes: 4 additions & 0 deletions tests/test_code_generator_minimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ def output_conversion(self, cpp_type, input_cpp_var, output_py_var):
minimal.m_accessible = 10
assert minimal.m_accessible == 10

minimal.m_bool = True
assert isinstance(minimal.m_bool, bool)
assert minimal.m_bool == True

try:
minimal.m_const = 10
assert False
Expand Down
7 changes: 4 additions & 3 deletions tests/test_files/minimal.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#include <iostream>
#include "minimal.hpp"

Minimal::Minimal() : _i(0), _mi(), m_accessible(0)
Minimal::Minimal() : _i(0), _mi(), m_accessible(0), m_bool(false)
{
}

Minimal::Minimal(std::vector<int> const & ii): _mi(), m_accessible(0)
Minimal::Minimal(std::vector<int> const & ii): _mi(), m_accessible(0), m_bool(false)
{
_i = ii.size();
}

Minimal::Minimal(int i) : _i(i), _mi(), m_accessible(0)
Minimal::Minimal(int i) : _i(i), _mi(), m_accessible(0), m_bool(false)
{
}

Expand All @@ -19,6 +19,7 @@ Minimal::Minimal(const Minimal &m)
_i = m._i;
_mi = m._mi;
m_accessible = m.m_accessible;
m_bool = m.m_bool;
}


Expand Down
1 change: 1 addition & 0 deletions tests/test_files/minimal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Minimal {
static const int m_const = -1;
static const int m_constdef = -1;
int m_accessible;
bool m_bool;

Minimal();
Minimal(Int);
Expand Down
2 changes: 2 additions & 0 deletions tests/test_files/minimal.pxd
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# cython: language_level=2
from libcpp.string cimport string as libcpp_string
from libcpp.vector cimport vector as libcpp_vector
from libcpp cimport bool
from libc.string cimport const_char

from minimal_td cimport *
Expand All @@ -22,6 +23,7 @@ cdef extern from "minimal.hpp":
Minimal(Minimal &)

int m_accessible
bool m_bool
int m_const # wrap-constant
const int m_constdef

Expand Down

0 comments on commit d4fd150

Please sign in to comment.