如何将 Boost.Python 中的 map_indexing_suite 与自定义非标准对象一起使用?

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

'map_indexing_suite' 用法的简单示例工作正常:

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

在 python 方面它按预期工作:

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

但是! 如果我尝试在地图中使用更复杂的对象 BoundaryCondition 而不是 string,例如下一个:

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>())
    ;

}

它编译得很好,我可以导入 riversim:

import riversim

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

和下一个命令:

bcs[1] = bc

给出错误:

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

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

如何正确地将地图移植到python?

ps: 也试用了这个解决方案 link 但它没有用,得到与上面相同的错误。

首先,

bcs = riversim.t_BoundaryConditions

评估 class。必须

bcs = riversim.t_BoundaryConditions()

现在

bcs[1] = bc

结果

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

那是因为同样适用于 bc,以下两个工作:

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