[C++-sig] How to deal with array?

Ralf W. Grosse-Kunstleve rwgk at yahoo.com
Fri Jul 1 09:01:32 CEST 2005


There are a number of possible solutions. One that I explored recently and is
particularly non-intrusive goes like this:

1. Copy this file to your space (remove the #include <gltbx/...> if it is still
version 1.1.1.1) and change the namespaces to your liking:

 
http://cvs.sourceforge.net/viewcvs.py/cctbx/gltbx/pointer_args_bpl.h?view=markup

2. Implement a thin wrapper like this (untested):

  void
  A_set(A& self, boost::python::object const& py_n)
  {
    boost_python::converter<int> n_proxy("n", py_n, 4, false);
    int* n = n_proxy.get();
    A.set(n);
  }

3. Wrap like this (also untested):

  class_<A>("A")
    .def("set", A_set)
  ;

HTH,
        Ralf

--- Clark <foo.Clark at gmail.com> wrote:

> Hi,
> Can I expose a C++ array to python with Boost.Python? And Can I use a
> python list conveniently in C++? 
> 
> For example, there is a C++ class as follows
>     class A
>     {
>     private:
>         int c[4];
>     public:
>         int d[10];
> 
>         void set(int *n)
>         {
>             for(int i = 0; i < 4; ++i)
>                 c[i] = n[i]
>         }
>     }
> and I want use class A above in Python like this
>     a = A()
>     print a.d[2]
>     n = [1, 2, ,3, 4]
>     a.set(n)
> 
> Can you give me some hints?


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 



More information about the Cplusplus-sig mailing list