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();

Answers


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;

Need Your Help

Send form data to external database in wordpress

php mysql database forms external

My client want to have form collecting the user data and save to external database

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.