私はこれを見つけましたhttps://gist.github.com/2945472しかし、私はC++ 11に依存しない実装が必要です。私はブーストだけを使用するようにそれを変換して私の手を試しましたが、私はいくつかの問題を抱えています。マップに挿入するとき、私はbad_any_castを取得していますvisitor pattern for boost :: any
#include <boost/any.hpp>
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <boost/lambda/lambda.hpp>
#include <boost/unordered_map.hpp>
struct type_info_hash {
std::size_t operator()(std::type_info const & t) const {
return t.hash_code();
}
};
struct equal_ref {
template <typename T> bool operator()(boost::reference_wrapper<T> a,boost::reference_wrapper<T> b) const {
return a.get() == b.get();
}
};
struct any_visitor {
boost::unordered_map<boost::reference_wrapper<std::type_info const>, boost::function<void(boost::any&)>, type_info_hash, equal_ref> fs;
template <typename T> void insert_visitor(boost::function<void(T)> f) {
try {
fs.insert(std::make_pair(boost::ref(typeid(T)), boost::bind(f, boost::any_cast<T>(boost::lambda::_1))));
} catch (boost::bad_any_cast& e) {
std::cout << e.what() << std::endl;
}
}
bool operator()(boost::any & x) {
boost::unordered_map<boost::reference_wrapper<std::type_info const>, boost::function<void(boost::any&)>, type_info_hash, equal_ref>::iterator it = fs.find(boost::ref(x.type()));
if (it != fs.end()) {
it->second(x);
return true;
} else {
return false;
}
}
};
struct abc {};
void fa(int i) { std::cout << "fa(" << i << ")" << std::endl; }
void fb(abc) { std::cout << "fb(abc())" << std::endl; }
int main() {
any_visitor f;
f.insert_visitor<int>(fa);
f.insert_visitor<abc>(fb);
std::vector<boost::any> xs;
xs.push_back(1);
xs.push_back(abc());
xs.push_back(1.5);
for (auto & x : xs) {
if (!f(x)) std::cout << "no visitor registered" << std::endl;
}
}
:ここ
は私が思いついたものです。 any_castはit-> second(x)だけ呼び出されるべきではありませんか?私は間違って何をしていますか?
訪問者がすぐにサポートされるようにするために 'boost :: variant'を使用することを検討しましたか? 'any'の使用は、型が* anything *、つまり型システムの* all *型であることを前提としています。 'variant'は、オブジェクトで使用したいかもしれない型のサブセットがあることを前提としています。異なる機能が定義されなければならないので、ビジターは「バリアント」に近い。 –
私はこれを使ってboost :: program_optionsから設定ファイルを書き出します。これはboost :: anyを使います。 – Keith