[C++-sig] call policies

Ralf W. Grosse-Kunstleve rwgk at yahoo.com
Mon Nov 7 20:11:16 CET 2005


--- Ricardo Kirkner <ricardokirkner at gmail.com> wrote:

> this time I have a question regarding call policies. I have a function
> that returns a double * which actually is an array of doubles.
> 
> I am required to provide a policy for this functions, however I cannot
> find out which one would apply here (from how well I understand each
> posibility).
> 
> Can anyone please enlighten me?

I don't think call policies will help you here. More fundamental
considerations are required.

The Python scripting level is supposed to provide a safe environment.
According to the Python spirit is should be impossible to provoke a
segmentation fault from the scripting level, or to access uninitialized
memory. Of course you can undermine this via C/C++, but Boost.Python
doesn't support grossly unsafe practices.

In your particular case, you have to tell Python the size of the
double* array, and you have to take care all elements are initialized.
There are several alternatives to achieve this. A simple alternative is
to write a "thin wrapper" which copies the double* array to a new
Python list of floats. It goes like this (all untested):

your.hpp:

  class yours
  {
    public:
      unsigned
      number_of_values() const;

      double*
      values() const;
  };

Boost.Python extension:

  #include <boost/python.hpp>

  boost::python::list
  values_as_list(yours const& self)
  {
    boost::python::list result;
    for(unsigned i=0;i<self.number_of_values();i++) {
      result.append(self.values()[i]);
    }
    return result;
  }

  BOOST_PYTHON_MODULE(your_choice)
  {
    using namespace boost::python;
    class_<yours>("yours")
      def("values", values_as_list)
    ;
  }

Cheers,
        Ralf



		
__________________________________ 
Yahoo! FareChase: Search multiple travel sites in one click.
http://farechase.yahoo.com



More information about the Cplusplus-sig mailing list