简体   繁体   中英

How to assign same signature to functions with different signatures?

In my C++ application, I have 2 threads: (i) main thread, (ii) background thread. I have a class defined as:

class helper
{
 public:
     bool login(const string& username, const string& password);
     void logout();

 private:
     bool loginInternal(const string& username, const string& password);
     void logoutInternal();
}

helper::login() and helper::logout() functions (and several other member functions with various return types and # of params and param types) are called on the main thread. In the implementations of these functions, the corresponding internal functions are to be enqueued in a queue, and the background thread calls these internal functions in the order in which they were enqueued. So something like this:

bool helper::login(const string& username, const string& password)
{
    queue.push_back(boost::bind(&helper::loginInternal, this, username, password));
}

void helper::logout()
{
    queue.push_back(boost::bind(&helper::logoutInternal, this));
}

All this time the background thread is running, waiting for the queue to fill up, and as soon as it does, this background thread would start calling the functions in the queue:

queue.front()();
queue.pop_front();

So the question is, how do I define such a queue?

deque<???> queue;

What could be the data type for this queue such that it can hold callback functions with different signatures in the same queue?

EDIT: Here's the solution (thanks to J. Calleja):

typedef boost::function<void ()> Command;
deque<Command> queue;

and then call the functor like this:

// Execute the command at the front
Command cmd = queue.front();
cmd();

// remove the executed command from the queue
queue.pop_front();

You may use boost::function if you normalize the return type or ignore it. This library defines a wrapper that can store any element that matches your signature (function or functor).

Using your example:

#include <boost/function.hpp>

class helper 
{  
public:      
  bool login(const std::string& username, const std::string& password);
  void logout();
private:      
  bool loginInternal(const std::string& username, const std::string& password);
  void logoutInternal();
private:
  typedef boost::function<void ()> Command;
  std::deque<Command> queue;
};

This example ignores the return type as it declares the functions returning void. If you want to know the return value, you have to make logout return a bool and change the declaration to:

  typedef boost::function<bool ()> Command;

I believe the type for the first one is bind<bool> and for the second it is bind<void> . As these are two different types, you can't put them into one queue. Making logout return a bool (even if it always returns true or something) would be one (presumably relatively painless) way to solve this issue.

tl;dr: Change logout to return a bool , and then declare your queue as deque< bind<bool> > .

EDIT: Considering the many types, I suggest that you create some kind of special container class for yourself. Something like this:

class EnqueuedFunc
{
    virtual void operator()() = 0;
};

class LoginFunc : public EnqueuedFunc
{
    bind<bool> func;

    LoginFunc(bind<bool> fn)
    {
        func = fn;
    }
    void operator()()
    {
        fn();
    }
};

class LogoutFunc : public EnqueuedFunc
{
    bind<void> func;

    LoginFunc(bind<void> fn)
    {
        func = fn;
    }
    void operator()()
    {
        fn();
    }
};

And then your queue is deque<EnqueuedFunc> .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM