Initial check-in of typelesscpp directory
This commit is contained in:
parent
ee8eb8773a
commit
7e549e25dd
50
typelesscpp/scripting_constructors.hpp
Normal file
50
typelesscpp/scripting_constructors.hpp
Normal file
@ -0,0 +1,50 @@
|
||||
#ifndef __scripting_constructors_hpp__
|
||||
#define __scripting_constrcutors_hpp__
|
||||
|
||||
#include <boost/shared_ptr.hpp>
|
||||
#include <boost/bind.hpp>
|
||||
#include <boost/function.hpp>
|
||||
|
||||
//Constructors, to be made variadic
|
||||
template<typename Class>
|
||||
boost::shared_ptr<Class> constructor()
|
||||
{
|
||||
return boost::shared_ptr<Class>(new Class());
|
||||
}
|
||||
|
||||
template<typename Class, typename Param1>
|
||||
boost::shared_ptr<Class> constructor(Param1 p1)
|
||||
{
|
||||
return boost::shared_ptr<Class>(new Class(p1));
|
||||
}
|
||||
|
||||
template<typename Class, typename Param1, typename Param2>
|
||||
boost::shared_ptr<Class> constructor(Param1 p1, Param2 p2)
|
||||
{
|
||||
return boost::shared_ptr<Class>(new Class(p1, p2));
|
||||
}
|
||||
|
||||
template<typename Class>
|
||||
boost::function<boost::shared_ptr<Class> ()> build_constructor()
|
||||
{
|
||||
typedef boost::shared_ptr<Class> (*func)();
|
||||
return boost::function<boost::shared_ptr<Class> ()>(func(&(constructor<Class>)));
|
||||
}
|
||||
|
||||
template<typename Class, typename Param1>
|
||||
boost::function<boost::shared_ptr<Class> (Param1)> build_constructor()
|
||||
{
|
||||
typedef boost::shared_ptr<Class> (*func)(Param1);
|
||||
return boost::function<boost::shared_ptr<Class> (Param1)>(func(&(constructor<Class, Param1>)));
|
||||
}
|
||||
|
||||
template<typename Class, typename Param1, typename Param2>
|
||||
boost::function<boost::shared_ptr<Class> (Param1, Param2)> build_constructor()
|
||||
{
|
||||
typedef boost::shared_ptr<Class> (*func)(Param1, Param2);
|
||||
return boost::function<boost::shared_ptr<Class> (Param1, Param2)>(func(&(constructor<Class, Param1, Param2>)));
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
|
131
typelesscpp/scripting_functions.hpp
Normal file
131
typelesscpp/scripting_functions.hpp
Normal file
@ -0,0 +1,131 @@
|
||||
#ifndef __scripting_function_hpp__
|
||||
#define __scripting_function_hpp__
|
||||
|
||||
|
||||
#include "scripting_object.hpp"
|
||||
|
||||
#include <string>
|
||||
#include <boost/function.hpp>
|
||||
#include <boost/bind.hpp>
|
||||
#include <stdexcept>
|
||||
#include <vector>
|
||||
|
||||
|
||||
// handle_return implementations
|
||||
template<typename Ret>
|
||||
struct Handle_Return
|
||||
{
|
||||
Scripting_Object operator()(const boost::function<Ret ()> &f)
|
||||
{
|
||||
return Scripting_Object(f());
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Ret>
|
||||
struct Handle_Return<Ret &>
|
||||
{
|
||||
Scripting_Object operator()(const boost::function<Ret &()> &f)
|
||||
{
|
||||
return Scripting_Object(boost::ref(f()));
|
||||
}
|
||||
};
|
||||
|
||||
template<>
|
||||
struct Handle_Return<void>
|
||||
{
|
||||
Scripting_Object operator()(const boost::function<void ()> &f)
|
||||
{
|
||||
f();
|
||||
return Scripting_Object();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
// call_func implementations todo: handle reference return types
|
||||
// to be made variadic
|
||||
template<typename Ret, typename Param1, typename Param2>
|
||||
Scripting_Object call_func(const boost::function<Ret (Param1, Param2)> &f, const std::vector<Scripting_Object> ¶ms)
|
||||
{
|
||||
if (params.size() != 2)
|
||||
{
|
||||
throw std::range_error("Incorrect number of parameters");
|
||||
} else {
|
||||
return Handle_Return<Ret>()(boost::bind(f, Cast_Helper<Param1>()(params[0]), Cast_Helper<Param2>()(params[1])));
|
||||
}
|
||||
}
|
||||
|
||||
template<typename Ret, typename Param1>
|
||||
Scripting_Object call_func(const boost::function<Ret (Param1)> &f, const std::vector<Scripting_Object> ¶ms)
|
||||
{
|
||||
if (params.size() != 1)
|
||||
{
|
||||
throw std::range_error("Incorrect number of parameters");
|
||||
} else {
|
||||
return Handle_Return<Ret>()(boost::bind(f, Cast_Helper<Param1>()(params[0])));
|
||||
}
|
||||
}
|
||||
|
||||
template<typename Ret>
|
||||
Scripting_Object call_func(const boost::function<Ret ()> &f, const std::vector<Scripting_Object> ¶ms)
|
||||
{
|
||||
if (params.size() != 0)
|
||||
{
|
||||
throw std::range_error("Incorrect number of parameters");
|
||||
} else {
|
||||
return Handle_Return<Ret>()(f);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
class Function_Handler
|
||||
{
|
||||
public:
|
||||
virtual Scripting_Object operator()(const std::vector<Scripting_Object> ¶ms) = 0;
|
||||
};
|
||||
|
||||
template<typename Func>
|
||||
class Function_Handler_Impl : public Function_Handler
|
||||
{
|
||||
public:
|
||||
Function_Handler_Impl(const Func &f)
|
||||
: m_f(f)
|
||||
{
|
||||
}
|
||||
|
||||
virtual Scripting_Object operator()(const std::vector<Scripting_Object> ¶ms)
|
||||
{
|
||||
return call_func(m_f, params);
|
||||
}
|
||||
|
||||
private:
|
||||
Func m_f;
|
||||
};
|
||||
|
||||
std::vector<Scripting_Object> build_param_list(const Scripting_Object &so)
|
||||
{
|
||||
std::vector<Scripting_Object> sos;
|
||||
sos.push_back(so);
|
||||
return sos;
|
||||
}
|
||||
std::vector<Scripting_Object> build_param_list(const Scripting_Object &so1, const Scripting_Object &so2)
|
||||
{
|
||||
std::vector<Scripting_Object> sos;
|
||||
sos.push_back(so1);
|
||||
sos.push_back(so2);
|
||||
return sos;
|
||||
}
|
||||
std::vector<Scripting_Object> build_param_list(const Scripting_Object &so1, const Scripting_Object &so2, const Scripting_Object &so3)
|
||||
{
|
||||
std::vector<Scripting_Object> sos;
|
||||
sos.push_back(so1);
|
||||
sos.push_back(so2);
|
||||
sos.push_back(so3);
|
||||
return sos;
|
||||
}
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
|
121
typelesscpp/scripting_object.hpp
Normal file
121
typelesscpp/scripting_object.hpp
Normal file
@ -0,0 +1,121 @@
|
||||
#ifndef __scripting_object_hpp__
|
||||
#define __scripting_object_hpp__
|
||||
|
||||
#include "scripting_type_info.hpp"
|
||||
#include <boost/shared_ptr.hpp>
|
||||
#include <boost/any.hpp>
|
||||
|
||||
#include <boost/ref.hpp>
|
||||
|
||||
class Scripting_Object
|
||||
{
|
||||
public:
|
||||
template<typename T>
|
||||
explicit Scripting_Object(boost::shared_ptr<T> obj)
|
||||
: m_type_info(Get_Type_Info<T>()()), m_obj(obj), m_is_ref(false)
|
||||
{
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
explicit Scripting_Object(boost::reference_wrapper<T> obj)
|
||||
: m_type_info(Get_Type_Info<T>()()), m_obj(obj), m_is_ref(true)
|
||||
{
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
explicit Scripting_Object(const T& t)
|
||||
: m_type_info(Get_Type_Info<T>()()), m_obj(boost::shared_ptr<T>(new T(t))), m_is_ref(false)
|
||||
{
|
||||
}
|
||||
|
||||
Scripting_Object(const Scripting_Object &t_so)
|
||||
: m_type_info(t_so.m_type_info), m_obj(t_so.m_obj), m_is_ref(t_so.m_is_ref)
|
||||
{
|
||||
}
|
||||
|
||||
Scripting_Object()
|
||||
: m_type_info(Get_Type_Info<void>()()), m_is_ref(false)
|
||||
{
|
||||
}
|
||||
|
||||
const Type_Info &get_type_info()
|
||||
{
|
||||
return m_type_info;
|
||||
}
|
||||
|
||||
boost::any get() const
|
||||
{
|
||||
return m_obj;
|
||||
}
|
||||
|
||||
bool is_ref() const
|
||||
{
|
||||
return m_is_ref;
|
||||
}
|
||||
|
||||
private:
|
||||
Type_Info m_type_info;
|
||||
boost::any m_obj;
|
||||
bool m_is_ref;
|
||||
};
|
||||
|
||||
|
||||
//cast_help specializations
|
||||
template<typename Result>
|
||||
struct Cast_Helper
|
||||
{
|
||||
typename boost::reference_wrapper<typename boost::add_const<Result>::type > operator()(Scripting_Object ob)
|
||||
{
|
||||
if (ob.is_ref())
|
||||
{
|
||||
return boost::cref((boost::any_cast<boost::reference_wrapper<Result> >(ob.get())).get());
|
||||
} else {
|
||||
return boost::cref(*(boost::any_cast<boost::shared_ptr<Result> >(ob.get())));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Result>
|
||||
struct Cast_Helper<const Result &>
|
||||
{
|
||||
typename boost::reference_wrapper<typename boost::add_const<Result>::type > operator()(Scripting_Object ob)
|
||||
{
|
||||
if (ob.is_ref())
|
||||
{
|
||||
return boost::cref((boost::any_cast<boost::reference_wrapper<Result> >(ob.get())).get());
|
||||
} else {
|
||||
return boost::cref(*(boost::any_cast<boost::shared_ptr<Result> >(ob.get())));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Result>
|
||||
struct Cast_Helper<Result *>
|
||||
{
|
||||
Result *operator()(Scripting_Object ob)
|
||||
{
|
||||
if (ob.is_ref())
|
||||
{
|
||||
return (boost::any_cast<boost::reference_wrapper<Result> >(ob.get())).get_pointer();
|
||||
} else {
|
||||
return (boost::any_cast<boost::shared_ptr<Result> >(ob.get())).get();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Result>
|
||||
struct Cast_Helper<Result &>
|
||||
{
|
||||
typename boost::reference_wrapper<Result> operator()(Scripting_Object ob)
|
||||
{
|
||||
if (ob.is_ref())
|
||||
{
|
||||
return boost::any_cast<boost::reference_wrapper<Result> >(ob.get());
|
||||
} else {
|
||||
return boost::ref(*(boost::any_cast<boost::shared_ptr<Result> >(ob.get())));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
|
64
typelesscpp/scripting_system.hpp
Normal file
64
typelesscpp/scripting_system.hpp
Normal file
@ -0,0 +1,64 @@
|
||||
#ifndef __scripting_system_hpp__
|
||||
#define __scripting_system_hpp__
|
||||
|
||||
#include <typeinfo>
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <boost/shared_ptr.hpp>
|
||||
#include <stdexcept>
|
||||
#include <vector>
|
||||
|
||||
#include "scripting_object.hpp"
|
||||
#include "scripting_functions.hpp"
|
||||
#include "scripting_constructors.hpp"
|
||||
|
||||
class Scripting_System
|
||||
{
|
||||
public:
|
||||
template<typename Function>
|
||||
void register_function(const Function &func, const std::string &name)
|
||||
{
|
||||
m_functions.insert(std::make_pair(name, boost::shared_ptr<Function_Handler>(new Function_Handler_Impl<Function>(func))));
|
||||
}
|
||||
|
||||
template<typename Class>
|
||||
void add_object(const std::string &name, const Class &obj)
|
||||
{
|
||||
m_objects.insert(std::make_pair(name, Scripting_Object(obj)));
|
||||
}
|
||||
|
||||
Scripting_Object get_object(const std::string &name) const
|
||||
{
|
||||
std::map<std::string, Scripting_Object>::const_iterator itr = m_objects.find(name);
|
||||
if (itr != m_objects.end())
|
||||
{
|
||||
return itr->second;
|
||||
} else {
|
||||
throw std::range_error("Object not known: " + name);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename Type>
|
||||
void register_type(const std::string &name)
|
||||
{
|
||||
m_types.insert(std::make_pair(name, &typeid(Type)));
|
||||
}
|
||||
|
||||
boost::shared_ptr<Function_Handler> get_function(const std::string &t_name)
|
||||
{
|
||||
std::map<std::string, boost::shared_ptr<Function_Handler> >::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);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
std::map<std::string, Scripting_Object > m_objects;
|
||||
std::map<std::string, const std::type_info *> m_types;
|
||||
std::map<std::string, boost::shared_ptr<Function_Handler> > m_functions;
|
||||
};
|
||||
|
||||
#endif
|
36
typelesscpp/scripting_type_info.hpp
Normal file
36
typelesscpp/scripting_type_info.hpp
Normal file
@ -0,0 +1,36 @@
|
||||
#ifndef __scripting_type_info_hpp__
|
||||
#define __scripting_type_info_hpp__
|
||||
|
||||
#include <boost/type_traits.hpp>
|
||||
|
||||
struct Type_Info
|
||||
{
|
||||
Type_Info(bool t_is_const, bool t_is_reference, bool t_is_pointer, bool t_is_void,
|
||||
const std::type_info *t_ti, const std::type_info *t_bareti)
|
||||
: m_is_const(t_is_const), m_is_reference(t_is_reference), m_is_pointer(t_is_pointer),
|
||||
m_type_info(t_ti), m_bare_type_info(t_bareti)
|
||||
{
|
||||
}
|
||||
|
||||
bool m_is_const;
|
||||
bool m_is_reference;
|
||||
bool m_is_pointer;
|
||||
bool m_is_void;
|
||||
const std::type_info *m_type_info;
|
||||
const std::type_info *m_bare_type_info;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct Get_Type_Info
|
||||
{
|
||||
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(T),
|
||||
&typeid(typename boost::remove_const<typename boost::remove_pointer<typename boost::remove_reference<T>::type>::type>::type));
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
101
typelesscpp/test.cpp
Normal file
101
typelesscpp/test.cpp
Normal file
@ -0,0 +1,101 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <boost/shared_ptr.hpp>
|
||||
#include <boost/function.hpp>
|
||||
|
||||
#include "scripting_system.hpp"
|
||||
|
||||
struct Test
|
||||
{
|
||||
Test()
|
||||
: md(0)
|
||||
{
|
||||
}
|
||||
|
||||
Test(const std::string &s)
|
||||
: message(s), md(0)
|
||||
{
|
||||
std::cout << "Test class constructed with value: " << s << std::endl;
|
||||
}
|
||||
|
||||
void show_message()
|
||||
{
|
||||
std::cout << "Constructed Message: " << message << std::endl;
|
||||
}
|
||||
|
||||
std::string &get_message()
|
||||
{
|
||||
return message;
|
||||
}
|
||||
|
||||
int method(double d)
|
||||
{
|
||||
md += d;
|
||||
std::cout << "Method called " << md << std::endl;
|
||||
return int(md);
|
||||
}
|
||||
|
||||
std::string message;
|
||||
double md;
|
||||
};
|
||||
|
||||
std::string testprint(const std::string &p)
|
||||
{
|
||||
std::cout << p << std::endl;
|
||||
return p;
|
||||
}
|
||||
|
||||
void print()
|
||||
{
|
||||
std::cout << "Test void function succeeded" << std::endl;
|
||||
}
|
||||
|
||||
|
||||
//Test main
|
||||
int main()
|
||||
{
|
||||
Scripting_System 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");
|
||||
ss.register_function(boost::function<void ()>(&print), "voidfunc");
|
||||
ss.register_function(build_constructor<Test, const std::string &>(), "Test");
|
||||
ss.register_function(boost::function<void (Test*)>(&Test::show_message), "show_message");
|
||||
ss.register_function(boost::function<std::string &(Test*)>(&Test::get_message), "get_message");
|
||||
|
||||
|
||||
ss.add_object("testobj", boost::shared_ptr<Test>(new Test()));
|
||||
ss.add_object("d", boost::shared_ptr<double>(new double(10.2)));
|
||||
ss.add_object("str", std::string("Hello World"));
|
||||
|
||||
std::vector<Scripting_Object> sos;
|
||||
sos.push_back(ss.get_object("testobj"));
|
||||
sos.push_back(ss.get_object("d"));
|
||||
|
||||
boost::shared_ptr<Function_Handler> method1(ss.get_function("method"));
|
||||
(*method1)(sos);
|
||||
(*method1)(sos);
|
||||
Scripting_Object o = (*method1)(sos);
|
||||
|
||||
(*ss.get_function("print"))(build_param_list(ss.get_object("str")));
|
||||
|
||||
//Add new dynamically created object from registered "Test" constructor
|
||||
ss.add_object("testobj2", (*ss.get_function("Test"))(build_param_list(Scripting_Object(std::string("Yo")))));
|
||||
|
||||
std::cout << Cast_Helper<int>()(o) << std::endl;
|
||||
|
||||
(*ss.get_function("voidfunc"))(std::vector<Scripting_Object>());
|
||||
|
||||
std::vector<Scripting_Object> sos3;
|
||||
sos3.push_back(ss.get_object("testobj2"));
|
||||
(*ss.get_function("show_message"))(sos3);
|
||||
|
||||
Scripting_Object stringref = (*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);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user