diff --git a/pybindgen/cppclass.py b/pybindgen/cppclass.py index e952a42..39468f8 100644 --- a/pybindgen/cppclass.py +++ b/pybindgen/cppclass.py @@ -409,6 +409,7 @@ class CppClass(object): self.helper_class_disabled = False self.cannot_be_constructed = '' # reason self.has_trivial_constructor = False + self.has_operator_os = False self._have_pure_virtual_methods = None ## list of CppClasses from which a value of this class can be ## implicitly generated; corresponds to a @@ -1083,6 +1084,9 @@ public: if not wrapper.parameters: self.has_trivial_constructor = True + def add_operator_os(self): + self.has_operator_os = True + def add_constructor(self, *args, **kwargs): """ Add a constructor to the class. See the documentation for @@ -1318,6 +1322,8 @@ typedef struct { self._generate_gc_methods(code_sink) self._generate_destructor(code_sink, have_constructor) + if self.has_operator_os: + self._generate_str(code_sink) self._generate_type_structure(code_sink, docstring) def _generate_type_structure(self, code_sink, docstring): @@ -1506,6 +1512,23 @@ static int } ''' % (tp_traverse_function_name, self.pystruct, visit_self)) + def _generate_str(self, code_sink): + """Generate a tp_str function and register it in the type""" + + tp_str_function_name = "_wrap_%s__tp_str" % (self.pystruct,) + self.slots.setdefault("tp_str", tp_str_function_name ) + + code_sink.writeln(''' +static PyObject * +%s(%s *self) +{ + std::ostringstream oss; + oss << *self->obj; + return PyString_FromString(oss.str ().c_str ()); +} +''' % (tp_str_function_name, self.pystruct)) + code_sink.writeln() + def _generate_destructor(self, code_sink, have_constructor): """Generate a tp_dealloc function and register it in the type""" diff --git a/pybindgen/gccxmlparser.py b/pybindgen/gccxmlparser.py index e5a40f1..ddda34d 100644 --- a/pybindgen/gccxmlparser.py +++ b/pybindgen/gccxmlparser.py @@ -1203,9 +1203,34 @@ pybindgen.settings.error_handler = ErrorHandler() return True return False + def _is_ostream(self,type): + return isinstance (type, cpptypes.reference_t) \ + and not isinstance (type.base, cpptypes.const_t) \ + and str (type.base) == 'std::ostream' + def _scan_class_methods(self, cls, class_wrapper, pygen_sink): have_trivial_constructor = False have_copy_constructor = False + has_operator_os = False + + + for op in self.module_namespace.free_operators(function=self.location_filter, + allow_empty=True, + recursive=True): + if self._is_ostream (op.return_type) \ + and len (op.arguments) >= 2 \ + and self._is_ostream (op.arguments[0].type) \ + and type_traits.is_convertible (cls, op.arguments[1].type): + has_operator_os = True + + for op in cls.member_operators(function=self.location_filter, + allow_empty=True, + recursive=True): + if self._is_ostream (op.return_type) \ + and len (op.arguments) >= 2 \ + and self._is_ostream (op.arguments[0].type) \ + and type_traits.is_convertible (cls, op.arguments[1].type): + has_operator_os = True if pygen_sink is None: pygen_sink = NullCodeSink() @@ -1471,7 +1496,7 @@ pybindgen.settings.error_handler = ErrorHandler() if type_traits.has_trivial_constructor(cls): class_wrapper.add_constructor([]) pygen_sink.writeln("cls.add_constructor([])") - + if not have_copy_constructor: try: # pygccxml > 0.9 has_copy_constructor = type_traits.has_copy_constructor(cls) @@ -1481,6 +1506,8 @@ pybindgen.settings.error_handler = ErrorHandler() class_wrapper.add_constructor([ class_wrapper.ThisClassRefParameter("%s const &" % class_wrapper.full_name, 'ctor_arg', is_const=True)]) + if has_operator_os: + pygen_sink.writeln("cls.add_operator_os()") def scan_functions(self):