Menu

Emulation of properties for composite objects

Sergey
2011-10-01
2013-04-06
  • Sergey

    Sergey - 2011-10-01

    Hi, Florian!

    I'm working on a scripting interface for the object representation of XML documents. In this regard, there is a need to emulate the properties of objects, whose members are other objects (composite objects), for example:

    class MyElem {
    public:
        QString attr2;
        MyElem ();
    };
    class MyDoc {
    public:
        QString attr1;
        MyElem elem;
        MyDoc ();
    };
    

    In a Python script to access a member of a nested object should be as follows:

    py> q = MyDoc ()
    py> q.elem.attr2 = "value"
    py> print q.elem.attr2

    Class decorators, obviously, must be determined as follows:

    class MyElemWrapper: public QObject {
        Q_OBJECT
    public slots:
        / / Constructor & destructor
        MyElem * new_MyElem () {return new MyElem ();}
        void delete_MyElem (MyElem * o) {delete o;}
        QString py_get_attr2 (MyElem * o) {return o-> attr2;}
        void py_set_attr2 (MyElem * o, QString value) {o-> attr2 = value;}
    };
    class MyDocWrapper: public QObject {
        Q_OBJECT
    public slots:
        / / Constructor & destructor
        MyDoc * new_MyDoc () {return new MyDoc ();}
        void delete_MyDoc (MyDoc * o) {delete o;}
        QString py_get_attr1 (MyDoc * o) {return o-> attr1;}
        void py_set_attr1 (MyDoc * o, QString value) {o-> attr1 = value;}
        MyElem py_get_elem (MyDoc * o) {return o-> elem;}
        void py_set_elem (MyDoc * o, MyElem value) {o-> elem = value;}
    };
    

    However, since the method MyDocWrapper::py_get_elem () returns a value (not a reference), the assignment is performed for a temporary object of MyElem. Therefore, the result of the print statement in the above script example is an empty string.
    Reading of attr2 member is performed correctly (understand why).

    Question: is there a way to ensure the desired behavior of the properties for the member elem?

    Sincerely,
    Sergey.

     
  • Florian Link

    Florian Link - 2011-10-02

    Sounds like you should do:

    MyElem* py_get_elem (MyDoc * o) {return &(o-> elem);}

    instead, so that you return the exact MyElem object to Python, instead of a copy.

     
  • Sergey

    Sergey - 2011-10-02

    I am ashamed to admit, I tried only the reference and got an error during script execution.
    Of course, one could assume that in such a wonderful library there should not be similar drawbacks. Thanks and successes!

    Sincerely,
    Sergey.

     
  • Florian Link

    Florian Link - 2011-10-02

    Thanks!

    The problem with references is that Qt's moc compiler doesn't like them as return values on slots, neither const & nor &,
    so one has to use pointers instead.

    Regards,
    Florian

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.