2

我必须存储不同boost::function对象的列表。为了提供这一点,我正在使用 boost::any。我有一些函数采用不同的函数签名,将它们打包成任何函数,然后插入到具有给定类型的特殊映射中。这是代码:

enum TypeEnumerator
{
    e_int,
    e_float,
    e_double
};

typedef map< string, pair<any, TypeEnumerator> > CallbackType;
CallbackType mCallbacks;

void Foo(const string &name, function<float ()> f)
{
    mCallbacks[name] = make_pair(any(f), CLASS::e_float);
}
void Foo(const string &name, function<int ()> f) { /* the same, but with e_int */ }
void Foo(const string &name, function<double ()> f) { /* the same, but with e_double */ }

现在我有地图增强功能,从枚举中打包到任何给定类型中,以便将来识别它。现在我必须调用给定的函数。来自 any 的铸造不起作用:

BOOST_FOREACH(CallbackType::value_type &row, mCallbacks)
{
    // pair<any, TypeEnumerator>
    switch (row.second.second) // Swith the TypeEnumerator
    {
        case 0: // int
            any_cast< function<int ()> >(row.first)();
        break;
        case 1: // float
            any_cast< function<float ()> >(row.first)();
        break;
        case 2: // double
            any_cast< function<double ()> >(row.first)();
        break;
    }
}

这不会强制转换,并且在运行期间出现异常:

  what():  boost::bad_any_cast: failed conversion using boost::any_cast

是否可以转换回boost::function对象?

4

2 回答 2

4

@TC provided the solution for the runtime error. But I believe you should use Boost.Variant instead of Boost.Any as there are only a fixed selection of types it can store. With Boost.Variant you could eliminate that enum too, as it already provided a standard visitor pattern interface. (result):

#include <boost/variant.hpp>
#include <boost/function.hpp>
#include <boost/foreach.hpp>
#include <map>
#include <string>
#include <iostream>

typedef boost::variant<boost::function<int()>,
                       boost::function<float()>,
                       boost::function<double()> > Callback;
typedef std::map<std::string, Callback> CallbackType;

CallbackType mCallbacks;

void Foo(const std::string& name, const Callback& f) {
    mCallbacks[name] = f;
}

//------------------------------------------------------------------------------

float f() { 
    std::cout << "f called" << std::endl;
    return 4;
}

int g() {
    std::cout << "g called" << std::endl;
    return 5;
}

double h() {
    std::cout << "h called" << std::endl;
    return 4;
}

//------------------------------------------------------------------------------

struct call_visitor : public boost::static_visitor<> {
    template <typename T>
    void operator() (const T& operand) const {
        operand();
    }
};


int main () {
    Foo("f", boost::function<float()>( f ));
    Foo("g", boost::function<int()>( g ));
    Foo("h", boost::function<double()>( h ));

    BOOST_FOREACH(CallbackType::value_type &row, mCallbacks) {
        boost::apply_visitor(call_visitor(), row.second);
    }

    return 0;
}
于 2010-08-31T07:36:05.597 回答
3

从外观上看,row.first是回调的名称,a string。您可能应该使用row.second.first

case 0: // int
    any_cast< function<int ()> >(row.second.first)();
    break;

此外,您应该在 switch ( case CLASS::e_int:) 中使用枚举常量,而不是幻数。

于 2010-08-31T07:13:48.287 回答