Moved typelesscpp into boxedcpp
This commit is contained in:
121
boxedcpp/boxed_value.hpp
Normal file
121
boxedcpp/boxed_value.hpp
Normal file
@@ -0,0 +1,121 @@
|
||||
#ifndef __boxed_value_hpp__
|
||||
#define __boxed_value_hpp__
|
||||
|
||||
#include "type_info.hpp"
|
||||
#include <boost/shared_ptr.hpp>
|
||||
#include <boost/any.hpp>
|
||||
|
||||
#include <boost/ref.hpp>
|
||||
|
||||
class Boxed_Value
|
||||
{
|
||||
public:
|
||||
template<typename T>
|
||||
explicit Boxed_Value(boost::shared_ptr<T> obj)
|
||||
: m_type_info(Get_Type_Info<T>()()), m_obj(obj), m_is_ref(false)
|
||||
{
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
explicit Boxed_Value(boost::reference_wrapper<T> obj)
|
||||
: m_type_info(Get_Type_Info<T>()()), m_obj(obj), m_is_ref(true)
|
||||
{
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
explicit Boxed_Value(const T& t)
|
||||
: m_type_info(Get_Type_Info<T>()()), m_obj(boost::shared_ptr<T>(new T(t))), m_is_ref(false)
|
||||
{
|
||||
}
|
||||
|
||||
Boxed_Value(const Boxed_Value &t_so)
|
||||
: m_type_info(t_so.m_type_info), m_obj(t_so.m_obj), m_is_ref(t_so.m_is_ref)
|
||||
{
|
||||
}
|
||||
|
||||
Boxed_Value()
|
||||
: 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()(Boxed_Value 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()(Boxed_Value 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()(Boxed_Value 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()(Boxed_Value 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
boxedcpp/boxedcpp.hpp
Normal file
64
boxedcpp/boxedcpp.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 "boxed_value.hpp"
|
||||
#include "proxy_functions.hpp"
|
||||
#include "proxy_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<Proxy_Function>(new Proxy_Function_Impl<Function>(func))));
|
||||
}
|
||||
|
||||
template<typename Class>
|
||||
void add_object(const std::string &name, const Class &obj)
|
||||
{
|
||||
m_objects.insert(std::make_pair(name, Boxed_Value(obj)));
|
||||
}
|
||||
|
||||
Boxed_Value get_object(const std::string &name) const
|
||||
{
|
||||
std::map<std::string, Boxed_Value>::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<Proxy_Function> get_function(const std::string &t_name)
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
};
|
||||
|
||||
#endif
|
46
boxedcpp/proxy_constructors.hpp
Normal file
46
boxedcpp/proxy_constructors.hpp
Normal file
@@ -0,0 +1,46 @@
|
||||
#include <boost/preprocessor.hpp>
|
||||
|
||||
#ifndef BOOST_PP_IS_ITERATING
|
||||
#ifndef __proxy_constructors_hpp__
|
||||
#define __proxy_constructors_hpp__
|
||||
|
||||
#include <boost/shared_ptr.hpp>
|
||||
#include <boost/bind.hpp>
|
||||
#include <boost/function.hpp>
|
||||
|
||||
template<typename Class>
|
||||
boost::shared_ptr<Class> constructor()
|
||||
{
|
||||
return boost::shared_ptr<Class>(new Class());
|
||||
}
|
||||
|
||||
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>)));
|
||||
}
|
||||
|
||||
#define BOOST_PP_ITERATION_LIMITS ( 1, 10 )
|
||||
#define BOOST_PP_FILENAME_1 "proxy_constructors.hpp"
|
||||
#include BOOST_PP_ITERATE()
|
||||
# endif
|
||||
#else
|
||||
# define n BOOST_PP_ITERATION()
|
||||
|
||||
|
||||
template<typename Class, BOOST_PP_ENUM_PARAMS(n, typename Param) >
|
||||
boost::shared_ptr<Class> constructor( BOOST_PP_ENUM_BINARY_PARAMS(n, Param, p) )
|
||||
{
|
||||
return boost::shared_ptr<Class>(new Class( BOOST_PP_ENUM_PARAMS(n, p) ));
|
||||
}
|
||||
|
||||
template<typename Class, BOOST_PP_ENUM_PARAMS(n, typename Param) >
|
||||
boost::function<boost::shared_ptr<Class> (BOOST_PP_ENUM_PARAMS(n, Param))> build_constructor()
|
||||
{
|
||||
typedef boost::shared_ptr<Class> (*func)(BOOST_PP_ENUM_PARAMS(n, Param));
|
||||
return boost::function<boost::shared_ptr<Class> (BOOST_PP_ENUM_PARAMS(n, Param))>(func(&(constructor<Class, BOOST_PP_ENUM_PARAMS(n, Param)>)));
|
||||
}
|
||||
|
||||
#endif
|
||||
|
161
boxedcpp/proxy_functions.hpp
Normal file
161
boxedcpp/proxy_functions.hpp
Normal file
@@ -0,0 +1,161 @@
|
||||
#include <boost/preprocessor.hpp>
|
||||
|
||||
#define gettypeinfo(z,n,text) ti.push_back(Get_Type_Info<Param ## n>()());
|
||||
#define casthelper(z,n,text) ,Cast_Helper<Param ## n>()(params[n])
|
||||
|
||||
|
||||
#ifndef BOOST_PP_IS_ITERATING
|
||||
#ifndef __proxy_functions_hpp__
|
||||
#define __proxy_functions_hpp__
|
||||
|
||||
#include "boxed_value.hpp"
|
||||
#include "type_info.hpp"
|
||||
#include <string>
|
||||
#include <boost/function.hpp>
|
||||
#include <boost/bind.hpp>
|
||||
#include <stdexcept>
|
||||
#include <vector>
|
||||
|
||||
// handle_return implementations
|
||||
template<typename Ret>
|
||||
struct Handle_Return
|
||||
{
|
||||
Boxed_Value operator()(const boost::function<Ret ()> &f)
|
||||
{
|
||||
return Boxed_Value(f());
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Ret>
|
||||
struct Handle_Return<Ret &>
|
||||
{
|
||||
Boxed_Value operator()(const boost::function<Ret &()> &f)
|
||||
{
|
||||
return Boxed_Value(boost::ref(f()));
|
||||
}
|
||||
};
|
||||
|
||||
template<>
|
||||
struct Handle_Return<void>
|
||||
{
|
||||
Boxed_Value operator()(const boost::function<void ()> &f)
|
||||
{
|
||||
f();
|
||||
return Boxed_Value();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
// Build param type list (variadic)
|
||||
template<typename Ret>
|
||||
std::vector<Type_Info> build_param_type_list(const boost::function<Ret ()> &f)
|
||||
{
|
||||
std::vector<Type_Info> ti;
|
||||
ti.push_back(Get_Type_Info<Ret>()());
|
||||
return ti;
|
||||
}
|
||||
|
||||
// call_func implementations (variadic)
|
||||
template<typename Ret>
|
||||
Boxed_Value call_func(const boost::function<Ret ()> &f, const std::vector<Boxed_Value> ¶ms)
|
||||
{
|
||||
if (params.size() != 0)
|
||||
{
|
||||
throw std::range_error("Incorrect number of parameters");
|
||||
} else {
|
||||
return Handle_Return<Ret>()(f);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
struct Param_List_Builder
|
||||
{
|
||||
Param_List_Builder &operator<<(const Boxed_Value &so)
|
||||
{
|
||||
objects.push_back(so);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
Param_List_Builder &operator<<(T t)
|
||||
{
|
||||
objects.push_back(Boxed_Value(t));
|
||||
return *this;
|
||||
}
|
||||
|
||||
operator const std::vector<Boxed_Value> &() const
|
||||
{
|
||||
return objects;
|
||||
}
|
||||
|
||||
std::vector<Boxed_Value> objects;
|
||||
|
||||
};
|
||||
|
||||
|
||||
#define BOOST_PP_ITERATION_LIMITS ( 1, 10 )
|
||||
#define BOOST_PP_FILENAME_1 "proxy_functions.hpp"
|
||||
#include BOOST_PP_ITERATE()
|
||||
|
||||
class Proxy_Function
|
||||
{
|
||||
public:
|
||||
virtual Boxed_Value operator()(const std::vector<Boxed_Value> ¶ms) = 0;
|
||||
virtual std::vector<Type_Info> get_param_types() = 0;
|
||||
|
||||
};
|
||||
|
||||
template<typename Func>
|
||||
class Proxy_Function_Impl : public Proxy_Function
|
||||
{
|
||||
public:
|
||||
Proxy_Function_Impl(const Func &f)
|
||||
: m_f(f)
|
||||
{
|
||||
}
|
||||
|
||||
virtual Boxed_Value operator()(const std::vector<Boxed_Value> ¶ms)
|
||||
{
|
||||
return call_func(m_f, params);
|
||||
}
|
||||
|
||||
virtual std::vector<Type_Info> get_param_types()
|
||||
{
|
||||
return build_param_type_list(m_f);
|
||||
}
|
||||
|
||||
private:
|
||||
Func m_f;
|
||||
};
|
||||
|
||||
|
||||
|
||||
# endif
|
||||
#else
|
||||
# define n BOOST_PP_ITERATION()
|
||||
|
||||
template<typename Ret, BOOST_PP_ENUM_PARAMS(n, typename Param) >
|
||||
std::vector<Type_Info> build_param_type_list(const boost::function<Ret (BOOST_PP_ENUM_PARAMS(n, Param))> &f)
|
||||
{
|
||||
std::vector<Type_Info> ti;
|
||||
ti.push_back(Get_Type_Info<Ret>()());
|
||||
|
||||
BOOST_PP_REPEAT(n, gettypeinfo, ~)
|
||||
|
||||
return ti;
|
||||
}
|
||||
|
||||
template<typename Ret, BOOST_PP_ENUM_PARAMS(n, typename Param)>
|
||||
Boxed_Value call_func(const boost::function<Ret (BOOST_PP_ENUM_PARAMS(n, Param))> &f,
|
||||
const std::vector<Boxed_Value> ¶ms)
|
||||
{
|
||||
if (params.size() != n)
|
||||
{
|
||||
throw std::range_error("Incorrect number of parameters");
|
||||
} else {
|
||||
return Handle_Return<Ret>()(boost::bind(f BOOST_PP_REPEAT(n, casthelper, ~)));
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
101
boxedcpp/test.cpp
Normal file
101
boxedcpp/test.cpp
Normal file
@@ -0,0 +1,101 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <boost/shared_ptr.hpp>
|
||||
#include <boost/function.hpp>
|
||||
|
||||
#include "boxedcpp.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<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"));
|
||||
(*method1)(sos);
|
||||
(*method1)(sos);
|
||||
Boxed_Value o = (*method1)(sos);
|
||||
|
||||
(*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")));
|
||||
|
||||
std::cout << Cast_Helper<int>()(o) << std::endl;
|
||||
|
||||
(*ss.get_function("voidfunc"))(std::vector<Boxed_Value>());
|
||||
|
||||
std::vector<Boxed_Value> sos3;
|
||||
sos3.push_back(ss.get_object("testobj2"));
|
||||
(*ss.get_function("show_message"))(sos3);
|
||||
|
||||
Boxed_Value 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);
|
||||
}
|
||||
|
36
boxedcpp/type_info.hpp
Normal file
36
boxedcpp/type_info.hpp
Normal file
@@ -0,0 +1,36 @@
|
||||
#ifndef __type_info_hpp__
|
||||
#define __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
|
Reference in New Issue
Block a user