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. Increasing usage for C++ new operators based on data model indexes?

Increasing usage for C++ new operators based on data model indexes?

Scheduled Pinned Locked Moved Unsolved General and Desktop
data modelscreateindexallocationnew operatorssoftware design
116 Posts 6 Posters 51.4k 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.
  • V VRonin
    15 Oct 2018, 13:24

    @elfring said in Increasing usage for C++ new operators based on data model indexes?:

    Only if you do not use the construct “placement new”.

    Is your plan to do exactly what dynamic_cast does but using a new operator? I mean it's possible but I don't see why you'd want to do it. How would you implement it?

    guess that the run time consequences are more interesting then for the distinction if only pointers are transferred or complete “value objects” are copied.
    There are software applications where more data transfers might not have a directly noticeable effect. But I guess that some C++ programmers have got a strong focus on software efficiency.

    @VRonin said in Increasing usage for C++ new operators based on data model indexes?:

    having your objects implicitly shared

    With implicitly shared objects, the distinction from a pointer to a complete value object when copying is just an operator++ on an int. 1 atomic instruction. 100000 times more efficient than a dynamic_cast.
    If you want to go into memory difference it's 32bits more. If 32 bits break your design then it's the design itself that comes into question

    E Offline
    E Offline
    elfring
    wrote on 15 Oct 2018, 13:32 last edited by
    #28

    Is your plan to do exactly what dynamic_cast does but using a new operator?

    Maybe.

    But these C++ operations provide different functionality, don't they?

    With implicitly shared objects, the distinction from a pointer to a complete value object when copying is just an operator++ on an int.

    Will it become nicer to avoid (or reduce) even the influence of object reference counting?

    V 1 Reply Last reply 15 Oct 2018, 14:39
    0
    • E elfring
      15 Oct 2018, 13:32

      Is your plan to do exactly what dynamic_cast does but using a new operator?

      Maybe.

      But these C++ operations provide different functionality, don't they?

      With implicitly shared objects, the distinction from a pointer to a complete value object when copying is just an operator++ on an int.

      Will it become nicer to avoid (or reduce) even the influence of object reference counting?

      V Offline
      V Offline
      VRonin
      wrote on 15 Oct 2018, 14:39 last edited by
      #29

      @elfring said in Increasing usage for C++ new operators based on data model indexes?:

      Will it become nicer to avoid (or reduce) even the influence of object reference counting?

      C++ is moving in the opposite direction actually. If you try to suggest replacing std::shared_ptr (which is a pointer + a reference counter) with raw pointers on Stack Overflow you'd better bring a helmet and kevlar vest because you are going to get shot.
      The ISO C++ style guide goes one step further and even discourages the use of owning raw pointer and suggest ownership encapsulation (an implicitly shared object behaves as a smart pointer). If you don't like it, good luck convincing Herb Sutter and Bjarne Stroustrup himself.

      But these C++ operations provide different functionality, don't they?

      Yes but so far you did not highlight any need for custom allocation, your problems all seem to come from casting.

      "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 15 Oct 2018, 16:08
      2
      • V VRonin
        15 Oct 2018, 14:39

        @elfring said in Increasing usage for C++ new operators based on data model indexes?:

        Will it become nicer to avoid (or reduce) even the influence of object reference counting?

        C++ is moving in the opposite direction actually. If you try to suggest replacing std::shared_ptr (which is a pointer + a reference counter) with raw pointers on Stack Overflow you'd better bring a helmet and kevlar vest because you are going to get shot.
        The ISO C++ style guide goes one step further and even discourages the use of owning raw pointer and suggest ownership encapsulation (an implicitly shared object behaves as a smart pointer). If you don't like it, good luck convincing Herb Sutter and Bjarne Stroustrup himself.

        But these C++ operations provide different functionality, don't they?

        Yes but so far you did not highlight any need for custom allocation, your problems all seem to come from casting.

        E Offline
        E Offline
        elfring
        wrote on 15 Oct 2018, 16:08 last edited by
        #30

        but so far you did not highlight any need for custom allocation,

        I suggest to reconsider the software situation once more.

        A pair of row and column values (data model index, address or coordinate) can be passed as a parameter to a C++ new operator.
        You can choose then if you need to work with fresh objects (using dynamic memory allocation) or would like to reuse existing data.

        Is the usage of the construct “placement new” a kind of customised operation?

        your problems all seem to come from casting.

        I hope that specific software development challenges can be adjusted when the new operator call takes care of casting to the desired target (pointer) data type already.

        V 1 Reply Last reply 15 Oct 2018, 17:09
        0
        • E elfring
          15 Oct 2018, 16:08

          but so far you did not highlight any need for custom allocation,

          I suggest to reconsider the software situation once more.

          A pair of row and column values (data model index, address or coordinate) can be passed as a parameter to a C++ new operator.
          You can choose then if you need to work with fresh objects (using dynamic memory allocation) or would like to reuse existing data.

          Is the usage of the construct “placement new” a kind of customised operation?

          your problems all seem to come from casting.

          I hope that specific software development challenges can be adjusted when the new operator call takes care of casting to the desired target (pointer) data type already.

          V Offline
          V Offline
          VRonin
          wrote on 15 Oct 2018, 17:09 last edited by
          #31

          @elfring said in Increasing usage for C++ new operators based on data model indexes?:

          A pair of row and column values (data model index, address or coordinate) can be passed as a parameter to a C++ new operator.
          You can choose then if you need to work with fresh objects (using dynamic memory allocation) or would like to reuse existing data.
          Is the usage of the construct “placement new” a kind of customised operation?

          Once again, the model implementation might or might not allocate data. This should not be a problem of the API user.

          Could you please provide a concrete example of what you are suggesting?
          Otherwise we are just discussing of theory and nobody is gaining any value out of it.
          I will not continue the discussion until you provide at least a code snippet

          "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 2 Replies Last reply 15 Oct 2018, 17:27
          3
          • V VRonin
            15 Oct 2018, 17:09

            @elfring said in Increasing usage for C++ new operators based on data model indexes?:

            A pair of row and column values (data model index, address or coordinate) can be passed as a parameter to a C++ new operator.
            You can choose then if you need to work with fresh objects (using dynamic memory allocation) or would like to reuse existing data.
            Is the usage of the construct “placement new” a kind of customised operation?

            Once again, the model implementation might or might not allocate data. This should not be a problem of the API user.

            Could you please provide a concrete example of what you are suggesting?
            Otherwise we are just discussing of theory and nobody is gaining any value out of it.
            I will not continue the discussion until you provide at least a code snippet

            E Offline
            E Offline
            elfring
            wrote on 15 Oct 2018, 17:27 last edited by
            #32

            Could you please provide a concrete example of what you are suggesting?

            Does the software situation become really more challenging for passing a few extra parameters to a (member) function when it is “accidentally” (or intentionally) called “new”?

            1 Reply Last reply
            0
            • V VRonin
              15 Oct 2018, 17:09

              @elfring said in Increasing usage for C++ new operators based on data model indexes?:

              A pair of row and column values (data model index, address or coordinate) can be passed as a parameter to a C++ new operator.
              You can choose then if you need to work with fresh objects (using dynamic memory allocation) or would like to reuse existing data.
              Is the usage of the construct “placement new” a kind of customised operation?

              Once again, the model implementation might or might not allocate data. This should not be a problem of the API user.

              Could you please provide a concrete example of what you are suggesting?
              Otherwise we are just discussing of theory and nobody is gaining any value out of it.
              I will not continue the discussion until you provide at least a code snippet

              E Offline
              E Offline
              elfring
              wrote on 15 Oct 2018, 19:55 last edited by
              #33

              … concrete example of what you are suggesting?

              • https://isocpp.org/wiki/faq/dtors#placement-new
              • https://en.wikipedia.org/wiki/Placement_syntax
              • https://stackoverflow.com/questions/222557/what-uses-are-there-for-placement-new
              • https://www.geeksforgeeks.org/placement-new-operator-cpp/
              • http://blog.aaronballman.com/2011/08/the-placement-new-operator/
              • https://thispointer.com/whats-placement-new-operator-and-why-do-we-need-it/
              • https://eli.thegreenplace.net/2011/02/17/the-many-faces-of-operator-new-in-c
              • https://archive.org/details/TICPP2ndEdVolOne
              1 Reply Last reply
              0
              • S Offline
                S Offline
                sierdzio
                Moderators
                wrote on 16 Oct 2018, 05:45 last edited by
                #34

                The first link clearly states:

                ADVICE: Don’t use this “placement new” syntax unless you have to. Use it only when you really care that an object is placed at a particular location in memory. For example, when your hardware has a memory-mapped I/O timer device, and you want to place a Clock object at that memory location.

                In case of model indexes, there seems to be no need to use placement new.

                (Z(:^

                E 1 Reply Last reply 16 Oct 2018, 08:43
                5
                • S sierdzio
                  16 Oct 2018, 05:45

                  The first link clearly states:

                  ADVICE: Don’t use this “placement new” syntax unless you have to. Use it only when you really care that an object is placed at a particular location in memory. For example, when your hardware has a memory-mapped I/O timer device, and you want to place a Clock object at that memory location.

                  In case of model indexes, there seems to be no need to use placement new.

                  E Offline
                  E Offline
                  elfring
                  wrote on 16 Oct 2018, 08:43 last edited by
                  #35

                  In case of model indexes, there seems to be no need to use placement new.

                  • Do you care if customised model data were “placed at a particular location in memory”?
                  • How do you think about to get direct access to object data which are managed by a class like QVariant?
                  S 1 Reply Last reply 16 Oct 2018, 08:56
                  0
                  • E elfring
                    16 Oct 2018, 08:43

                    In case of model indexes, there seems to be no need to use placement new.

                    • Do you care if customised model data were “placed at a particular location in memory”?
                    • How do you think about to get direct access to object data which are managed by a class like QVariant?
                    S Offline
                    S Offline
                    sierdzio
                    Moderators
                    wrote on 16 Oct 2018, 08:56 last edited by
                    #36

                    @elfring said in Increasing usage for C++ new operators based on data model indexes?:

                    In case of model indexes, there seems to be no need to use placement new.

                    • Do you care if customised model data were “placed at a particular location in memory”?

                    No I don't.

                    • How do you think about to get direct access to object data which are managed by a class like QVariant?

                    I don't understand the question. Accessing data of a QVariant is possible and trivial, if necessary.

                    (Z(:^

                    E 1 Reply Last reply 16 Oct 2018, 09:03
                    4
                    • S sierdzio
                      16 Oct 2018, 08:56

                      @elfring said in Increasing usage for C++ new operators based on data model indexes?:

                      In case of model indexes, there seems to be no need to use placement new.

                      • Do you care if customised model data were “placed at a particular location in memory”?

                      No I don't.

                      • How do you think about to get direct access to object data which are managed by a class like QVariant?

                      I don't understand the question. Accessing data of a QVariant is possible and trivial, if necessary.

                      E Offline
                      E Offline
                      elfring
                      wrote on 16 Oct 2018, 09:03 last edited by
                      #37

                      Accessing data of a QVariant is possible and trivial, if necessary.

                      Can the data access become more convenient (and safe) for customised data types?

                      V 1 Reply Last reply 16 Oct 2018, 09:53
                      0
                      • E elfring
                        16 Oct 2018, 09:03

                        Accessing data of a QVariant is possible and trivial, if necessary.

                        Can the data access become more convenient (and safe) for customised data types?

                        V Offline
                        V Offline
                        VRonin
                        wrote on 16 Oct 2018, 09:53 last edited by
                        #38

                        @elfring said in Increasing usage for C++ new operators based on data model indexes?:

                        Can the data access become more convenient (and safe) for customised data types?

                        No.
                        get->change->set is mildly less convenient but infinitely safer.

                        Can you think of an example code, and paste it below, where it would?

                        "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 16 Oct 2018, 10:44
                        1
                        • V VRonin
                          16 Oct 2018, 09:53

                          @elfring said in Increasing usage for C++ new operators based on data model indexes?:

                          Can the data access become more convenient (and safe) for customised data types?

                          No.
                          get->change->set is mildly less convenient but infinitely safer.

                          Can you think of an example code, and paste it below, where it would?

                          E Offline
                          E Offline
                          elfring
                          wrote on 16 Oct 2018, 10:44 last edited by
                          #39

                          Can you think of an example code, and paste it below, where it would?

                          I suggest to take another look at a specific implementation detail: How many function calls do you need finally to get access to a member variable within a customised data model so far?

                          V 1 Reply Last reply 16 Oct 2018, 12:03
                          0
                          • E elfring
                            16 Oct 2018, 10:44

                            Can you think of an example code, and paste it below, where it would?

                            I suggest to take another look at a specific implementation detail: How many function calls do you need finally to get access to a member variable within a customised data model so far?

                            V Offline
                            V Offline
                            VRonin
                            wrote on 16 Oct 2018, 12:03 last edited by VRonin
                            #40

                            @elfring said in Increasing usage for C++ new operators based on data model indexes?:

                            How many function calls do you need finally to get access to a member variable within a customised data model so far?

                            2 chained. QModelIndex::data and QVariant::value<T>. e.g.: index.data().value<QString>();. Don't think you can do better.
                            Can you show us how would your new implementation look? Call this a test. You haven't wrote a single line of code in all your posts. I want to check that at least you have a rough idea of what you are talking about

                            "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 16 Oct 2018, 13:09
                            4
                            • V VRonin
                              16 Oct 2018, 12:03

                              @elfring said in Increasing usage for C++ new operators based on data model indexes?:

                              How many function calls do you need finally to get access to a member variable within a customised data model so far?

                              2 chained. QModelIndex::data and QVariant::value<T>. e.g.: index.data().value<QString>();. Don't think you can do better.
                              Can you show us how would your new implementation look? Call this a test. You haven't wrote a single line of code in all your posts. I want to check that at least you have a rough idea of what you are talking about

                              E Offline
                              E Offline
                              elfring
                              wrote on 16 Oct 2018, 13:09 last edited by
                              #41

                              Don't think you can do better.

                              I imagine that a single function call will be a bit nicer. It can be that it will still need to combine the other mentioned functions.
                              (Reminder: The usage of the function “QVariant::value” is “unsafe” for non-core data types so far, isn't it?)

                              You haven't wrote a single line of code in all your posts.

                              • I find this information partly inappropriate because I contributed a (questionable) test case in this forum.
                              • Source code might distract from the really relevant software development ideas, doesn't it?

                              Can you show us how would your new implementation look?

                              Did other information sources show in sufficient ways already how such a function can be written?

                              V 1 Reply Last reply 16 Oct 2018, 13:42
                              0
                              • E elfring
                                16 Oct 2018, 13:09

                                Don't think you can do better.

                                I imagine that a single function call will be a bit nicer. It can be that it will still need to combine the other mentioned functions.
                                (Reminder: The usage of the function “QVariant::value” is “unsafe” for non-core data types so far, isn't it?)

                                You haven't wrote a single line of code in all your posts.

                                • I find this information partly inappropriate because I contributed a (questionable) test case in this forum.
                                • Source code might distract from the really relevant software development ideas, doesn't it?

                                Can you show us how would your new implementation look?

                                Did other information sources show in sufficient ways already how such a function can be written?

                                V Offline
                                V Offline
                                VRonin
                                wrote on 16 Oct 2018, 13:42 last edited by
                                #42

                                @elfring said in Increasing usage for C++ new operators based on data model indexes?:

                                I find this information partly inappropriate because I contributed a (questionable) test case in this forum.

                                This is a great starting point. Let's assume the overloaded new existed. How would you use it in your example?

                                Source code might distract from the really relevant software development ideas, doesn't it?

                                No, it really helps focusing on the problem, what really matters and what is overhead.

                                "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 16 Oct 2018, 14:15
                                1
                                • V VRonin
                                  16 Oct 2018, 13:42

                                  @elfring said in Increasing usage for C++ new operators based on data model indexes?:

                                  I find this information partly inappropriate because I contributed a (questionable) test case in this forum.

                                  This is a great starting point. Let's assume the overloaded new existed. How would you use it in your example?

                                  Source code might distract from the really relevant software development ideas, doesn't it?

                                  No, it really helps focusing on the problem, what really matters and what is overhead.

                                  E Offline
                                  E Offline
                                  elfring
                                  wrote on 16 Oct 2018, 14:15 last edited by
                                  #43

                                  How would you use it in your example?

                                  I would not use extra statements in the implementation of the constructor from the class “my_views” because this test case tried to check other details.

                                  No, it really helps focusing on the problem, what really matters and what is overhead.

                                  I prefer an other clarification approach.

                                  Which names can you imagine for functions which determine a pointer data type for a model index?

                                  V 1 Reply Last reply 16 Oct 2018, 14:23
                                  0
                                  • E elfring
                                    16 Oct 2018, 14:15

                                    How would you use it in your example?

                                    I would not use extra statements in the implementation of the constructor from the class “my_views” because this test case tried to check other details.

                                    No, it really helps focusing on the problem, what really matters and what is overhead.

                                    I prefer an other clarification approach.

                                    Which names can you imagine for functions which determine a pointer data type for a model index?

                                    V Offline
                                    V Offline
                                    VRonin
                                    wrote on 16 Oct 2018, 14:23 last edited by
                                    #44

                                    @elfring said in Increasing usage for C++ new operators based on data model indexes?:

                                    Which names can you imagine for functions which determine a pointer data type for a model index?

                                    userType() as already used

                                    To clarify a single index can contain multiple data points of non-homogeneous data type

                                    "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 16 Oct 2018, 14:48
                                    0
                                    • V VRonin
                                      16 Oct 2018, 14:23

                                      @elfring said in Increasing usage for C++ new operators based on data model indexes?:

                                      Which names can you imagine for functions which determine a pointer data type for a model index?

                                      userType() as already used

                                      To clarify a single index can contain multiple data points of non-homogeneous data type

                                      E Offline
                                      E Offline
                                      elfring
                                      wrote on 16 Oct 2018, 14:48 last edited by
                                      #45

                                      userType() as already used

                                      This function returns the data type “int”.
                                      What would you like to say for pointers within data models?

                                      To clarify a single index can contain multiple data points of non-homogeneous data type

                                      This information seems to point an other software development concern out.
                                      Would you like to introduce another case distinction?

                                      V 1 Reply Last reply 17 Oct 2018, 07:13
                                      0
                                      • E elfring
                                        16 Oct 2018, 14:48

                                        userType() as already used

                                        This function returns the data type “int”.
                                        What would you like to say for pointers within data models?

                                        To clarify a single index can contain multiple data points of non-homogeneous data type

                                        This information seems to point an other software development concern out.
                                        Would you like to introduce another case distinction?

                                        V Offline
                                        V Offline
                                        VRonin
                                        wrote on 17 Oct 2018, 07:13 last edited by
                                        #46

                                        @elfring said in Increasing usage for C++ new operators based on data model indexes?:

                                        What would you like to say for pointers within data models?

                                        While my point is that you shouldn't use pointers as data because it create unclear ownership of that piece of memory, this constructor allows you to specify the usertype you want that pointer to be identified by

                                        @elfring said in Increasing usage for C++ new operators based on data model indexes?:

                                        This function returns the data type “int”.

                                        In C or C++ there's no alternative. You can't have a function that changes return type based on its body. The only way to downcast safely is to keep a track of what type you want to return (here an integer that represent the return value of qMetaTypeId<T>()).

                                        This information seems to point an other software development concern out.
                                        Would you like to introduce another case distinction?

                                        There's nothing new to introduce, different arguments to QModelIndex::data return different data roles.

                                        "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 17 Oct 2018, 07:55
                                        2
                                        • V VRonin
                                          17 Oct 2018, 07:13

                                          @elfring said in Increasing usage for C++ new operators based on data model indexes?:

                                          What would you like to say for pointers within data models?

                                          While my point is that you shouldn't use pointers as data because it create unclear ownership of that piece of memory, this constructor allows you to specify the usertype you want that pointer to be identified by

                                          @elfring said in Increasing usage for C++ new operators based on data model indexes?:

                                          This function returns the data type “int”.

                                          In C or C++ there's no alternative. You can't have a function that changes return type based on its body. The only way to downcast safely is to keep a track of what type you want to return (here an integer that represent the return value of qMetaTypeId<T>()).

                                          This information seems to point an other software development concern out.
                                          Would you like to introduce another case distinction?

                                          There's nothing new to introduce, different arguments to QModelIndex::data return different data roles.

                                          E Offline
                                          E Offline
                                          elfring
                                          wrote on 17 Oct 2018, 07:55 last edited by
                                          #47

                                          While my point is that you shouldn't use pointers as data because it create unclear ownership of that piece of memory, …

                                          This is one way of thinking around the strict usage of value objects while I would prefer to avoid unnecessary data transfers as much as possible.
                                          The ownership information can be managed also by other means, can't it?

                                          The only way to downcast safely is to keep a track of what type you want to return …

                                          I imagine that additional software design options can be relevant here.

                                          …, different arguments to QModelIndex::data return different data roles.

                                          • “Data copies” are returned for each possible role.
                                          • Can any function provide a reference (and not a pointer) for a specific object within this data model?
                                          S VRoninV 2 Replies Last reply 17 Oct 2018, 08:00
                                          0

                                          37/116

                                          16 Oct 2018, 09:03

                                          • Login

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