Clean up chaiscript namespace by adding the "detail" namespace for internal functions/classes
This commit is contained in:
parent
a3d4b6698a
commit
6775863415
@ -119,7 +119,7 @@ namespace chaiscript
|
|||||||
boost::shared_ptr<Data> get(Boxed_Value::Void_Type)
|
boost::shared_ptr<Data> get(Boxed_Value::Void_Type)
|
||||||
{
|
{
|
||||||
return boost::shared_ptr<Data> (new Data(
|
return boost::shared_ptr<Data> (new Data(
|
||||||
Get_Type_Info<void>::get(),
|
detail::Get_Type_Info<void>::get(),
|
||||||
boost::any(),
|
boost::any(),
|
||||||
false)
|
false)
|
||||||
);
|
);
|
||||||
@ -135,7 +135,7 @@ namespace chaiscript
|
|||||||
boost::shared_ptr<Data> get(const boost::shared_ptr<T> &obj)
|
boost::shared_ptr<Data> get(const boost::shared_ptr<T> &obj)
|
||||||
{
|
{
|
||||||
boost::shared_ptr<Data> data(new Data(
|
boost::shared_ptr<Data> data(new Data(
|
||||||
Get_Type_Info<T>::get(),
|
detail::Get_Type_Info<T>::get(),
|
||||||
boost::any(obj),
|
boost::any(obj),
|
||||||
false,
|
false,
|
||||||
boost::shared_ptr<Data::Shared_Ptr_Proxy>(new Data::Shared_Ptr_Proxy_Impl<T>()))
|
boost::shared_ptr<Data::Shared_Ptr_Proxy>(new Data::Shared_Ptr_Proxy_Impl<T>()))
|
||||||
@ -164,7 +164,7 @@ namespace chaiscript
|
|||||||
boost::shared_ptr<Data> get(boost::reference_wrapper<T> obj)
|
boost::shared_ptr<Data> get(boost::reference_wrapper<T> obj)
|
||||||
{
|
{
|
||||||
boost::shared_ptr<Data> data(new Data(
|
boost::shared_ptr<Data> data(new Data(
|
||||||
Get_Type_Info<T>::get(),
|
detail::Get_Type_Info<T>::get(),
|
||||||
boost::any(obj),
|
boost::any(obj),
|
||||||
true)
|
true)
|
||||||
);
|
);
|
||||||
@ -192,7 +192,7 @@ namespace chaiscript
|
|||||||
boost::shared_ptr<Data> get(const T& t)
|
boost::shared_ptr<Data> get(const T& t)
|
||||||
{
|
{
|
||||||
boost::shared_ptr<Data> data(new Data(
|
boost::shared_ptr<Data> data(new Data(
|
||||||
Get_Type_Info<T>::get(),
|
detail::Get_Type_Info<T>::get(),
|
||||||
boost::any(boost::shared_ptr<T>(new T(t))),
|
boost::any(boost::shared_ptr<T>(new T(t))),
|
||||||
false,
|
false,
|
||||||
boost::shared_ptr<Data::Shared_Ptr_Proxy>(new Data::Shared_Ptr_Proxy_Impl<T>()))
|
boost::shared_ptr<Data::Shared_Ptr_Proxy>(new Data::Shared_Ptr_Proxy_Impl<T>()))
|
||||||
@ -331,175 +331,178 @@ namespace chaiscript
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
// Cast_Helper helper classes
|
namespace detail
|
||||||
|
{
|
||||||
|
// Cast_Helper helper classes
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generic Cast_Helper, for casting to any type
|
* Generic Cast_Helper, for casting to any type
|
||||||
*/
|
*/
|
||||||
template<typename Result>
|
template<typename Result>
|
||||||
struct Cast_Helper
|
struct Cast_Helper
|
||||||
{
|
|
||||||
typedef typename boost::reference_wrapper<typename boost::add_const<Result>::type > Result_Type;
|
|
||||||
|
|
||||||
static Result_Type cast(const Boxed_Value &ob)
|
|
||||||
{
|
{
|
||||||
if (ob.is_ref())
|
typedef typename boost::reference_wrapper<typename boost::add_const<Result>::type > Result_Type;
|
||||||
|
|
||||||
|
static Result_Type cast(const Boxed_Value &ob)
|
||||||
{
|
{
|
||||||
if (!ob.get_type_info().m_is_const)
|
if (ob.is_ref())
|
||||||
{
|
{
|
||||||
return boost::cref((boost::any_cast<boost::reference_wrapper<Result> >(ob.get())).get());
|
if (!ob.get_type_info().m_is_const)
|
||||||
|
{
|
||||||
|
return boost::cref((boost::any_cast<boost::reference_wrapper<Result> >(ob.get())).get());
|
||||||
|
} else {
|
||||||
|
return boost::any_cast<boost::reference_wrapper<const Result> >(ob.get());
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
return boost::any_cast<boost::reference_wrapper<const Result> >(ob.get());
|
return boost::cref(*(boost::any_cast<boost::shared_ptr<Result> >(ob.get())));
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
return boost::cref(*(boost::any_cast<boost::shared_ptr<Result> >(ob.get())));
|
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Cast_Helper for casting to a const & type
|
* Cast_Helper for casting to a const & type
|
||||||
*/
|
*/
|
||||||
template<typename Result>
|
template<typename Result>
|
||||||
struct Cast_Helper<const Result &>
|
struct Cast_Helper<const Result &>
|
||||||
{
|
|
||||||
typedef typename boost::reference_wrapper<typename boost::add_const<Result>::type > Result_Type;
|
|
||||||
|
|
||||||
static Result_Type cast(const Boxed_Value &ob)
|
|
||||||
{
|
{
|
||||||
if (ob.is_ref())
|
typedef typename boost::reference_wrapper<typename boost::add_const<Result>::type > Result_Type;
|
||||||
|
|
||||||
|
static Result_Type cast(const Boxed_Value &ob)
|
||||||
{
|
{
|
||||||
if (!ob.get_type_info().m_is_const)
|
if (ob.is_ref())
|
||||||
{
|
{
|
||||||
return boost::cref((boost::any_cast<boost::reference_wrapper<Result> >(ob.get())).get());
|
if (!ob.get_type_info().m_is_const)
|
||||||
|
{
|
||||||
|
return boost::cref((boost::any_cast<boost::reference_wrapper<Result> >(ob.get())).get());
|
||||||
|
} else {
|
||||||
|
return boost::any_cast<boost::reference_wrapper<const Result> >(ob.get());
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
return boost::any_cast<boost::reference_wrapper<const Result> >(ob.get());
|
return boost::cref(*(boost::any_cast<boost::shared_ptr<Result> >(ob.get())));
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
return boost::cref(*(boost::any_cast<boost::shared_ptr<Result> >(ob.get())));
|
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Cast_Helper for casting to a const * type
|
* Cast_Helper for casting to a const * type
|
||||||
*/
|
*/
|
||||||
template<typename Result>
|
template<typename Result>
|
||||||
struct Cast_Helper<const Result *>
|
struct Cast_Helper<const Result *>
|
||||||
{
|
|
||||||
typedef const Result * Result_Type;
|
|
||||||
|
|
||||||
static Result_Type cast(const Boxed_Value &ob)
|
|
||||||
{
|
{
|
||||||
if (ob.is_ref())
|
typedef const Result * Result_Type;
|
||||||
|
|
||||||
|
static Result_Type cast(const Boxed_Value &ob)
|
||||||
{
|
{
|
||||||
if (!ob.get_type_info().m_is_const)
|
if (ob.is_ref())
|
||||||
|
{
|
||||||
|
if (!ob.get_type_info().m_is_const)
|
||||||
|
{
|
||||||
|
return (boost::any_cast<boost::reference_wrapper<Result> >(ob.get())).get_pointer();
|
||||||
|
} else {
|
||||||
|
return (boost::any_cast<boost::reference_wrapper<const Result> >(ob.get())).get_pointer();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return (boost::any_cast<boost::shared_ptr<Result> >(ob.get())).get();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cast_Helper for casting to a * type
|
||||||
|
*/
|
||||||
|
template<typename Result>
|
||||||
|
struct Cast_Helper<Result *>
|
||||||
|
{
|
||||||
|
typedef Result * Result_Type;
|
||||||
|
|
||||||
|
static Result_Type cast(const Boxed_Value &ob)
|
||||||
|
{
|
||||||
|
if (ob.is_ref())
|
||||||
{
|
{
|
||||||
return (boost::any_cast<boost::reference_wrapper<Result> >(ob.get())).get_pointer();
|
return (boost::any_cast<boost::reference_wrapper<Result> >(ob.get())).get_pointer();
|
||||||
} else {
|
} else {
|
||||||
return (boost::any_cast<boost::reference_wrapper<const Result> >(ob.get())).get_pointer();
|
return (boost::any_cast<boost::shared_ptr<Result> >(ob.get())).get();
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
return (boost::any_cast<boost::shared_ptr<Result> >(ob.get())).get();
|
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Cast_Helper for casting to a * type
|
* Cast_Helper for casting to a & type
|
||||||
*/
|
*/
|
||||||
template<typename Result>
|
template<typename Result>
|
||||||
struct Cast_Helper<Result *>
|
struct Cast_Helper<Result &>
|
||||||
{
|
|
||||||
typedef Result * Result_Type;
|
|
||||||
|
|
||||||
static Result_Type cast(const Boxed_Value &ob)
|
|
||||||
{
|
{
|
||||||
if (ob.is_ref())
|
typedef typename boost::reference_wrapper<Result> Result_Type;
|
||||||
|
|
||||||
|
static Result_Type cast(const Boxed_Value &ob)
|
||||||
{
|
{
|
||||||
return (boost::any_cast<boost::reference_wrapper<Result> >(ob.get())).get_pointer();
|
if (ob.is_ref())
|
||||||
} else {
|
{
|
||||||
return (boost::any_cast<boost::shared_ptr<Result> >(ob.get())).get();
|
return boost::any_cast<boost::reference_wrapper<Result> >(ob.get());
|
||||||
|
} else {
|
||||||
|
return boost::ref(*(boost::any_cast<boost::shared_ptr<Result> >(ob.get())));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Cast_Helper for casting to a & type
|
* Cast_Helper for casting to a boost::shared_ptr<> type
|
||||||
*/
|
*/
|
||||||
template<typename Result>
|
template<typename Result>
|
||||||
struct Cast_Helper<Result &>
|
struct Cast_Helper<typename boost::shared_ptr<Result> >
|
||||||
{
|
|
||||||
typedef typename boost::reference_wrapper<Result> Result_Type;
|
|
||||||
|
|
||||||
static Result_Type cast(const Boxed_Value &ob)
|
|
||||||
{
|
{
|
||||||
if (ob.is_ref())
|
typedef typename boost::shared_ptr<Result> Result_Type;
|
||||||
|
|
||||||
|
static Result_Type cast(const Boxed_Value &ob)
|
||||||
{
|
{
|
||||||
return boost::any_cast<boost::reference_wrapper<Result> >(ob.get());
|
return boost::any_cast<boost::shared_ptr<Result> >(ob.get());
|
||||||
} else {
|
|
||||||
return boost::ref(*(boost::any_cast<boost::shared_ptr<Result> >(ob.get())));
|
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Cast_Helper for casting to a boost::shared_ptr<> type
|
* Cast_Helper for casting to a boost::shared_ptr<> type
|
||||||
*/
|
*/
|
||||||
template<typename Result>
|
template<typename Result>
|
||||||
struct Cast_Helper<typename boost::shared_ptr<Result> >
|
struct Cast_Helper<const boost::shared_ptr<Result> &>
|
||||||
{
|
|
||||||
typedef typename boost::shared_ptr<Result> Result_Type;
|
|
||||||
|
|
||||||
static Result_Type cast(const Boxed_Value &ob)
|
|
||||||
{
|
{
|
||||||
return boost::any_cast<boost::shared_ptr<Result> >(ob.get());
|
typedef typename boost::shared_ptr<Result> Result_Type;
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
static Result_Type cast(const Boxed_Value &ob)
|
||||||
* Cast_Helper for casting to a boost::shared_ptr<> type
|
{
|
||||||
*/
|
return boost::any_cast<boost::shared_ptr<Result> >(ob.get());
|
||||||
template<typename Result>
|
}
|
||||||
struct Cast_Helper<const boost::shared_ptr<Result> &>
|
};
|
||||||
{
|
|
||||||
typedef typename boost::shared_ptr<Result> Result_Type;
|
|
||||||
|
|
||||||
static Result_Type cast(const Boxed_Value &ob)
|
/**
|
||||||
|
* Cast_Helper for casting to a Boxed_Value type
|
||||||
|
*/
|
||||||
|
template<>
|
||||||
|
struct Cast_Helper<Boxed_Value>
|
||||||
{
|
{
|
||||||
return boost::any_cast<boost::shared_ptr<Result> >(ob.get());
|
typedef const Boxed_Value & Result_Type;
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
|
static Result_Type cast(const Boxed_Value &ob)
|
||||||
|
{
|
||||||
|
return ob;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
/**
|
* Cast_Helper for casting to a const Boxed_Value & type
|
||||||
* Cast_Helper for casting to a Boxed_Value type
|
*/
|
||||||
*/
|
template<>
|
||||||
template<>
|
struct Cast_Helper<const Boxed_Value &>
|
||||||
struct Cast_Helper<Boxed_Value>
|
|
||||||
{
|
|
||||||
typedef const Boxed_Value & Result_Type;
|
|
||||||
|
|
||||||
static Result_Type cast(const Boxed_Value &ob)
|
|
||||||
{
|
{
|
||||||
return ob;
|
typedef const Boxed_Value & Result_Type;
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
static Result_Type cast(const Boxed_Value &ob)
|
||||||
* Cast_Helper for casting to a const Boxed_Value & type
|
{
|
||||||
*/
|
return ob;
|
||||||
template<>
|
}
|
||||||
struct Cast_Helper<const Boxed_Value &>
|
};
|
||||||
{
|
}
|
||||||
typedef const Boxed_Value & Result_Type;
|
|
||||||
|
|
||||||
static Result_Type cast(const Boxed_Value &ob)
|
|
||||||
{
|
|
||||||
return ob;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* class that is thrown in the event of a bad_boxed_cast. That is,
|
* class that is thrown in the event of a bad_boxed_cast. That is,
|
||||||
@ -537,10 +540,10 @@ namespace chaiscript
|
|||||||
* int &i = boxed_cast<int &>(boxedvalue);
|
* int &i = boxed_cast<int &>(boxedvalue);
|
||||||
*/
|
*/
|
||||||
template<typename Type>
|
template<typename Type>
|
||||||
typename Cast_Helper<Type>::Result_Type boxed_cast(const Boxed_Value &bv)
|
typename detail::Cast_Helper<Type>::Result_Type boxed_cast(const Boxed_Value &bv)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
return Cast_Helper<Type>::cast(bv);
|
return detail::Cast_Helper<Type>::cast(bv);
|
||||||
} catch (const boost::bad_any_cast &) {
|
} catch (const boost::bad_any_cast &) {
|
||||||
throw bad_boxed_cast(bv.get_type_info(), typeid(Type));
|
throw bad_boxed_cast(bv.get_type_info(), typeid(Type));
|
||||||
}
|
}
|
||||||
@ -676,19 +679,22 @@ namespace chaiscript
|
|||||||
bool m_isfloat;
|
bool m_isfloat;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
namespace detail
|
||||||
* Cast_Helper for converting from Boxed_Value to Boxed_POD_Value
|
{
|
||||||
*/
|
/**
|
||||||
template<>
|
* Cast_Helper for converting from Boxed_Value to Boxed_POD_Value
|
||||||
struct Cast_Helper<Boxed_POD_Value>
|
*/
|
||||||
{
|
template<>
|
||||||
typedef Boxed_POD_Value Result_Type;
|
struct Cast_Helper<Boxed_POD_Value>
|
||||||
|
|
||||||
static Result_Type cast(const Boxed_Value &ob)
|
|
||||||
{
|
{
|
||||||
return Boxed_POD_Value(ob);
|
typedef Boxed_POD_Value Result_Type;
|
||||||
}
|
|
||||||
};
|
static Result_Type cast(const Boxed_Value &ob)
|
||||||
|
{
|
||||||
|
return Boxed_POD_Value(ob);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
Boxed_Value var(T t)
|
Boxed_Value var(T t)
|
||||||
@ -697,7 +703,7 @@ namespace chaiscript
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return true if the two Boxed_Value's share the same internal type
|
* Return true if the two Boxed_Values share the same internal type
|
||||||
*/
|
*/
|
||||||
static bool type_match(Boxed_Value l, Boxed_Value r)
|
static bool type_match(Boxed_Value l, Boxed_Value r)
|
||||||
{
|
{
|
||||||
|
@ -4,13 +4,6 @@
|
|||||||
// and Jason Turner (lefticus@gmail.com)
|
// and Jason Turner (lefticus@gmail.com)
|
||||||
// http://www.chaiscript.com
|
// http://www.chaiscript.com
|
||||||
|
|
||||||
#include <boost/preprocessor.hpp>
|
|
||||||
|
|
||||||
#define addparam(z,n,text) params.push_back(boost::is_reference<Param ## n>::value?Boxed_Value(boost::ref(BOOST_PP_CAT(p, n))):Boxed_Value(BOOST_PP_CAT(p, n) ));
|
|
||||||
#define curry(z,n,text) BOOST_PP_CAT(_, BOOST_PP_INC(n))
|
|
||||||
|
|
||||||
|
|
||||||
#ifndef BOOST_PP_IS_ITERATING
|
|
||||||
#ifndef __function_call_hpp__
|
#ifndef __function_call_hpp__
|
||||||
#define __function_call_hpp__
|
#define __function_call_hpp__
|
||||||
|
|
||||||
@ -20,50 +13,7 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include "proxy_functions.hpp"
|
#include "proxy_functions.hpp"
|
||||||
|
#include "function_call_detail.hpp"
|
||||||
namespace chaiscript
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* Internal helper class for handling the return
|
|
||||||
* value of a build_function_caller
|
|
||||||
*/
|
|
||||||
template<typename Ret>
|
|
||||||
class Function_Caller_Ret
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
Function_Caller_Ret()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
Ret call(const std::vector<std::pair<std::string, Proxy_Function > > &t_funcs,
|
|
||||||
const std::vector<Boxed_Value> ¶ms)
|
|
||||||
{
|
|
||||||
return boxed_cast<Ret>(dispatch(t_funcs, params));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Specialization for void return types
|
|
||||||
*/
|
|
||||||
template<>
|
|
||||||
class Function_Caller_Ret<void>
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
Function_Caller_Ret()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
void call(const std::vector<std::pair<std::string, Proxy_Function > > &t_funcs,
|
|
||||||
const std::vector<Boxed_Value> ¶ms)
|
|
||||||
{
|
|
||||||
dispatch(t_funcs, params);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#define BOOST_PP_ITERATION_LIMITS ( 0, 9 )
|
|
||||||
#define BOOST_PP_FILENAME_1 <chaiscript/dispatchkit/function_call.hpp>
|
|
||||||
#include BOOST_PP_ITERATE()
|
|
||||||
|
|
||||||
namespace chaiscript
|
namespace chaiscript
|
||||||
{
|
{
|
||||||
@ -118,39 +68,5 @@ namespace chaiscript
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# endif
|
|
||||||
#else
|
|
||||||
# define n BOOST_PP_ITERATION()
|
|
||||||
|
|
||||||
namespace chaiscript
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* used internally for unwrapping a function call's types
|
|
||||||
*/
|
|
||||||
template<typename Ret BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, typename Param) >
|
|
||||||
Ret function_caller(const std::vector<std::pair<std::string, Proxy_Function > > &funcs
|
|
||||||
BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_BINARY_PARAMS(n, Param, p) )
|
|
||||||
{
|
|
||||||
std::vector<Boxed_Value> params;
|
|
||||||
|
|
||||||
BOOST_PP_REPEAT(n, addparam, ~)
|
|
||||||
|
|
||||||
return Function_Caller_Ret<Ret>().call(funcs, params);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* used internally for unwrapping a function call's types
|
|
||||||
*/
|
|
||||||
template<typename Ret BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, typename Param) >
|
|
||||||
boost::function<Ret (BOOST_PP_ENUM_PARAMS(n, Param)) >
|
|
||||||
build_function_caller_helper(Ret (BOOST_PP_ENUM_PARAMS(n, Param)), const std::vector<std::pair<std::string, Proxy_Function> > &funcs)
|
|
||||||
{
|
|
||||||
return boost::bind(&function_caller<Ret BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, Param)>, funcs
|
|
||||||
BOOST_PP_ENUM_TRAILING(n, curry, ~));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -26,7 +26,7 @@ namespace chaiscript
|
|||||||
Proxy_Function constructor()
|
Proxy_Function constructor()
|
||||||
{
|
{
|
||||||
T *f = 0;
|
T *f = 0;
|
||||||
return (build_constructor_(f));
|
return (detail::build_constructor_(f));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -35,28 +35,31 @@ namespace chaiscript
|
|||||||
|
|
||||||
namespace chaiscript
|
namespace chaiscript
|
||||||
{
|
{
|
||||||
/**
|
namespace detail
|
||||||
* A constructor function, used for creating a new object
|
{
|
||||||
* of a given type with a given set of params
|
/**
|
||||||
*/
|
* A constructor function, used for creating a new object
|
||||||
template<typename Class BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, typename Param) >
|
* of a given type with a given set of params
|
||||||
boost::shared_ptr<Class> constructor_( BOOST_PP_ENUM_BINARY_PARAMS(n, Param, p) )
|
*/
|
||||||
{
|
template<typename Class BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, typename Param) >
|
||||||
return boost::shared_ptr<Class>(new Class( BOOST_PP_ENUM_PARAMS(n, p) ));
|
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) ));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Helper function for build a constructor function
|
* Helper function for build a constructor function
|
||||||
* example:
|
* example:
|
||||||
* dispatchengine.register_function(build_constructor<MyClass, int, const std::string&>, "MyClass");
|
* dispatchengine.register_function(build_constructor<MyClass, int, const std::string&>, "MyClass");
|
||||||
* \todo See if it is possible to make this not be a variadic function
|
* \todo See if it is possible to make this not be a variadic function
|
||||||
*/
|
*/
|
||||||
template<typename Class BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, typename Param) >
|
template<typename Class BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, typename Param) >
|
||||||
Proxy_Function build_constructor_(Class (*)(BOOST_PP_ENUM_PARAMS(n, Param)))
|
Proxy_Function build_constructor_(Class (*)(BOOST_PP_ENUM_PARAMS(n, Param)))
|
||||||
{
|
{
|
||||||
typedef boost::shared_ptr<Class> (sig)(BOOST_PP_ENUM_PARAMS(n, Param));
|
typedef boost::shared_ptr<Class> (sig)(BOOST_PP_ENUM_PARAMS(n, Param));
|
||||||
return Proxy_Function(new Proxy_Function_Impl<sig>(boost::function<sig>(&(constructor_<Class BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, Param)>))));
|
return Proxy_Function(new Proxy_Function_Impl<sig>(boost::function<sig>(&(constructor_<Class BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, Param)>))));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -4,109 +4,22 @@
|
|||||||
// and Jason Turner (lefticus@gmail.com)
|
// and Jason Turner (lefticus@gmail.com)
|
||||||
// http://www.chaiscript.com
|
// http://www.chaiscript.com
|
||||||
|
|
||||||
#include <boost/preprocessor.hpp>
|
|
||||||
|
|
||||||
#define gettypeinfo(z,n,text) ti.push_back(Get_Type_Info<Param ## n>::get());
|
|
||||||
#define casthelper(z,n,text) ,chaiscript::boxed_cast< Param ## n >(params[n])
|
|
||||||
#define comparetype(z,n,text) && ((Get_Type_Info<Param ## n>::get() == params[n].get_type_info()))
|
|
||||||
#define trycast(z,n,text) chaiscript::boxed_cast<Param ## n>(params[n]);
|
|
||||||
|
|
||||||
|
|
||||||
#ifndef BOOST_PP_IS_ITERATING
|
|
||||||
#ifndef __proxy_functions_hpp__
|
#ifndef __proxy_functions_hpp__
|
||||||
#define __proxy_functions_hpp__
|
#define __proxy_functions_hpp__
|
||||||
|
|
||||||
|
|
||||||
#include "boxed_value.hpp"
|
#include "boxed_value.hpp"
|
||||||
#include "type_info.hpp"
|
#include "type_info.hpp"
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <boost/function.hpp>
|
#include <boost/function.hpp>
|
||||||
#include <boost/bind.hpp>
|
|
||||||
#include <boost/lexical_cast.hpp>
|
|
||||||
|
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include "proxy_functions_detail.hpp"
|
||||||
|
|
||||||
namespace chaiscript
|
namespace chaiscript
|
||||||
{
|
{
|
||||||
/**
|
|
||||||
* Used internally for handling a return value from a Proxy_Function call
|
|
||||||
*/
|
|
||||||
template<typename Ret>
|
|
||||||
struct Handle_Return
|
|
||||||
{
|
|
||||||
Boxed_Value operator()(const boost::function<Ret ()> &f)
|
|
||||||
{
|
|
||||||
return Boxed_Value(f());
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
template<typename Ret>
|
|
||||||
struct Handle_Return<boost::shared_ptr<Ret> &>
|
|
||||||
{
|
|
||||||
Boxed_Value operator()(const boost::function<boost::shared_ptr<Ret> & ()> &f)
|
|
||||||
{
|
|
||||||
return Boxed_Value(f());
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
template<typename Ret>
|
|
||||||
struct Handle_Return<const boost::shared_ptr<Ret> &>
|
|
||||||
{
|
|
||||||
Boxed_Value operator()(const boost::function<const boost::shared_ptr<Ret> & ()> &f)
|
|
||||||
{
|
|
||||||
return Boxed_Value(f());
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Used internally for handling a return value from a Proxy_Function call
|
|
||||||
*/
|
|
||||||
template<typename Ret>
|
|
||||||
struct Handle_Return<Ret &>
|
|
||||||
{
|
|
||||||
Boxed_Value operator()(const boost::function<Ret &()> &f)
|
|
||||||
{
|
|
||||||
return Boxed_Value(boost::ref(f()));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Used internally for handling a return value from a Proxy_Function call
|
|
||||||
*/
|
|
||||||
template<>
|
|
||||||
struct Handle_Return<Boxed_Value>
|
|
||||||
{
|
|
||||||
Boxed_Value operator()(const boost::function<Boxed_Value ()> &f)
|
|
||||||
{
|
|
||||||
return f();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Used internally for handling a return value from a Proxy_Function call
|
|
||||||
*/
|
|
||||||
template<>
|
|
||||||
struct Handle_Return<Boxed_Value &>
|
|
||||||
{
|
|
||||||
Boxed_Value operator()(const boost::function<Boxed_Value &()> &f)
|
|
||||||
{
|
|
||||||
return f();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Used internally for handling a return value from a Proxy_Function call
|
|
||||||
*/
|
|
||||||
template<>
|
|
||||||
struct Handle_Return<void>
|
|
||||||
{
|
|
||||||
Boxed_Value operator()(const boost::function<void ()> &f)
|
|
||||||
{
|
|
||||||
f();
|
|
||||||
return Boxed_Value(Boxed_Value::Void_Type());
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Helper for building a list of parameters for calling a Proxy_Function
|
* Helper for building a list of parameters for calling a Proxy_Function
|
||||||
* it does automatic conversion to Boxed_Value types via operator<<
|
* it does automatic conversion to Boxed_Value types via operator<<
|
||||||
@ -138,30 +51,6 @@ namespace chaiscript
|
|||||||
std::vector<Boxed_Value> objects;
|
std::vector<Boxed_Value> objects;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
* Exception thrown when there is a mismatch in number of
|
|
||||||
* parameters during Proxy_Function execution
|
|
||||||
*/
|
|
||||||
struct arity_error : std::range_error
|
|
||||||
{
|
|
||||||
arity_error(int t_got, int t_expected)
|
|
||||||
: std::range_error("Function dispatch arity mismatch"),
|
|
||||||
got(t_got), expected(t_expected)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual ~arity_error() throw() {}
|
|
||||||
int got;
|
|
||||||
int expected;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#define BOOST_PP_ITERATION_LIMITS ( 0, 10 )
|
|
||||||
#define BOOST_PP_FILENAME_1 <chaiscript/dispatchkit/proxy_functions.hpp>
|
|
||||||
#include BOOST_PP_ITERATE()
|
|
||||||
|
|
||||||
namespace chaiscript
|
|
||||||
{
|
|
||||||
/**
|
/**
|
||||||
* Pure virtual base class for all Proxy_Function implementations
|
* Pure virtual base class for all Proxy_Function implementations
|
||||||
* Proxy_Functions are a type erasure of type safe C++
|
* Proxy_Functions are a type erasure of type safe C++
|
||||||
@ -321,16 +210,16 @@ namespace chaiscript
|
|||||||
{
|
{
|
||||||
std::vector<Type_Info> types;
|
std::vector<Type_Info> types;
|
||||||
|
|
||||||
types.push_back(Get_Type_Info<Boxed_Value>::get());
|
types.push_back(detail::Get_Type_Info<Boxed_Value>::get());
|
||||||
|
|
||||||
if (arity >= 0)
|
if (arity >= 0)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < arity; ++i)
|
for (int i = 0; i < arity; ++i)
|
||||||
{
|
{
|
||||||
types.push_back(Get_Type_Info<Boxed_Value>::get());
|
types.push_back(detail::Get_Type_Info<Boxed_Value>::get());
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
types.push_back(Get_Type_Info<std::vector<Boxed_Value> >::get());
|
types.push_back(detail::Get_Type_Info<std::vector<Boxed_Value> >::get());
|
||||||
}
|
}
|
||||||
|
|
||||||
return types;
|
return types;
|
||||||
@ -395,7 +284,7 @@ namespace chaiscript
|
|||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
while (barg != m_args.end()
|
while (barg != m_args.end()
|
||||||
&& !(barg->get_type_info() == Get_Type_Info<Placeholder_Object>::get()))
|
&& !(barg->get_type_info() == detail::Get_Type_Info<Placeholder_Object>::get()))
|
||||||
{
|
{
|
||||||
args.push_back(*barg);
|
args.push_back(*barg);
|
||||||
++barg;
|
++barg;
|
||||||
@ -408,7 +297,7 @@ namespace chaiscript
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (barg != m_args.end()
|
if (barg != m_args.end()
|
||||||
&& barg->get_type_info() == Get_Type_Info<Placeholder_Object>::get())
|
&& barg->get_type_info() == detail::Get_Type_Info<Placeholder_Object>::get())
|
||||||
{
|
{
|
||||||
++barg;
|
++barg;
|
||||||
}
|
}
|
||||||
@ -546,70 +435,5 @@ namespace chaiscript
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
# endif
|
|
||||||
#else
|
|
||||||
# define n BOOST_PP_ITERATION()
|
|
||||||
|
|
||||||
namespace chaiscript
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* Used by Proxy_Function_Impl to return a list of all param types
|
|
||||||
* it contains.
|
|
||||||
*/
|
|
||||||
template<typename Ret BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, typename Param) >
|
|
||||||
std::vector<Type_Info> build_param_type_list(Ret (*)(BOOST_PP_ENUM_PARAMS(n, Param)))
|
|
||||||
{
|
|
||||||
std::vector<Type_Info> ti;
|
|
||||||
ti.push_back(Get_Type_Info<Ret>::get());
|
|
||||||
|
|
||||||
BOOST_PP_REPEAT(n, gettypeinfo, ~)
|
|
||||||
|
|
||||||
return ti;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Used by Proxy_Function_Impl to perform typesafe execution of a function.
|
|
||||||
* The function attempts to unbox each paramter to the expected type.
|
|
||||||
* if any unboxing fails the execution of the function fails and
|
|
||||||
* the bad_boxed_cast is passed up to the caller.
|
|
||||||
*/
|
|
||||||
template<typename Ret BOOST_PP_COMMA_IF(n) 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 arity_error(params.size(), n);
|
|
||||||
} else {
|
|
||||||
return Handle_Return<Ret>()(boost::bind(f BOOST_PP_REPEAT(n, casthelper, ~)));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Used by Proxy_Function_Impl to determine if it is equivalent to another
|
|
||||||
* Proxy_Function_Impl object. This function is primarly used to prevent
|
|
||||||
* registration of two functions with the exact same signatures
|
|
||||||
*/
|
|
||||||
template<typename Ret BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, typename Param)>
|
|
||||||
bool compare_types(Ret (*)(BOOST_PP_ENUM_PARAMS(n, Param)),
|
|
||||||
const std::vector<Boxed_Value> ¶ms)
|
|
||||||
{
|
|
||||||
if (params.size() != n)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
} else {
|
|
||||||
bool val = true BOOST_PP_REPEAT(n, comparetype, ~);
|
|
||||||
if (val) return true;
|
|
||||||
|
|
||||||
try {
|
|
||||||
BOOST_PP_REPEAT(n, trycast, ~);
|
|
||||||
} catch (const bad_boxed_cast &) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -22,15 +22,17 @@ namespace chaiscript
|
|||||||
return Proxy_Function(new Proxy_Function_Impl<T>(f));
|
return Proxy_Function(new Proxy_Function_Impl<T>(f));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
namespace detail
|
||||||
/**
|
{
|
||||||
* Helper function for register_member function
|
/**
|
||||||
*/
|
* Helper function for register_member function
|
||||||
template<typename T, typename Class>
|
*/
|
||||||
T &get_member(T Class::* m, Class *obj)
|
template<typename T, typename Class>
|
||||||
{
|
T &get_member(T Class::* m, Class *obj)
|
||||||
return (obj->*m);
|
{
|
||||||
}
|
return (obj->*m);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Automatically create a get_member helper function for an object
|
* Automatically create a get_member helper function for an object
|
||||||
@ -40,7 +42,7 @@ namespace chaiscript
|
|||||||
template<typename T, typename Class>
|
template<typename T, typename Class>
|
||||||
Proxy_Function fun(T Class::* m)
|
Proxy_Function fun(T Class::* m)
|
||||||
{
|
{
|
||||||
return fun(boost::function<T& (Class *)>(boost::bind(&get_member<T, Class>, m, _1)));
|
return fun(boost::function<T& (Class *)>(boost::bind(&detail::get_member<T, Class>, m, _1)));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -77,57 +77,59 @@ namespace chaiscript
|
|||||||
bool m_is_unknown;
|
bool m_is_unknown;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
namespace detail
|
||||||
* Helper used to create a Type_Info object
|
{
|
||||||
*/
|
/**
|
||||||
template<typename T>
|
* Helper used to create a Type_Info object
|
||||||
struct Get_Type_Info
|
*/
|
||||||
{
|
template<typename T>
|
||||||
static Type_Info get()
|
struct Get_Type_Info
|
||||||
{
|
{
|
||||||
return Type_Info(boost::is_const<T>::value, boost::is_reference<T>::value, boost::is_pointer<T>::value,
|
static Type_Info get()
|
||||||
boost::is_void<T>::value,
|
{
|
||||||
&typeid(T),
|
return Type_Info(boost::is_const<T>::value, boost::is_reference<T>::value, boost::is_pointer<T>::value,
|
||||||
&typeid(typename boost::remove_const<typename boost::remove_pointer<typename boost::remove_reference<T>::type>::type>::type));
|
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>
|
template<typename T>
|
||||||
struct Get_Type_Info<boost::shared_ptr<T> >
|
struct Get_Type_Info<boost::shared_ptr<T> >
|
||||||
{
|
|
||||||
static Type_Info get()
|
|
||||||
{
|
{
|
||||||
return Type_Info(boost::is_const<T>::value, boost::is_reference<T>::value, boost::is_pointer<T>::value,
|
static Type_Info get()
|
||||||
boost::is_void<T>::value,
|
{
|
||||||
&typeid(boost::shared_ptr<T> ),
|
return Type_Info(boost::is_const<T>::value, boost::is_reference<T>::value, boost::is_pointer<T>::value,
|
||||||
&typeid(typename boost::remove_const<typename boost::remove_pointer<typename boost::remove_reference<T>::type>::type>::type));
|
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>
|
template<typename T>
|
||||||
struct Get_Type_Info<boost::reference_wrapper<T> >
|
struct Get_Type_Info<boost::reference_wrapper<T> >
|
||||||
{
|
|
||||||
static Type_Info get()
|
|
||||||
{
|
{
|
||||||
return Type_Info(boost::is_const<T>::value, boost::is_reference<T>::value, boost::is_pointer<T>::value,
|
static Type_Info get()
|
||||||
boost::is_void<T>::value,
|
{
|
||||||
&typeid(boost::reference_wrapper<T> ),
|
return Type_Info(boost::is_const<T>::value, boost::is_reference<T>::value, boost::is_pointer<T>::value,
|
||||||
&typeid(typename boost::remove_const<typename boost::remove_pointer<typename boost::remove_reference<T>::type>::type>::type));
|
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));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
Type_Info user_type(T)
|
Type_Info user_type(T)
|
||||||
{
|
{
|
||||||
return Get_Type_Info<T>::get();
|
return detail::Get_Type_Info<T>::get();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
Type_Info user_type()
|
Type_Info user_type()
|
||||||
{
|
{
|
||||||
return Get_Type_Info<T>::get();
|
return detail::Get_Type_Info<T>::get();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user