title: Messaging system: Callbacks can be anything
body: <p>I'm trying to write an event system for my game. The callbacks that my event manager will store can be both plain functions as well as functors. I also need to be able to compare functions/functors so I know which one I need to disconnect from the event manager.</p>
  
  <p>• Initially I tried using boost::function; it handles functions and functors perfectly well, except it has no operator==, so I can't remove callbacks if I want to.</p>
  
  <pre><code>class EventManager
  {
      typedef boost::function&lt;void (boost::weak_ptr&lt;Event&gt;)&gt; Callback;
      std::map&lt;Event::Type, std::vector&lt;Callback&gt;&gt; eventHandlerMap_;
  };
  </code></pre>
  
  <p>• I also tried using boost::signal, but that also gives me a compilation problem related to operator==:</p>
  
  <blockquote>
    <p>binary '==' : no operator found which takes a left-hand operand of type 'const Functor' (or there is no acceptable conversion)</p>
  </blockquote>
  
  <pre><code>void test(int c) {
      std::cout &lt;&lt; "test(" &lt;&lt; c &lt;&lt; ")";
  }
  
  struct Functor
  {
      void operator()(int g) {
          std::cout &lt;&lt; "Functor::operator(" &lt;&lt; g &lt;&lt; ")";
      }
  };
  
  int main()
  {
      boost::signal&lt;void (int)&gt; sig;
  
      Functor f;
  
      sig.connect(test);
      sig.connect(f);
  
      sig(7);
  
      sig.disconnect(f); // Error
  }
  </code></pre>
  
  <p>Any other suggestions about how I might implement this? Or maybe how I can make either boost:: function or boost::signal work? (I'd rather use boost:: function though, since I've heard signal is rather slow for small collections of items.)</p>
  
  <hr>
  
  <p><strong>Edit:</strong> This is the interface of that I'd like EventManager to have.</p>
  
  <pre><code>class EventManager
  {
    public:
      void addEventHandler(Event::Type evType, Callback func);
      void removeEventHandler(Event::Type evType, Callback func);
  
      void queueEvent(boost::shared_ptr&lt;Event&gt; ev);
      void dispatchNextEvent();
  };
  </code></pre>