Python Properties & Swig

后端 未结 6 941
日久生厌
日久生厌 2021-01-30 09:24

I am attempting to create python bindings for some C++ code using swig. I seem have run into a problem trying to create python properties from some accessor functions I have for

6条回答
  •  独厮守ぢ
    2021-01-30 10:22

    From http://www.swig.org/Doc2.0/SWIGDocumentation.html#SWIG_adding_member_functions:

    A little known feature of the %extend directive is that it can also be used to add synthesized attributes or to modify the behavior of existing data attributes. For example, suppose you wanted to make magnitude a read-only attribute of Vector instead of a method.

    So in your example the following should work:

    %extend Player {
        Entity entity;
    }
    
    %{
    Entity* Player_entity_get(Player* p) {
      return p->get_entity();
    }
    void Player_entityProp_set(Player* p, Entity* e) {
      p->set_entity(e);
    }
    %}
    

提交回复
热议问题