How to use map_indexing_suite from Boost.Python with custom not std object?

Viewed 397

Simple example of 'map_indexing_suite' usage is working fine:

class_<map<int, string> >("testMap")
        .def(map_indexing_suite<std::map<int, string>, true>())
    ;

and on python side it works as expected:

a = riversim.testMap()
a[1]='sdf'

But! If I try to use more complicated object BoundaryCondition instead of string in map, for example next one:

enum t_boundary 
{
    DIRICHLET = 0, 
    NEUMAN
}; 

struct BoundaryCondition
{
    t_boundary type = DIRICHLET;
    double value = 0;

    bool operator==(const BoundaryCondition& bc) const;
    friend ostream& operator <<(ostream& write, const BoundaryCondition & boundary_condition);
};

typedef map<t_boundary_id, BoundaryCondition> t_BoundaryConditions;

BOOST_PYTHON_MODULE(riversim)
{
    enum_<t_boundary>("t_boundary")
        .value("DIRICHLET", DIRICHLET)
        .value("NEUMAN", NEUMAN)
        .export_values()
        ;

    class_<BoundaryCondition>("BoundaryCondition")
        .def_readwrite("value", &BoundaryCondition::value)
        .def_readwrite("type", &BoundaryCondition::type)
        .def(self == self) 
    ;

    //and problematic class:
    class_<River::t_BoundaryConditions >("t_BoundaryConditions")
        .def(map_indexing_suite<t_BoundaryConditions, true>())
    ;

}

it compiles fine, and I can import riversim:

import riversim

bound = riversim.t_boundary
bc = riversim.BoundaryCondition
bcs = riversim.t_BoundaryConditions

and next command:

bcs[1] = bc

gives error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
 in 
----> 1 bcs[1] = bc

TypeError: 'Boost.Python.class' object does not support item assignment

How to correctly port map to python?

ps: also triend this solution link and it didn't worked, got same error as above.

1 Answers

First off,

bcs = riversim.t_BoundaryConditions

Assings the class. It needs to be

bcs = riversim.t_BoundaryConditions()

Now

bcs[1] = bc

Results in

Traceback (most recent call last):
  File "./test.py", line 10, in <module>
    bcs[1] = bc
TypeError: Invalid assignment

That's because the same applied to bc, both the following work:

bcs[1] = bc()
bcs[2] = riversim.BoundaryCondition()
Related