Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Returning C++ references from more programming interfaces?

Returning C++ references from more programming interfaces?

Scheduled Pinned Locked Moved Unsolved General and Desktop
qstandarditemqvectorreferencesapisoftware design
27 Posts 4 Posters 7.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E elfring
    24 Oct 2018, 11:45

    I can't think of how but I'm happy to see an example implementation of what would work

    I am curious on how the corresponding change acceptance will evolve for possible extensions around the class “QStandardItem”.

    J Offline
    J Offline
    jsulm
    Lifetime Qt Champion
    wrote on 24 Oct 2018, 11:49 last edited by
    #13

    @elfring I think @VRonin asked for an example, you do not have to submit a patch to Qt

    https://forum.qt.io/topic/113070/qt-code-of-conduct

    E 1 Reply Last reply 24 Oct 2018, 12:00
    2
    • J jsulm
      24 Oct 2018, 11:49

      @elfring I think @VRonin asked for an example, you do not have to submit a patch to Qt

      E Offline
      E Offline
      elfring
      wrote on 24 Oct 2018, 12:00 last edited by
      #14

      … , you do not have to submit a patch to Qt

      • Can the work with elements from a QStandardItem object become as convenient as the access for simple arrays?
      • Are you used to the programming with template functions and overloaded operators?
      1 Reply Last reply
      0
      • V Offline
        V Offline
        VRonin
        wrote on 24 Oct 2018, 12:00 last edited by VRonin
        #15

        To put it into prospective, let's assume we have something like this and let's assume int is not super cheap to copy, just for argument sake:

        class Example : public QObject{
            Q_OBJECT
        public:
            explicit Example(QObject* parent = nullptr) 
                : QObject(parent), m_value(0)
            {}
            const int& value() const {return m_value;}
            void setValue(const int& val){
                if(m_value==val)
                    return;
                m_value=val;
                valueChanged(m_value);
            }
        signals:
            void valueChanged(int val);
        private:
            int m_value;
        };
        

        How would you structure something like int& value() {return m_value;} that assures you the signal valueChanged is emitted in case the reference is changed?

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        E V 2 Replies Last reply 24 Oct 2018, 12:38
        1
        • V VRonin
          24 Oct 2018, 12:00

          To put it into prospective, let's assume we have something like this and let's assume int is not super cheap to copy, just for argument sake:

          class Example : public QObject{
              Q_OBJECT
          public:
              explicit Example(QObject* parent = nullptr) 
                  : QObject(parent), m_value(0)
              {}
              const int& value() const {return m_value;}
              void setValue(const int& val){
                  if(m_value==val)
                      return;
                  m_value=val;
                  valueChanged(m_value);
              }
          signals:
              void valueChanged(int val);
          private:
              int m_value;
          };
          

          How would you structure something like int& value() {return m_value;} that assures you the signal valueChanged is emitted in case the reference is changed?

          E Offline
          E Offline
          elfring
          wrote on 24 Oct 2018, 12:38 last edited by
          #16

          How would you structure something like …

          I would expect that the member variable will be changed only if no C++ exceptions were thrown.
          Thus I would interpret the following approach as an useful software design option.

          void modify(my_data const & md)
          {
          m_value = md;
          emit valueChanged();
          }
          

          Would you like to extend such an example with function objects or the application of lambdas?

          1 Reply Last reply
          0
          • V Offline
            V Offline
            VRonin
            wrote on 24 Oct 2018, 13:03 last edited by VRonin
            #17

            m_value = md;

            So what's the advantage of using a reference if you still call the assignment operator (that would trigger a copy)? and what's the difference between your modify and my setValue?

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            E 1 Reply Last reply 24 Oct 2018, 14:42
            0
            • V VRonin
              24 Oct 2018, 13:03

              m_value = md;

              So what's the advantage of using a reference if you still call the assignment operator (that would trigger a copy)? and what's the difference between your modify and my setValue?

              E Offline
              E Offline
              elfring
              wrote on 24 Oct 2018, 14:42 last edited by
              #18

              … and what's the difference between your modify and my setValue?

              I omitted an equality check in my example.
              But I guess that this implementation detail distracts from the original issue of my feature request here.

              Would you like to adjust programming interfaces around container class variants any further?

              1 Reply Last reply
              0
              • V VRonin
                24 Oct 2018, 12:00

                To put it into prospective, let's assume we have something like this and let's assume int is not super cheap to copy, just for argument sake:

                class Example : public QObject{
                    Q_OBJECT
                public:
                    explicit Example(QObject* parent = nullptr) 
                        : QObject(parent), m_value(0)
                    {}
                    const int& value() const {return m_value;}
                    void setValue(const int& val){
                        if(m_value==val)
                            return;
                        m_value=val;
                        valueChanged(m_value);
                    }
                signals:
                    void valueChanged(int val);
                private:
                    int m_value;
                };
                

                How would you structure something like int& value() {return m_value;} that assures you the signal valueChanged is emitted in case the reference is changed?

                V Offline
                V Offline
                VRonin
                wrote on 24 Oct 2018, 15:09 last edited by
                #19

                You didn't answer

                @VRonin said in Returning C++ references from more programming interfaces?:

                How would you structure something like int& value() {return m_value;} that assures you the signal valueChanged is emitted in case the reference is changed?

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                E 1 Reply Last reply 24 Oct 2018, 16:11
                0
                • V VRonin
                  24 Oct 2018, 15:09

                  You didn't answer

                  @VRonin said in Returning C++ references from more programming interfaces?:

                  How would you structure something like int& value() {return m_value;} that assures you the signal valueChanged is emitted in case the reference is changed?

                  E Offline
                  E Offline
                  elfring
                  wrote on 24 Oct 2018, 16:11 last edited by
                  #20

                  You didn't answer

                  I suggest to distinguish the update scope and the actor which should trigger the desired change notification (by a specific function call).
                  Another software design option would be the use of a corresponding class, wouldn't it?

                  Example demo1;
                  
                  struct notifier
                  {
                   Example& ex;
                   
                   notifier(Example& target, int input)
                   : ex(target)
                   { ex[0] = input; }
                   
                   ~notifier()
                   { ex.valueChanged(); }
                  } demo2(demo1, 123);
                  
                  J 1 Reply Last reply 24 Oct 2018, 22:40
                  0
                  • E elfring
                    24 Oct 2018, 16:11

                    You didn't answer

                    I suggest to distinguish the update scope and the actor which should trigger the desired change notification (by a specific function call).
                    Another software design option would be the use of a corresponding class, wouldn't it?

                    Example demo1;
                    
                    struct notifier
                    {
                     Example& ex;
                     
                     notifier(Example& target, int input)
                     : ex(target)
                     { ex[0] = input; }
                     
                     ~notifier()
                     { ex.valueChanged(); }
                    } demo2(demo1, 123);
                    
                    J Offline
                    J Offline
                    JKSH
                    Moderators
                    wrote on 24 Oct 2018, 22:40 last edited by JKSH
                    #21

                    @elfring, so you want programmers to replace Code 1 with Code 2; have I understood you correctly?

                    //======
                    // Code 1
                    //======
                    Example demo1;
                    demo1.setData(123); // Automatically emits valueChanged() immediately
                    
                    //======
                    // Code 2
                    //======
                    Example demo1;
                    notifier demo2(demo1, 123);
                    
                    // valueChanged() is emitted when demo2 is destroyed
                    

                    I must say that Code 1 is a much more elegant and intuitive than Code 2.

                    Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                    E 1 Reply Last reply 25 Oct 2018, 05:20
                    2
                    • J JKSH
                      24 Oct 2018, 22:40

                      @elfring, so you want programmers to replace Code 1 with Code 2; have I understood you correctly?

                      //======
                      // Code 1
                      //======
                      Example demo1;
                      demo1.setData(123); // Automatically emits valueChanged() immediately
                      
                      //======
                      // Code 2
                      //======
                      Example demo1;
                      notifier demo2(demo1, 123);
                      
                      // valueChanged() is emitted when demo2 is destroyed
                      

                      I must say that Code 1 is a much more elegant and intuitive than Code 2.

                      E Offline
                      E Offline
                      elfring
                      wrote on 25 Oct 2018, 05:20 last edited by
                      #22

                      so you want programmers to replace Code 1 with Code 2; …

                      Not really. - I suggest to choose between available software design options.

                      The standard behaviour of the function “QStandardItem::setData” is generally fine.
                      The software situaton might look different if more reference-returning functions from a container class like QVector will be taken into account.
                      A need can evolve to call the function “valueChanged” (or “dataChanged”) in a C++ destructor, can't it?

                      1 Reply Last reply
                      0
                      • J Offline
                        J Offline
                        JKSH
                        Moderators
                        wrote on 25 Oct 2018, 07:53 last edited by
                        #23

                        @elfring said in Returning C++ references from more programming interfaces?:

                        so you want programmers to replace Code 1 with Code 2; …

                        Not really. - I suggest to choose between available software design options.

                        OK.

                        The standard behaviour of the function “QStandardItem::setData” is generally fine.

                        I'm glad you think it's generally fine.

                        The software situaton might look different if more reference-returning functions from a container class like QVector will be taken into account.

                        I already explained above why QStandardItem must not provide a reference to is internal data. Do you understand that explanation?

                        A need can evolve to call the function “valueChanged” (or “dataChanged”) in a C++ destructor, can't it?

                        No, it can't. The signal should be emitted immediately when the data is changed. It should not wait for the destructor of another struct/object.

                        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                        E 1 Reply Last reply 25 Oct 2018, 08:32
                        2
                        • J JKSH
                          25 Oct 2018, 07:53

                          @elfring said in Returning C++ references from more programming interfaces?:

                          so you want programmers to replace Code 1 with Code 2; …

                          Not really. - I suggest to choose between available software design options.

                          OK.

                          The standard behaviour of the function “QStandardItem::setData” is generally fine.

                          I'm glad you think it's generally fine.

                          The software situaton might look different if more reference-returning functions from a container class like QVector will be taken into account.

                          I already explained above why QStandardItem must not provide a reference to is internal data. Do you understand that explanation?

                          A need can evolve to call the function “valueChanged” (or “dataChanged”) in a C++ destructor, can't it?

                          No, it can't. The signal should be emitted immediately when the data is changed. It should not wait for the destructor of another struct/object.

                          E Offline
                          E Offline
                          elfring
                          wrote on 25 Oct 2018, 08:32 last edited by
                          #24

                          Do you understand that explanation?

                          I can follow software development concerns (which were expressed here) to some degree.

                          The signal should be emitted immediately when the data is changed.

                          This expectation is also generally fine.

                          It should not wait for the destructor of another struct/object.

                          The available programming interfaces support to call desired functions in C++ destructors. You can choose under which circumstances such a software design approach will be appropriate.

                          J 1 Reply Last reply 25 Oct 2018, 23:12
                          0
                          • E elfring
                            25 Oct 2018, 08:32

                            Do you understand that explanation?

                            I can follow software development concerns (which were expressed here) to some degree.

                            The signal should be emitted immediately when the data is changed.

                            This expectation is also generally fine.

                            It should not wait for the destructor of another struct/object.

                            The available programming interfaces support to call desired functions in C++ destructors. You can choose under which circumstances such a software design approach will be appropriate.

                            J Offline
                            J Offline
                            JKSH
                            Moderators
                            wrote on 25 Oct 2018, 23:12 last edited by
                            #25

                            @elfring said in Returning C++ references from more programming interfaces?:

                            I can follow software development concerns (which were expressed here) to some degree.

                            That's good. So please address those concerns. For example, why should Qt provide extensions that break encapsulation and increase the risk of errors?

                            The signal should be emitted immediately when the data is changed.

                            This expectation is also generally fine.

                            Good.

                            It should not wait for the destructor of another struct/object.

                            The available programming interfaces support to call desired functions in C++ destructors. You can choose under which circumstances such a software design approach will be appropriate.

                            I cannot see any circumstance where such a software design approach will be appropriate.

                            Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                            E 1 Reply Last reply 26 Oct 2018, 09:11
                            0
                            • J JKSH
                              25 Oct 2018, 23:12

                              @elfring said in Returning C++ references from more programming interfaces?:

                              I can follow software development concerns (which were expressed here) to some degree.

                              That's good. So please address those concerns. For example, why should Qt provide extensions that break encapsulation and increase the risk of errors?

                              The signal should be emitted immediately when the data is changed.

                              This expectation is also generally fine.

                              Good.

                              It should not wait for the destructor of another struct/object.

                              The available programming interfaces support to call desired functions in C++ destructors. You can choose under which circumstances such a software design approach will be appropriate.

                              I cannot see any circumstance where such a software design approach will be appropriate.

                              E Offline
                              E Offline
                              elfring
                              wrote on 26 Oct 2018, 09:11 last edited by
                              #26

                              For example, why should Qt provide extensions that break encapsulation and increase the risk of errors?

                              I suggest to use algorithms which can work together with container classes at more source code places.

                              J 1 Reply Last reply 26 Oct 2018, 13:02
                              -1
                              • E elfring
                                26 Oct 2018, 09:11

                                For example, why should Qt provide extensions that break encapsulation and increase the risk of errors?

                                I suggest to use algorithms which can work together with container classes at more source code places.

                                J Offline
                                J Offline
                                JKSH
                                Moderators
                                wrote on 26 Oct 2018, 13:02 last edited by JKSH
                                #27

                                @elfring said in Returning C++ references from more programming interfaces?:

                                For example, why should Qt provide extensions that break encapsulation and increase the risk of errors?

                                I suggest to use algorithms which can work together with container classes at more source code places.

                                You did not address any of the concerns. You only added suggestions.

                                That is not acceptable. You must only submit ideas/proposals that don't break encapsulation.

                                Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                                1 Reply Last reply
                                3

                                22/27

                                25 Oct 2018, 05:20

                                • Login

                                • Login or register to search.
                                22 out of 27
                                • First post
                                  22/27
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • Users
                                • Groups
                                • Search
                                • Get Qt Extensions
                                • Unsolved