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. What's the best way to loop over a vector that is a member of a vector of parent dummy objects also being looped over
Forum Updated to NodeBB v4.3 + New Features

What's the best way to loop over a vector that is a member of a vector of parent dummy objects also being looped over

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 222 Views 2 Watching
  • 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.
  • C Offline
    C Offline
    Crag_Hack
    wrote on last edited by Crag_Hack
    #1

    I want to loop over a vector of objects each containing another vector of objects to be looped over in turn. The subvector object members (subjobs) need to be able to be modified from the operation methods in the worker object and not copy constructed or implicitly shared. They are accessed from different worker methods and their state needs to persist in those different worker methods. See the code below for my current setup. The parent jobs are dummy objects that contain properties for the subjobs. I copy construct the subjobs from the parent dummy job. What's the best way to do this while following proper coding techniques? Thanks!

    class Worker : public QObject
    {
        public:
        void start();
        void startOperation1();
        void startOperation2();
    
        private:
        QVector<Job> jobs;
    }
    
    class Job
    {
        public:
        QVector<Job> getSubJobs() { return subJobs; }
    
        private:
        QVector<BackupJob> subJobs;
    }
    
    void Worker::start()
    {
        startOperation1();
        startOperation2();
    }
    
    void Worker::startOperation1()
    {
        for (Job& job : jobs)
            for (Job& subJob : job.getSubJobs())
                //do operation1 on each subjob here
    }
    
    void Worker::startOperation2()
    {
        for (Job& job : jobs)
            for (Job& subJob : job.getSubJobs())
                //do operation2 on each subjob here
    }
    
    C 1 Reply Last reply
    0
    • C Crag_Hack marked this topic as a regular topic on
    • C Crag_Hack

      I want to loop over a vector of objects each containing another vector of objects to be looped over in turn. The subvector object members (subjobs) need to be able to be modified from the operation methods in the worker object and not copy constructed or implicitly shared. They are accessed from different worker methods and their state needs to persist in those different worker methods. See the code below for my current setup. The parent jobs are dummy objects that contain properties for the subjobs. I copy construct the subjobs from the parent dummy job. What's the best way to do this while following proper coding techniques? Thanks!

      class Worker : public QObject
      {
          public:
          void start();
          void startOperation1();
          void startOperation2();
      
          private:
          QVector<Job> jobs;
      }
      
      class Job
      {
          public:
          QVector<Job> getSubJobs() { return subJobs; }
      
          private:
          QVector<BackupJob> subJobs;
      }
      
      void Worker::start()
      {
          startOperation1();
          startOperation2();
      }
      
      void Worker::startOperation1()
      {
          for (Job& job : jobs)
              for (Job& subJob : job.getSubJobs())
                  //do operation1 on each subjob here
      }
      
      void Worker::startOperation2()
      {
          for (Job& job : jobs)
              for (Job& subJob : job.getSubJobs())
                  //do operation2 on each subjob here
      }
      
      C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #2

      @Crag_Hack said in What's the best way to loop over a vector that is a member of a vector of parent dummy objects also being looped over:

      They are accessed from different worker methods and their state needs to persist in those different worker methods.

      Are you saying that e.g., Job x Subjob y, has to have some state inside startOperation1() that:

      • Is different from the state it has in startOperation2()..startOperationN()
      • Persists between calls startOperation1()

      The subvector object members (subjobs) need to be able to be modified from the operation methods in the worker object and not copy constructed or implicitly shared.

      Each startOperationN() currently gets a copy of Job x subjobs independent of any other.
      You want to both modify the original subjob without copying it. This is what pointers (or references) are for. However, this automatically breaks the independence between startOperationN() methods you appear to ask for. For example, what is to happen when startOperation1() modifies Job x Subjob y in a way that invalidates anything that startOperation2() thinks it knows about that subjob?

      C 1 Reply Last reply
      0
      • C ChrisW67

        @Crag_Hack said in What's the best way to loop over a vector that is a member of a vector of parent dummy objects also being looped over:

        They are accessed from different worker methods and their state needs to persist in those different worker methods.

        Are you saying that e.g., Job x Subjob y, has to have some state inside startOperation1() that:

        • Is different from the state it has in startOperation2()..startOperationN()
        • Persists between calls startOperation1()

        The subvector object members (subjobs) need to be able to be modified from the operation methods in the worker object and not copy constructed or implicitly shared.

        Each startOperationN() currently gets a copy of Job x subjobs independent of any other.
        You want to both modify the original subjob without copying it. This is what pointers (or references) are for. However, this automatically breaks the independence between startOperationN() methods you appear to ask for. For example, what is to happen when startOperation1() modifies Job x Subjob y in a way that invalidates anything that startOperation2() thinks it knows about that subjob?

        C Offline
        C Offline
        Crag_Hack
        wrote on last edited by Crag_Hack
        #3

        @ChrisW67 said in What's the best way to loop over a vector that is a member of a vector of parent dummy objects also being looped over:

        Are you saying that e.g., Job x Subjob y, has to have some state inside startOperation1() that:

        Is different from the state it has in startOperation2()..startOperationN()
        Persists between calls startOperation1()

        I am saying that the state before and after each operation is different but that those changes need to be recorded in the job object. So operation1 will change the state of the subjob and operation2 will be able to see those changes and change the state if it needs to and so on for as many operations as necessary. I should have said state changes to persist through the different worker methods.

        @ChrisW67 said in What's the best way to loop over a vector that is a member of a vector of parent dummy objects also being looped over:

        Each startOperationN() currently gets a copy of Job x subjobs independent of any other.
        You want to both modify the original subjob without copying it. This is what pointers (or references) are for. However, this automatically breaks the independence between startOperationN() methods you appear to ask for. For example, what is to happen when startOperation1() modifies Job x Subjob y in a way that invalidates anything that startOperation2() thinks it knows about that subjob?

        I was thinking I could return by reference for the getSubJobs() function but don't think that is "proper" coding practice. I think I didn't explain myself well enough... see the first part of this post.

        Thanks again!

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Crag_Hack
          wrote on last edited by
          #4

          Things I've thought of so far but I don't think are proper... return by reference for getSubJobs(), making subJobs public, friending Worker and Job, making a Map with job name keys and subJob values in Worker, making operation methods static and calling from Job methods (too much recoding).

          1 Reply Last reply
          0

          • Login

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