Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8411771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:30:30+00:00 2026-06-10T00:30:30+00:00

I have a vector of tasks implementing the same interface. I have a state

  • 0

I have a vector of tasks implementing the same interface. I have a state machine object that can have multiple tasks, and I have a whole bunch of events. If a particular event is called, I would like the event to call a function to ‘ProcessTasks’ in which ProcessTasks takes the particular interface function that needs to be called, and calls that function for every task. I would like to avoid having a giant case statement or repeating the for loop iteration in every event function, but I’m not sure how to. Is there a construct/approach that allows me to do this, or is the case statement approach the best method, or the toss the loop in each function best?

Thanks : )

Sample Example ( a single state class in my state patterned sm ):

State_e StateIdle::EVENT_REQUEST_STOP_()
{
    ProcessTasks( HandleStopFn );
    return STATE_STOPPED;
}

// -- more events

/* desired solution allows me to have to implement
   the loop only once, but be able to call any of
   the functions in the interface, for any number of events */

    for( vector<TaskPtr>::iterator it = m_tasks.begin(); it != m_tasks.end(); ++it )
    {
        it->HandlerFunction()
    }

//TaskPtr is boost auto ptr and implements this shortened interface

class Task
{
    void HandleActiveFn() = 0;
    void HandleStopFn() = 0;
};
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T00:30:32+00:00Added an answer on June 10, 2026 at 12:30 am

    Provide a private member function in StateIdle that accepts a Task member function pointer and use std::for_each to invoke the member function on each of the Task instances:

    void _invoker(void (Task::*fun)())
    {
        std::for_each(m_tasks.begin(),
                      m_tasks.end(),
                      [&](TaskPtr a_t) { (a_t->*fun)(); });
    }
    

    See demo http://ideone.com/A4c5U .

    If you wanted to avoid a switch you could construct a function table using a std::map:

    std::map<std::string, void (Task::*)()> function_table;
    function_table["ACTIVE"] = &Task::HandleActiveFn;
    function_table["STOP"]   = &Task::HandleStopFn;
    
    void _invoker(const std::string& a_name)
    {
        auto function_entry = function_table.find(a_name);
        if (function_table.end() != function_entry)
        {
            std::for_each(m_tasks.begin(),
                          m_tasks.end(),
                          [&](TaskPtr a_t)
                          {
                              (a_t->*(function_entry->second))(); 
                          });
        }
    }
    

    And to call:

    _invoker("STOP");
    _invoker("ACTIVE");
    

    which you may prefer to:

    _invoker(&Task::HandleStopFn);
    _invoker(&Task::HandleActiveFn);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

in clojure i have vector [myfn1 myfn2 myfn3] how can i call functions named
I have a vector of objects (objects are term nodes that amongst other fields
I have a file that contains java serialized objects like Vector. I have stored
I have an std::vector -like class that is compiled with Visual C++ 2008. There's
I have vector< pair<int, int>> myVec (N); I want to have all pairs initialized
I have a vector class and I defined the __mul__ method to multiply a
I have the vector d<-1:100 I want to sample k=3 times from this vector
I have a vector where I keep an incrementing data. Normally each element of
I have the vector output = PV_out(:); I am trying to break this down
I have a vector of pairs representing hops between nodes which I would like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.