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 6221675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:10:34+00:00 2026-05-24T08:10:34+00:00

I’m trying to write an event system for my game. The callbacks that my

  • 0

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.

• 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.

class EventManager
{
    typedef boost::function<void (boost::weak_ptr<Event>)> Callback;
    std::map<Event::Type, std::vector<Callback>> eventHandlerMap_;
};

• I also tried using boost::signal, but that also gives me a compilation problem related to operator==:

binary ‘==’ : no operator found which takes a left-hand operand of type ‘const Functor’ (or there is no acceptable conversion)

void test(int c) {
    std::cout << "test(" << c << ")";
}

struct Functor
{
    void operator()(int g) {
        std::cout << "Functor::operator(" << g << ")";
    }
};

int main()
{
    boost::signal<void (int)> sig;

    Functor f;

    sig.connect(test);
    sig.connect(f);

    sig(7);

    sig.disconnect(f); // Error
}

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.)


Edit: This is the interface of that I’d like EventManager to have.

class EventManager
{
  public:
    void addEventHandler(Event::Type evType, Callback func);
    void removeEventHandler(Event::Type evType, Callback func);

    void queueEvent(boost::shared_ptr<Event> ev);
    void dispatchNextEvent();
};
  • 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-05-24T08:10:35+00:00Added an answer on May 24, 2026 at 8:10 am

    No matter, I found the solution. A little template magic and things become simple(r):

    template<typename F>
    void EventManager::removeEventHandler(Event::Type evType, F func)
    {
        auto compare = [func](const Callback& other) -> bool {
            F const* f = other.target<F>();
            if (f == nullptr) return false;
            return *f == func;
        };
    
        std::vector<Callback>& callbacks = ...;
        auto pend = std::remove_if(callbacks.begin(), callbacks.end(), compare);
        callbacks.erase(pend, callbacks.end());
    }
    
    
    template<typename R, typename F, typename L>
    void EventManager::removeEventHandler(
        Event::Type evType, const boost::_bi::bind_t<R, F, L>& func)
    {
        auto compare = [&func](const Callback& other) -> bool {
            auto const* f = other.target<boost::_bi::bind_t<R, F, L>>();
            if (f == nullptr) return false;
            return func.compare(*f);
        };
    
        std::vector<Callback>& callbacks = ...;
        auto pend = std::remove_if(callbacks.begin(), callbacks.end(), compare);
        callbacks.erase(pend, callbacks.end());
    }
    

    I need to handle Boost.Bind objects separately because operator== doesn’t actually do comparison for Bind objects, but produce a new functor that compares the result of the other two (read more). To compare Boost.Bind you have to use the member function compare().

    The type boost::_bi::bind_t seems to be an internal type of Boost (I guess that’s what the underscore in namespace ‘_bi’ means), however it should be safe to use it as all overloads of boost::function_equal also use this type (reference).

    This code will work for all types of functors as long as there is an operator== defined that does comparison, or if you’re using Boost.Bind. I had a superficial look into std::bind (C++0x), but that doesn’t seem to be comparable, so it won’t work with the code I posted above.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a French site that I want to parse, but am running into

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.