Add code for dumping all registered types / functions
This commit is contained in:
parent
a8ba63277e
commit
ee46b63f95
@ -1,5 +1,5 @@
|
||||
#ifndef __scripting_system_hpp__
|
||||
#define __scripting_system_hpp__
|
||||
#ifndef __boxedcpp_system_hpp__
|
||||
#define __boxedcpp_system_hpp__
|
||||
|
||||
#include <typeinfo>
|
||||
#include <string>
|
||||
@ -9,12 +9,16 @@
|
||||
#include <vector>
|
||||
|
||||
#include "boxed_value.hpp"
|
||||
#include "type_info.hpp"
|
||||
#include "proxy_functions.hpp"
|
||||
#include "proxy_constructors.hpp"
|
||||
|
||||
class Scripting_System
|
||||
class BoxedCPP_System
|
||||
{
|
||||
public:
|
||||
typedef std::multimap<std::string, boost::shared_ptr<Proxy_Function> > Function_Map;
|
||||
typedef std::map<std::string, Type_Info> Type_Name_Map;
|
||||
|
||||
template<typename Function>
|
||||
void register_function(const Function &func, const std::string &name)
|
||||
{
|
||||
@ -41,24 +45,89 @@ class Scripting_System
|
||||
template<typename Type>
|
||||
void register_type(const std::string &name)
|
||||
{
|
||||
m_types.insert(std::make_pair(name, &typeid(Type)));
|
||||
m_types.insert(std::make_pair(name, Get_Type_Info<Type>()()));
|
||||
}
|
||||
|
||||
boost::shared_ptr<Proxy_Function> get_function(const std::string &t_name)
|
||||
std::vector<Type_Name_Map::value_type> get_types() const
|
||||
{
|
||||
std::map<std::string, boost::shared_ptr<Proxy_Function> >::const_iterator itr = m_functions.find(t_name);
|
||||
if (itr != m_functions.end())
|
||||
{
|
||||
return itr->second;
|
||||
} else {
|
||||
throw std::range_error("Function not known: " + t_name);
|
||||
}
|
||||
return std::vector<Type_Name_Map::value_type>(m_types.begin(), m_types.end());
|
||||
}
|
||||
|
||||
std::vector<Function_Map::value_type> get_function(const std::string &t_name) const
|
||||
{
|
||||
std::pair<Function_Map::const_iterator, Function_Map::const_iterator> range
|
||||
= m_functions.equal_range(t_name);
|
||||
|
||||
return std::vector<Function_Map::value_type>(range.first, range.second);
|
||||
}
|
||||
|
||||
std::vector<Function_Map::value_type> get_functions() const
|
||||
{
|
||||
return std::vector<Function_Map::value_type>(m_functions.begin(), m_functions.end());
|
||||
}
|
||||
|
||||
private:
|
||||
std::map<std::string, Boxed_Value > m_objects;
|
||||
std::map<std::string, const std::type_info *> m_types;
|
||||
std::map<std::string, boost::shared_ptr<Proxy_Function> > m_functions;
|
||||
|
||||
Function_Map m_functions;
|
||||
Type_Name_Map m_types;
|
||||
};
|
||||
|
||||
void dump_type(const Type_Info &type)
|
||||
{
|
||||
std::cout << type.m_bare_type_info->name();
|
||||
}
|
||||
|
||||
void dump_function(const BoxedCPP_System::Function_Map::value_type &f)
|
||||
{
|
||||
std::vector<Type_Info> params = f.second->get_param_types();
|
||||
|
||||
dump_type(params.front());
|
||||
std::cout << " " << f.first << "(";
|
||||
|
||||
for (std::vector<Type_Info>::const_iterator itr = params.begin() + 1;
|
||||
itr != params.end();
|
||||
++itr)
|
||||
{
|
||||
dump_type(*itr);
|
||||
std::cout << ", ";
|
||||
}
|
||||
|
||||
std::cout << ")" << std::endl;
|
||||
}
|
||||
|
||||
void dump_system(const BoxedCPP_System &s)
|
||||
{
|
||||
std::cout << "Registered Types: " << std::endl;
|
||||
std::vector<BoxedCPP_System::Type_Name_Map::value_type> types = s.get_types();
|
||||
for (std::vector<BoxedCPP_System::Type_Name_Map::value_type>::const_iterator itr = types.begin();
|
||||
itr != types.end();
|
||||
++itr)
|
||||
{
|
||||
std::cout << itr->first << ": ";
|
||||
dump_type(itr->second);
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
|
||||
std::cout << std::endl; std::vector<BoxedCPP_System::Function_Map::value_type> funcs = s.get_functions();
|
||||
|
||||
std::cout << "Functions: " << std::endl;
|
||||
for (std::vector<BoxedCPP_System::Function_Map::value_type>::const_iterator itr = funcs.begin();
|
||||
itr != funcs.end();
|
||||
++itr)
|
||||
{
|
||||
dump_function(*itr);
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
void bootstrap(BoxedCPP_System &s)
|
||||
{
|
||||
s.register_type<void>("void");
|
||||
s.register_type<double>("double");
|
||||
s.register_type<int>("int");
|
||||
s.register_type<std::string>("string");
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@ -67,7 +67,6 @@ Boxed_Value call_func(const boost::function<Ret ()> &f, const std::vector<Boxed_
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
struct Param_List_Builder
|
||||
{
|
||||
Param_List_Builder &operator<<(const Boxed_Value &so)
|
||||
@ -89,7 +88,6 @@ struct Param_List_Builder
|
||||
}
|
||||
|
||||
std::vector<Boxed_Value> objects;
|
||||
|
||||
};
|
||||
|
||||
|
||||
@ -128,7 +126,22 @@ class Proxy_Function_Impl : public Proxy_Function
|
||||
Func m_f;
|
||||
};
|
||||
|
||||
Boxed_Value dispatch(const std::vector<std::pair<const std::string, boost::shared_ptr<Proxy_Function> > > &funcs,
|
||||
const std::vector<Boxed_Value> &plist)
|
||||
{
|
||||
for (std::vector<std::pair<const std::string, boost::shared_ptr<Proxy_Function> > >::const_iterator itr = funcs.begin();
|
||||
itr != funcs.end();
|
||||
++itr)
|
||||
{
|
||||
try {
|
||||
return (*itr->second)(plist);
|
||||
} catch (const std::bad_cast &) {
|
||||
//try again
|
||||
}
|
||||
}
|
||||
|
||||
throw std::runtime_error("No mathcing function to dispatch to");
|
||||
}
|
||||
|
||||
# endif
|
||||
#else
|
||||
|
@ -55,7 +55,9 @@ void print()
|
||||
//Test main
|
||||
int main()
|
||||
{
|
||||
Scripting_System ss;
|
||||
BoxedCPP_System ss;
|
||||
bootstrap(ss);
|
||||
|
||||
ss.register_type<Test>("Test");
|
||||
ss.register_function(boost::function<int (Test*, double)>(&Test::method), "method");
|
||||
ss.register_function(boost::function<std::string (const std::string &)>(&testprint), "print");
|
||||
@ -69,33 +71,37 @@ int main()
|
||||
ss.add_object("d", boost::shared_ptr<double>(new double(10.2)));
|
||||
ss.add_object("str", std::string("Hello World"));
|
||||
|
||||
|
||||
dump_system(ss);
|
||||
|
||||
|
||||
std::vector<Boxed_Value> sos;
|
||||
sos.push_back(ss.get_object("testobj"));
|
||||
sos.push_back(ss.get_object("d"));
|
||||
|
||||
boost::shared_ptr<Proxy_Function> method1(ss.get_function("method"));
|
||||
boost::shared_ptr<Proxy_Function> method1(ss.get_function("method").front().second);
|
||||
(*method1)(sos);
|
||||
(*method1)(sos);
|
||||
Boxed_Value o = (*method1)(sos);
|
||||
|
||||
(*ss.get_function("print"))(Param_List_Builder() << ss.get_object("str"));
|
||||
dispatch(ss.get_function("print"), Param_List_Builder() << ss.get_object("str"));
|
||||
|
||||
//Add new dynamically created object from registered "Test" constructor
|
||||
ss.add_object("testobj2", (*ss.get_function("Test"))(Param_List_Builder() << std::string("Yo")));
|
||||
ss.add_object("testobj2", dispatch(ss.get_function("Test"), Param_List_Builder() << std::string("Yo")));
|
||||
|
||||
std::cout << Cast_Helper<int>()(o) << std::endl;
|
||||
|
||||
(*ss.get_function("voidfunc"))(std::vector<Boxed_Value>());
|
||||
dispatch(ss.get_function("voidfunc"), Param_List_Builder());
|
||||
|
||||
std::vector<Boxed_Value> sos3;
|
||||
sos3.push_back(ss.get_object("testobj2"));
|
||||
(*ss.get_function("show_message"))(sos3);
|
||||
dispatch(ss.get_function("show_message"), sos3);
|
||||
|
||||
Boxed_Value stringref = (*ss.get_function("get_message"))(sos3);
|
||||
Boxed_Value stringref = dispatch(ss.get_function("get_message"), sos3);
|
||||
|
||||
std::string &sr = Cast_Helper<std::string &>()(stringref);
|
||||
sr = "Bob Updated The message";
|
||||
|
||||
(*ss.get_function("show_message"))(sos3);
|
||||
dispatch(ss.get_function("show_message"), sos3);
|
||||
}
|
||||
|
||||
|
@ -29,7 +29,30 @@ struct Get_Type_Info
|
||||
boost::is_void<T>::value,
|
||||
&typeid(T),
|
||||
&typeid(typename boost::remove_const<typename boost::remove_pointer<typename boost::remove_reference<T>::type>::type>::type));
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct Get_Type_Info<boost::shared_ptr<T> >
|
||||
{
|
||||
Type_Info operator()()
|
||||
{
|
||||
return Type_Info(boost::is_const<T>::value, boost::is_reference<T>::value, boost::is_pointer<T>::value,
|
||||
boost::is_void<T>::value,
|
||||
&typeid(boost::shared_ptr<T> ),
|
||||
&typeid(typename boost::remove_const<typename boost::remove_pointer<typename boost::remove_reference<T>::type>::type>::type));
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct Get_Type_Info<boost::reference_wrapper<T> >
|
||||
{
|
||||
Type_Info operator()()
|
||||
{
|
||||
return Type_Info(boost::is_const<T>::value, boost::is_reference<T>::value, boost::is_pointer<T>::value,
|
||||
boost::is_void<T>::value,
|
||||
&typeid(boost::reference_wrapper<T> ),
|
||||
&typeid(typename boost::remove_const<typename boost::remove_pointer<typename boost::remove_reference<T>::type>::type>::type));
|
||||
}
|
||||
};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user