Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Access a class instance based on its ID

Access a class instance based on its ID

Scheduled Pinned Locked Moved C / C++ / MFC
questiondata-structures
4 Posts 2 Posters 0 Views 1 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.
  • M Offline
    M Offline
    meerokh
    wrote on last edited by
    #1

    I have two queues in my program,queue and waitingQueue. queue contains the workpackage instances that are created by user . After creating a workpackge by a user, if he decides to yield it, i want to change the status of created workpackage to waiting and add it to the waitingQueue. Creation of workpackage returns a unique id of the package that is added to the queue. This is what i want to acheive:

    //Done in user application
    int packageId = creatpackage(Function, arguments); // this create a package and adds it to a normal queue
    wait(packageId); //wait call yeild function that is part of my library

    //Inside library
    void classname::yeild(int packageId){
    set the state of workpackage to Waiting
    add the package to WaitingQueue
    if(there is another package in WaitingQueue)
    Make a switch context to it
    else
    Go to normal queue and execute the package
    }

    My question is how can i access the workpackage state after which yeild function was called. I thought of accessing it based on id somehow(is it even possible??) Based on my full scenario, is there any better approach then what i am doing??

    L 1 Reply Last reply
    0
    • M meerokh

      I have two queues in my program,queue and waitingQueue. queue contains the workpackage instances that are created by user . After creating a workpackge by a user, if he decides to yield it, i want to change the status of created workpackage to waiting and add it to the waitingQueue. Creation of workpackage returns a unique id of the package that is added to the queue. This is what i want to acheive:

      //Done in user application
      int packageId = creatpackage(Function, arguments); // this create a package and adds it to a normal queue
      wait(packageId); //wait call yeild function that is part of my library

      //Inside library
      void classname::yeild(int packageId){
      set the state of workpackage to Waiting
      add the package to WaitingQueue
      if(there is another package in WaitingQueue)
      Make a switch context to it
      else
      Go to normal queue and execute the package
      }

      My question is how can i access the workpackage state after which yeild function was called. I thought of accessing it based on id somehow(is it even possible??) Based on my full scenario, is there any better approach then what i am doing??

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      In theory yes, you can do it that way. But you need some mechanism to find the actual object from the id. When you call the creatpackage function, where does it keep all the data related to that package?

      M 1 Reply Last reply
      0
      • L Lost User

        In theory yes, you can do it that way. But you need some mechanism to find the actual object from the id. When you call the creatpackage function, where does it keep all the data related to that package?

        M Offline
        M Offline
        meerokh
        wrote on last edited by
        #3

        It got stored in an instance of class WorkPackage which in turn is added to packagequeue WorkPackage.h

        class WorkPackage {
        private:
        WorkPackageState wp_state;
        void (*m_action)(void*);
        void* m_arguments = nullptr;
        protected:
        static int id;

        public:
        WorkPackage(){};
        WorkPackage(void (*action)(void*), void* arguments);
        void destroystack();
        void execute();
        static void setState(WorkPackageState wp_state);
        WorkPackageState getState();
        Stack Wp_localstack;
        fcontext_t m_context;
        int packageId;
        };

        Workpackage.cpp

        WorkPackage::WorkPackage(void (*action)(void*), void* arguments) {
        Wp_localstack.local_stack= Stack::make_stack();
        m_action = action;
        m_arguments = arguments;
        Wp_localstack.local_stack = static_cast (Wp_localstack.local_stack) + 1000;
        m_context = make_fcontext(Wp_localstack.local_stack, 1000, m_action);
        wp_state = running;
        packageId=++id;
        }

        WorkPackageState WorkPackage::getState() {
        return state;
        }

        void WorkPackage::execute(int thread_id) {
        m_action(m_arguments);
        destroystack();
        }

        void WorkPackage::setState(WorkPackageState state) {
        WorkPackageState new_state = state;
        }

        L 1 Reply Last reply
        0
        • M meerokh

          It got stored in an instance of class WorkPackage which in turn is added to packagequeue WorkPackage.h

          class WorkPackage {
          private:
          WorkPackageState wp_state;
          void (*m_action)(void*);
          void* m_arguments = nullptr;
          protected:
          static int id;

          public:
          WorkPackage(){};
          WorkPackage(void (*action)(void*), void* arguments);
          void destroystack();
          void execute();
          static void setState(WorkPackageState wp_state);
          WorkPackageState getState();
          Stack Wp_localstack;
          fcontext_t m_context;
          int packageId;
          };

          Workpackage.cpp

          WorkPackage::WorkPackage(void (*action)(void*), void* arguments) {
          Wp_localstack.local_stack= Stack::make_stack();
          m_action = action;
          m_arguments = arguments;
          Wp_localstack.local_stack = static_cast (Wp_localstack.local_stack) + 1000;
          m_context = make_fcontext(Wp_localstack.local_stack, 1000, m_action);
          wp_state = running;
          packageId=++id;
          }

          WorkPackageState WorkPackage::getState() {
          return state;
          }

          void WorkPackage::execute(int thread_id) {
          m_action(m_arguments);
          destroystack();
          }

          void WorkPackage::setState(WorkPackageState state) {
          WorkPackageState new_state = state;
          }

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          So, I guess, packagequeue is the place to find it.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups