Boost logo

Boost :

From: Phlip (pplumlee_at_[hidden])
Date: 2001-02-09 15:47:08


Proclaimed David Abrahams from the mountaintops:

> if (result.get() != 0) {
> return boost::python::reference(boost::python::to_python(result));
> }
> else {
> Py_INCREF(Py_None);
> return boost::python::reference(Py_None);

Below my sig appears how far I could take this in example1.cpp. But it
expires with these dying screams:

[phlip_at_rrm example]$ g++ -fPIC -Wall -W -I/usr/include/python2.0/ -I. -o
example2.o -c ../example/example2.cppboost/python/reference.hpp: In method
`boost::python::reference<boost::python::detail::extension_instance>::reference<PyObject>(PyObject
*)':
../example/example2.cpp:20: instantiated from here
boost/python/reference.hpp:76: type
`boost::python::detail::extension_instance' is not a base type for type
`PyObject'
../example/example2.cpp: In method `class reference_t
hello::world::getWorld(int)':
../example/example2.cpp:24: parse error before `return'
../example/example2.cpp:26: warning: control reaches end of non-void
function `hello::world::getWorld(int)'
boost/python/detail/extension_class.hpp: In function `struct PyObject *
to_python<boost::shared_ptr<hello::world> >(const
boost::shared_ptr<hello::world> &)':
../example/example2.cpp:20: instantiated from here
boost/python/detail/extension_class.hpp:299: conversion from
`boost::python::type<boost::shared_ptr<hello::world> >' to non-scalar type
`boost::python::type<hello::world>' requested
boost/python/detail/extension_class.hpp:299: warning: control reaches end
of non-void function `to_python<boost::shared_ptr<hello::world> >(const
boost::shared_ptr<hello::world> &)'
make: *** [example2.o] Error 1

I'l keep researching what to put in the reference<> template, so I'm
posting my results asynchronously...

> Maybe I should automatically convert null smart pointers to None and
> None to null smart pointers. I never thought to do it. Opinions?

Ain't that what both SWIG and CXX do?

-- 
  Phlip                          phlip_cpp_at_[hidden]
============ http://c2.com/cgi/wiki?PhlIp ============
  --  Set phasers on illin'  --
#include <string.h>
#include <boost/python/class_builder.hpp>
#include <boost/python/conversions.hpp>
using namespace boost; // don't do this
typedef python::reference<python::detail::extension_instance>
		reference_t;
namespace hello {
  class world
  {
   public:
      world(int) {}
      ~world() {}
      const char* get() const { return "hi, world"; }
	typedef shared_ptr<world> world_ptr_t;
	reference_t getWorld(int x) {
		if (x)
			return to_python (world_ptr_t (new world (x)));
		else
			{
			Py_INCREF(Py_None);
			return return Py_None;
			}
		}
  };
  size_t length(const world& x) { return strlen(x.get()); }
}

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk