3 Replies - 8870 Views - Last Post: 18 December 2012 - 05:25 PM Rate Topic: -----

#1 friday13  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 24-July 12

Wrapping/exposing c++ array using boost::python

Posted 17 December 2012 - 05:06 PM

I am working with some legacy c++ classes, and they include a struct which I can't alter.
I need to wrap them with boost::python and then run python unittests.
I am new to python and the boost::python libraries.
I'm running this in a cygwin environment on windows7, using python 2.6.8-2 and boost::python 1.48.

my difficulty is how to properly access the c++ array in python.

typedef struct {
    double values [15];
}my_struct_type;



i have wrapped the struct as follows using boost::python
#include <boost/python.hpp>
#include "my_struct_type.h"

BOOST_PYTHON_MODULE(_mystruct)
{
    class_< my_struct_type >("MyStructType")
        .def("d_values", &my_struct_type::values)
    ;
}



in my py file (mystruct.py), i have:
from array import *
from _mystruct import *



in my __init__.py file, i have:
__all__ = [  _mystruct  ]



for unit testing, i'm trying to access the array elements from the wrapped c++ struct.
i want to be able to either get, or set the array elements to the test values i've saved into a
python array.

import unittest
import random
import array as array

class StructTest(unittest.TestCase):
    def setUp(self):
        self.dvalues = array.array('d')
        for i in range(15):
            self.dvalues.append(random.random() * 100)

class TestConstructor(StructTest):
    def runTest(self):
        temp = mystruct.MyStructType()   
        for index in range(15):            
            #i get an error here
            x = temp.d_values[index]



when I try to access the wrapped c++ array elements, i get the following error:

     x = temp.d_values[index]
TypeError: No to_python (by-value) converter found for C++ type: double [15]



i'm hoping someone can shed some light on this as i'm really stuck.

Is This A Good Question/Topic? 0
  • +

Replies To: Wrapping/exposing c++ array using boost::python

#2 AKMafia001  Icon User is offline

  • </code.in.dream>

Reputation: 187
  • View blog
  • Posts: 624
  • Joined: 11-June 11

Re: Wrapping/exposing c++ array using boost::python

Posted 18 December 2012 - 12:12 PM

Perhaps this might help...
Was This Post Helpful? 0
  • +
  • -

#3 friday13  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 24-July 12

Re: Wrapping/exposing c++ array using boost::python

Posted 18 December 2012 - 05:10 PM

I'm not sure how to implement the suggestions found there (the numpy idea). Also, I cannot make any modifications to the struct itself. I am at a loss to come up with the proper syntax to do this.
Was This Post Helpful? 0
  • +
  • -

#4 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1353
  • View blog
  • Posts: 4,660
  • Joined: 19-February 09

Re: Wrapping/exposing c++ array using boost::python

Posted 18 December 2012 - 05:25 PM

I've had a look but not sure how to define the array yet.

I found this program though, might or might not help.

C++ Python language bindings


Does def_readwrite do anything?

 .def_readwrite("d_values", &my_struct_type::values)


This post has been edited by #define: 18 December 2012 - 08:39 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1