在容器中存储boost :: function对象

Kai*_*aan 4 c++ boost boost-function

我有一个KeyCallbacks 的向量:

typedef boost::function<void (const KeyEvent&)> KeyCallback
Run Code Online (Sandbox Code Playgroud)

我用它来存储按下键盘按钮时的所有监听器.我可以添加它们并将事件发送到所有回调for_each,但我不知道如何KeyCallback从我的向量中删除特定的签名.

例如,我想要这样的东西:

void InputManager::UnregisterCallback(KeyCallback callback) {
  mKeyCallbacks.erase(std::find(mKeyCallbacks.begin(), mKeyCallbacks.end(), callback));
}
Run Code Online (Sandbox Code Playgroud)

根据boost::function文档(见这里),没有比较函数对象的东西,这可以解释我上面的问题.我被困了吗?这有什么好办法吗?

(我读到boost::signals了回调机制,但它显然很慢,我希望回调可能会在一帧内被解雇几次.)

Evg*_*yuk 8

方法#1:

http://www.boost.org/doc/libs/1_51_0/doc/html/function/tutorial.html#id1546064

函数对象包装器可以通过==或!=与可以存储在包装器中的任何函数对象进行比较.

因此,其中一个解决方案是为UnregisterCallback的参数定义特殊类型(它也支持类型擦除).这是基于事实,你可以将boost :: function与functor/function进行比较 - 结果你仍然会有boost :: function的向量,只有你需要执行比较的地方需要new类型,例如UnregisterCallback:

现场演示

#include <boost/scoped_ptr.hpp>
#include <boost/function.hpp>
#include <algorithm>
#include <iostream>
#include <typeinfo>
#include <ostream>
#include <vector>
#include <string>

using namespace std;
using namespace boost;

typedef int KeyEvent;
typedef function<void (const KeyEvent &)> KeyCallback;

struct AbstractCallback
{
    virtual bool equals(const KeyCallback &f) const=0;
    virtual ~AbstractCallback(){}
};

template<typename Callback>
struct ConcreteCallback : AbstractCallback
{
    const Callback &callback;
    explicit ConcreteCallback(const Callback &p_callback) : callback(p_callback) {}
    virtual bool equals(const KeyCallback &f) const
    {
        return callback == f;
    }
};

struct KeyCallbackChecker
{
    scoped_ptr<AbstractCallback> func;
public:
    template<typename Func>
    KeyCallbackChecker(const Func &f) : func(new ConcreteCallback<Func>(f)) {}
    friend bool operator==(const KeyCallback &lhs,const KeyCallbackChecker &rhs)
    {
        return rhs.func->equals(lhs);
    }
    friend bool operator==(const KeyCallbackChecker &lhs,const KeyCallback &rhs)
    {
        return rhs==lhs;
    }
};

void func1(const KeyEvent &)
{
    cout << "func1" << endl;
}

void func3(const KeyEvent &)
{
    cout << "func3" << endl;
}

class func2
{
    int data;
public:
    explicit func2(int n) : data(n) {}
    friend bool operator==(const func2 &lhs,const func2 &rhs)
    {
        return lhs.data==rhs.data;
    }
    void operator()(const KeyEvent &)
    {
        cout << "func2, data=" << data << endl;
    }
};

struct Caller
{
    template<typename F> void operator()(F f)
    {
        f(1);
    }
};

class Callbacks
{
    vector<KeyCallback> v;
public:
    void register_callback(const KeyCallback &callback)
    {
        v.push_back(callback);
    }
    void unregister_callback(const KeyCallbackChecker &callback)
    {
        vector<KeyCallback>::iterator it=find(v.begin(),v.end(),callback);
        if(it!=v.end())
            v.erase(it);
    }
    void call_all()
    {
        for_each(v.begin(),v.end(),Caller());
        cout << string(16,'_') << endl;
    }
};

int main(int argc,char *argv[])
{
    Callbacks cb;
    cb.register_callback(func1);
    cb.register_callback(func2(1));
    cb.register_callback(func2(2));
    cb.register_callback(func3);
    cb.call_all();

    cb.unregister_callback(func2(2));
    cb.call_all();
    cb.unregister_callback(func1);
    cb.call_all();

return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出是:

func1
func2, data=1
func2, data=2
func3
________________
func1
func2, data=1
func3
________________
func2, data=1
func3
________________
Run Code Online (Sandbox Code Playgroud)

优点:

  • 我们仍然使用boost :: function进行registring并存储在vector中
  • Functor对象只有在需要将其传递给unregister_callback时才应定义比较
  • 它可以很容易地推广 - 只需添加一个模板参数,而不是使用typedefed KeyCallback.因此,可以在其他地方轻松使用,用于其他类型的回调.

缺点:

  • 如果用户已将回调包装到boost :: function - 它不能与unregister_callback一起使用,因为它需要一些可以与boost :: function进行比较的东西(例如函数指针或带有定义比较的函子)


方法#2:

另一种方法是实现自定义的boost :: function-like解决方案,它接受可比较的回调.

现场演示

#include <boost/shared_ptr.hpp>
#include <algorithm>
#include <iostream>
#include <typeinfo>
#include <ostream>
#include <vector>

using namespace std;
using namespace boost;

typedef int KeyEvent;
typedef void (*func_type)(const KeyEvent &);

struct AbstractCallback
{
    virtual void operator()(const KeyEvent &p)=0;
    virtual bool compare_to(const std::type_info &rhs_type,const void *rhs) const=0;
    virtual bool compare_to(const std::type_info &rhs_type,const func_type *rhs) const=0;
    virtual bool equals(const AbstractCallback &rhs) const=0;
};

template<typename Callback>
struct ConcreteCallback : AbstractCallback
{
    Callback callback;
    ConcreteCallback(Callback p_callback) : callback(p_callback) {}
    void operator()(const KeyEvent &p)
    {
        callback(p);
    }
    bool compare_to(const std::type_info &rhs_type,const void *rhs) const
    {
        return (typeid(Callback)==rhs_type) &&
            ( *static_cast<const Callback*>(rhs) == callback );
    }
    bool compare_to(const std::type_info &rhs_type,const func_type *rhs) const
    {
        return false;
    }
    bool equals(const AbstractCallback &rhs) const
    {
        return rhs.compare_to(typeid(Callback),&callback);
    }
};

template<>
struct ConcreteCallback<func_type> : AbstractCallback
{
    func_type callback;
    ConcreteCallback(func_type p_callback) : callback(p_callback) {}
    void operator()(const KeyEvent &p)
    {
        callback(p);
    }
    bool compare_to(const std::type_info &rhs_type,const void *rhs) const
    {
        return false;
    }
    bool compare_to(const std::type_info &rhs_type,const func_type *rhs) const
    {
        return *rhs == callback;
    }
    bool equals(const AbstractCallback &rhs) const
    {
        return rhs.compare_to(typeid(func_type),&callback);
    }
};


struct KeyCallback
{
    shared_ptr<AbstractCallback> func;
public:
    template<typename Func>
    KeyCallback(Func f) : func(new ConcreteCallback<Func>(f)) {}
    friend bool operator==(const KeyCallback &lhs,const KeyCallback &rhs)
    {
        return lhs.func->equals(*rhs.func);
    }
    void operator()(const KeyEvent &p)
    {
        (*func)(p);
    }
};

void func1(const KeyEvent &)
{
    cout << "func1" << endl;
}

void func3(const KeyEvent &)
{
    cout << "func3" << endl;
}

class func2
{
    int data;
public:
    func2(int n) : data(n) {}
    friend bool operator==(const func2 &lhs,const func2 &rhs)
    {
        return lhs.data==rhs.data;
    }
    void operator()(const KeyEvent &)
    {
        cout << "func2, data=" << data << endl;
    }
};

struct Caller
{
    template<typename F>
    void operator()(F f)
    {
        f(1);
    }
};

int main(int argc,char *argv[])
{
    vector<KeyCallback> v;

    v.push_back(KeyCallback(func1));
    v.push_back(KeyCallback(func1));
    v.push_back(KeyCallback(func1));

    v.push_back(KeyCallback(func2(1)));
    v.push_back(KeyCallback(func2(1)));

    v.push_back(KeyCallback(func2(2)));
    v.push_back(KeyCallback(func2(2)));
    v.push_back(KeyCallback(func2(2)));
    v.push_back(KeyCallback(func2(2)));

    v.push_back(KeyCallback(func3));

    for_each(v.begin(),v.end(),Caller());

    cout << count(v.begin(),v.end(),KeyCallback(func1)) << endl;
    cout << count(v.begin(),v.end(),KeyCallback(func2(1))) << endl;
    cout << count(v.begin(),v.end(),KeyCallback(func2(2))) << endl;
    cout << count(v.begin(),v.end(),KeyCallback(func3)) << endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出是:

func1
func1
func1
func2, data=1
func2, data=1
func2, data=2
func2, data=2
func2, data=2
func2, data=2
func3
3
2
4
1
Run Code Online (Sandbox Code Playgroud)

优点:

  • 我们在注册/取消注册回调中使用相同的类型.用户可以将他的函数和函子存储在包装到KeyCallback之外 - 并将KeyCallback传递给我们的unregister_callback.
  • 对boost :: function没有依赖性

缺点:

  • Functor对象必须具有已定义的比较,即使它未与unregister_callback一起使用
  • 如果用户已将回调包装到boost :: function - 它无法转换为我们的KeyCallback,因为它需要定义比较.
  • 如果你需要在其他地方使用类似的功能,使用不同类型的回调 - 那么我们的boost :: function-like类应该被改进(使用不同的和几个参数等等),或者我们可以提取和修改boost :: funciton本身.


方法#3:

这里我们创建一个继承自std/boost :: function的新类

现场演示

#include <type_traits>
#include <functional>
#include <algorithm>
#include <stdexcept>
#include <iostream>
#include <typeinfo>
#include <utility>
#include <ostream>
#include <vector>
#include <string>

using namespace std;

// _____________________________Implementation__________________________________________

#define USE_VARIADIC_TEMPLATES 0

template<typename Callback,typename Function>
bool func_compare(const Function &lhs,const Function &rhs)
{
    typedef typename conditional<is_function<Callback>::value,typename add_pointer<Callback>::type,Callback>::type request_type;
    if (const request_type* lhs_internal = lhs.template target<request_type>())
        if (const request_type* rhs_internal = rhs.template target<request_type>())
            return *rhs_internal == *lhs_internal;
    return false;
}

#if USE_VARIADIC_TEMPLATES
    #define FUNC_SIG_TYPES typename ...Args
    #define FUNC_SIG_TYPES_PASS Args...
#else
    #define FUNC_SIG_TYPES typename function_signature
    #define FUNC_SIG_TYPES_PASS function_signature
#endif

template<FUNC_SIG_TYPES>
struct function_comparable: function<FUNC_SIG_TYPES_PASS>
{
    typedef function<FUNC_SIG_TYPES_PASS> Function;
    bool (*type_holder)(const Function &,const Function &);
public:
    function_comparable(){}
    template<typename Func>
    function_comparable(Func f_)
        : Function(f_), type_holder(func_compare<Func,Function>)
    {
    }
    template<typename Func>
    function_comparable &operator=(Func f_)
    {
        Function::operator=(f_);
        type_holder=func_compare<Func,Function>;
        return *this;
    }
    friend bool operator==(const Function &lhs,const function_comparable &rhs)
    {
        return rhs.type_holder(lhs,rhs);
    }
    friend bool operator==(const function_comparable &lhs,const Function &rhs)
    {
        return rhs==lhs;
    }
    friend void swap(function_comparable &lhs,function_comparable &rhs)// noexcept
    {
        lhs.swap(rhs);
        lhs.type_holder.swap(rhs.type_holder);
    }
};

// ________________________________Example______________________________________________

typedef void (function_signature)();

void func1()
{
    cout << "func1" << endl;
}

void func3()
{
    cout << "func3" << endl;
}

class func2
{
    int data;
public:
    explicit func2(int n) : data(n) {}
    friend bool operator==(const func2 &lhs,const func2 &rhs)
    {
        return lhs.data==rhs.data;
    }
    void operator()()
    {
        cout << "func2, data=" << data << endl;
    }
};
struct Caller
{
    template<typename Func>
    void operator()(Func f)
    {
        f();
    }
};
class Callbacks
{
    vector<function<function_signature>> v;
public:
    void register_callback_comparator(function_comparable<function_signature> callback)
    {
        v.push_back(callback);
    }
    void register_callback(function<function_signature> callback)
    {
        v.push_back(callback);
    }
    void unregister_callback(function_comparable<function_signature> callback)
    {
        auto it=find(v.begin(),v.end(),callback);
        if(it!=v.end())
            v.erase(it);
        else
            throw runtime_error("not found");
    }
    void call_all()
    {
        for_each(v.begin(),v.end(),Caller());
        cout << string(16,'_') << endl;
    }
};

int main()
{
    Callbacks cb;
    function_comparable<function_signature> f;
    f=func1;
    cb.register_callback_comparator(f);

    cb.register_callback(func2(1));
    cb.register_callback(func2(2));
    cb.register_callback(func3);
    cb.call_all();

    cb.unregister_callback(func2(2));
    cb.call_all();
    cb.unregister_callback(func1);
    cb.call_all();
}
Run Code Online (Sandbox Code Playgroud)

输出是:

func1
func2, data=1
func2, data=2
func3
________________
func1
func2, data=1
func3
________________
func2, data=1
func3
________________
Run Code Online (Sandbox Code Playgroud)

优点:

  • 我们可以在注册/取消注册回调中使用相同的类型.用户可以将他的函数和函子存储在包装到KeyCallback之外 - 并将KeyCallback传递给我们的unregister_callback.在这个版本中,我们可以使用普通的boost :: function作为寄存器功能的参数.
  • 我们仍然可以将boost :: function用于registring并存储在vector中
  • 当我们使用boost :: function进行注册时,functor对象应该只在需要将它传递给unregister_callback时才定义比较.
  • 它是通用的 - 因此,可以很容易地在其他地方使用,用于其他类型的回调.
  • 此版本基于普通函数指针而不是分配+抽象类(vptr).所以它有一个较少的无效,更容易管理.

缺点:

  • 如果用户已将回调包装到boost :: function - 它不能与unregister_callback一起使用,因为它需要一些可以与boost :: function进行比较的东西(例如函数指针或带有定义比较的函子)


编辑:

太棒了,我现在正在尝试#1,但我不太明白为什么它应用我们自己的==运算符时有效?

boost :: function 可以与函数或函子进行比较,但不能与另一个boost :: function进行比较:

#include <boost/function.hpp>

void f1(){}
void f2(){}

int main()
{
    boost::function<void ()> bf1(f1),bf2(f2);
    bf1 == f1; // Works OK
    //bf1 == bf2; - COMPILE ERROR
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

在我们的#1方法中,我们进行类似于"bf1 == f1;"的比较.KeyCallbackChecker捕获函子/函数并在ConcreteCallback :: equals中执行此类比较.