$include_dir="/home/hyper-archives/boost-users/include"; include("$include_dir/msg-header.inc") ?>
From: Qinfeng(Javen) Shi  (shiqinfeng_at_[hidden])
Date: 2006-08-28 08:47:57
Dear all,
I wraped a v_varray class into python by boost.
I called it in python, most of its attributes work well. Only when I call
a.push(1.0),it crashed  due to  not match  type.The problem is that my input
argument in c++ is float*(for passing an array) while python has no pointer
type.
How can I pass a variable in python to a.push() to make sure it work
correctly?
Many thanks for help!
Javen
/*************************running result in python:****************/
>>> a.push(1.0)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
Boost.Python.ArgumentError: Python argument types in
    array_floatstar.push(array_floatstar, float)
did not match C++ signature:
    push(v_array<float*> {lvalue}, float*)
>>>
/*************************v_array def in stack.h:****************/
#ifndef _STACK_H
#define _STACK_H
#include <stdlib.h>
template<class T> class v_array{
 public:
  int index;
  int length;
  T* elements;
  T last() { return elements[index-1];}
  void decr() { index--;}
  v_array() { index = 0; length=0; elements = NULL;}
  T& operator[](unsigned int i) { return elements[i]; }
  void push(const T &new_ele)
  {
    while(index >= length)
    {
      length = 2*length + 3;
      elements = (T *)realloc(elements,sizeof(T) * length);
    }
    this->elements[index++] = new_ele;
  }
  void alloc(int newLength)
  {
    elements = (T *)realloc(elements, sizeof(T) * newLength);
    length = newLength;
  }
  T pop()
  {
    if (index > 0)
        return this->elements[--index];
    else
    return T();
  }
};
#endif
/*************************stack.cpp for boost.python(bjam need it to build a
stack.so file):****************/
// Boost Includes
==============================================================
#include <boost/python.hpp>
#include <boost/cstdint.hpp>
// Includes
====================================================================
#include "stack.h"
// Using
=======================================================================
using namespace boost::python;
// Declarations
================================================================
BOOST_PYTHON_OPAQUE_SPECIALIZED_TYPE_ID(float)
// Module
======================================================================
BOOST_PYTHON_MODULE(stack)
{
    class_< v_array<float*> >("array_floatstar", init<  >())
        .def(init< const v_array<float*>& >())
        .def_readwrite("index", &v_array<float*>::index)
        .def_readwrite("length", &v_array<float*>::length)
        .def_readwrite("elements", &v_array<float*>::elements)
        .def("last", &v_array<float*>::last, return_value_policy<
return_opaque_pointer >())
        .def("decr", &v_array<float*>::decr)
        .def("push", &v_array<float*>::push)
        .def("alloc", &v_array<float*>::alloc)
        .def("pop", &v_array<float*>::pop, return_value_policy<
return_opaque_pointer >())
    ;
}
-- Qinfeng(Javen) Shi Research School of Information Sciences and Engineering Australian National University Locked Bag 8001 Canberra ACT 2601