Initial revision
[luabind.git] / examples / intrusive_ptr / any_converter.cpp
blob8eda997e0db65e3989943b1d5a85a0d5c2080368
1 template<class T>
2 struct convert_any
4 static void convert(lua_State* L, const boost::any& a)
6 typename luabind::detail::default_converter::template generate_converter<T>::type conv;
7 conv.apply(any_cast<T>(&a));
9 };
11 std::map<const std::type_info*, void(*)(const boost::any&)> any_converters;
13 template<class T>
14 void register_any_converter()
16 any_converters[&typeid(T)] = convert_any<T>::convert;
19 namespace luabind
21 namespace converters
23 yes_t is_user_defined(by_value<boost::any>);
24 yes_t is_user_defined(by_const_reference<boost::any>);
26 void convert_cpp_to_lua(lua_State* L, const boost::any& a)
28 typedef void(*conv_t)(const boost::any&);
29 conv_t conv = any_converters[&a.type()];
30 conv(L, a);
35 boost::any f()
37 return "foobar";
40 int main()
42 register_any_converter<int>();
43 register_any_converter<float>();
44 register_any_converter<const char*>();
45 register_any_converter<std::string>();
46 register_any_converter<boost::intrusive_ptr<Actor> >();
48 lua_State* L = lua_open();
50 function(L, "f", &f);
52 lua_close(L);