Clean up chaiscript namespace by adding the "detail" namespace for internal functions/classes

This commit is contained in:
Jason Turner 2009-08-08 16:38:04 +00:00
parent a3d4b6698a
commit 6775863415
6 changed files with 239 additions and 486 deletions

View File

@ -119,7 +119,7 @@ namespace chaiscript
boost::shared_ptr<Data> get(Boxed_Value::Void_Type)
{
return boost::shared_ptr<Data> (new Data(
Get_Type_Info<void>::get(),
detail::Get_Type_Info<void>::get(),
boost::any(),
false)
);
@ -135,7 +135,7 @@ namespace chaiscript
boost::shared_ptr<Data> get(const boost::shared_ptr<T> &obj)
{
boost::shared_ptr<Data> data(new Data(
Get_Type_Info<T>::get(),
detail::Get_Type_Info<T>::get(),
boost::any(obj),
false,
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> data(new Data(
Get_Type_Info<T>::get(),
detail::Get_Type_Info<T>::get(),
boost::any(obj),
true)
);
@ -192,7 +192,7 @@ namespace chaiscript
boost::shared_ptr<Data> get(const T& t)
{
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))),
false,
boost::shared_ptr<Data::Shared_Ptr_Proxy>(new Data::Shared_Ptr_Proxy_Impl<T>()))
@ -331,6 +331,8 @@ namespace chaiscript
};
namespace detail
{
// Cast_Helper helper classes
/**
@ -500,6 +502,7 @@ namespace chaiscript
return ob;
}
};
}
/**
* 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);
*/
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 {
return Cast_Helper<Type>::cast(bv);
return detail::Cast_Helper<Type>::cast(bv);
} catch (const boost::bad_any_cast &) {
throw bad_boxed_cast(bv.get_type_info(), typeid(Type));
}
@ -676,6 +679,8 @@ namespace chaiscript
bool m_isfloat;
};
namespace detail
{
/**
* Cast_Helper for converting from Boxed_Value to Boxed_POD_Value
*/
@ -689,6 +694,7 @@ namespace chaiscript
return Boxed_POD_Value(ob);
}
};
}
template<typename 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)
{

View File

@ -4,13 +4,6 @@
// and Jason Turner (lefticus@gmail.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__
#define __function_call_hpp__
@ -20,50 +13,7 @@
#include <string>
#include <vector>
#include "proxy_functions.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> &params)
{
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> &params)
{
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()
#include "function_call_detail.hpp"
namespace chaiscript
{
@ -119,38 +69,4 @@ 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

View File

@ -26,7 +26,7 @@ namespace chaiscript
Proxy_Function constructor()
{
T *f = 0;
return (build_constructor_(f));
return (detail::build_constructor_(f));
}
}
@ -34,6 +34,8 @@ namespace chaiscript
# define n BOOST_PP_ITERATION()
namespace chaiscript
{
namespace detail
{
/**
* A constructor function, used for creating a new object
@ -58,6 +60,7 @@ namespace chaiscript
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

View File

@ -4,109 +4,22 @@
// and Jason Turner (lefticus@gmail.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__
#define __proxy_functions_hpp__
#include "boxed_value.hpp"
#include "type_info.hpp"
#include <string>
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <boost/lexical_cast.hpp>
#include <stdexcept>
#include <vector>
#include "proxy_functions_detail.hpp"
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
* it does automatic conversion to Boxed_Value types via operator<<
@ -138,30 +51,6 @@ namespace chaiscript
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
* Proxy_Functions are a type erasure of type safe C++
@ -321,16 +210,16 @@ namespace chaiscript
{
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)
{
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 {
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;
@ -395,7 +284,7 @@ namespace chaiscript
while (true)
{
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);
++barg;
@ -408,7 +297,7 @@ namespace chaiscript
}
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;
}
@ -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> &params)
{
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> &params)
{
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

View File

@ -22,7 +22,8 @@ namespace chaiscript
return Proxy_Function(new Proxy_Function_Impl<T>(f));
}
namespace detail
{
/**
* Helper function for register_member function
*/
@ -31,6 +32,7 @@ namespace chaiscript
{
return (obj->*m);
}
}
/**
* Automatically create a get_member helper function for an object
@ -40,7 +42,7 @@ namespace chaiscript
template<typename T, typename Class>
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)));
}
}

View File

@ -77,6 +77,8 @@ namespace chaiscript
bool m_is_unknown;
};
namespace detail
{
/**
* Helper used to create a Type_Info object
*/
@ -115,19 +117,19 @@ namespace chaiscript
&typeid(typename boost::remove_const<typename boost::remove_pointer<typename boost::remove_reference<T>::type>::type>::type));
}
};
}
template<typename T>
Type_Info user_type(T)
{
return Get_Type_Info<T>::get();
return detail::Get_Type_Info<T>::get();
}
template<typename T>
Type_Info user_type()
{
return Get_Type_Info<T>::get();
return detail::Get_Type_Info<T>::get();
}