cppbind: Add some methods to nasal::CallContext
This commit is contained in:
@@ -121,6 +121,16 @@ namespace nasal
|
||||
};
|
||||
|
||||
BOOST_MPL_HAS_XXX_TRAIT_DEF(element_type)
|
||||
|
||||
template<class T1, class T2>
|
||||
struct reduced_is_same:
|
||||
public boost::is_same<
|
||||
typename boost::remove_cv<
|
||||
typename boost::remove_reference<T1>::type
|
||||
>::type,
|
||||
T2
|
||||
>
|
||||
{};
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -134,6 +144,48 @@ namespace nasal
|
||||
args(args)
|
||||
{}
|
||||
|
||||
bool isNumeric(size_t index) const
|
||||
{
|
||||
return (index < argc && naIsNum(args[index]));
|
||||
}
|
||||
|
||||
bool isString(size_t index) const
|
||||
{
|
||||
return (index < argc && naIsString(args[index]));
|
||||
}
|
||||
|
||||
bool isHash(size_t index) const
|
||||
{
|
||||
return (index < argc && naIsHash(args[index]));
|
||||
}
|
||||
|
||||
bool isVector(size_t index) const
|
||||
{
|
||||
return (index < argc && naIsVector(args[index]));
|
||||
}
|
||||
|
||||
bool isGhost(size_t index) const
|
||||
{
|
||||
return (index < argc && naIsGhost(args[index]));
|
||||
}
|
||||
|
||||
void popFront(size_t num = 1)
|
||||
{
|
||||
if( argc < num )
|
||||
return;
|
||||
|
||||
args += num;
|
||||
argc -= num;
|
||||
}
|
||||
|
||||
void popBack(size_t num = 1)
|
||||
{
|
||||
if( argc < num )
|
||||
return;
|
||||
|
||||
argc -= num;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the argument with given index if it exists. Otherwise returns the
|
||||
* passed default value.
|
||||
@@ -149,7 +201,7 @@ namespace nasal
|
||||
if( index >= argc )
|
||||
return def;
|
||||
|
||||
return (*from_nasal_ptr<T>::get())(c, args[index]);
|
||||
return from_nasal<T>(args[index]);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -163,7 +215,7 @@ namespace nasal
|
||||
if( index >= argc )
|
||||
naRuntimeError(c, "Missing required arg #%d", index);
|
||||
|
||||
return (*from_nasal_ptr<T>::get())(c, args[index]);
|
||||
return from_nasal<T>(args[index]);
|
||||
}
|
||||
|
||||
template<class T>
|
||||
@@ -172,6 +224,13 @@ namespace nasal
|
||||
return nasal::to_nasal(c, arg);
|
||||
}
|
||||
|
||||
template<class T>
|
||||
typename from_nasal_ptr<T>::return_type
|
||||
from_nasal(naRef ref) const
|
||||
{
|
||||
return (*from_nasal_ptr<T>::get())(c, ref);
|
||||
}
|
||||
|
||||
naContext c;
|
||||
size_t argc;
|
||||
naRef *args;
|
||||
@@ -794,7 +853,7 @@ namespace nasal
|
||||
template<class Arg>
|
||||
static
|
||||
typename boost::disable_if<
|
||||
boost::is_same<Arg, const CallContext&>,
|
||||
internal::reduced_is_same<Arg, CallContext>,
|
||||
typename from_nasal_ptr<Arg>::return_type
|
||||
>::type
|
||||
arg_from_nasal(const CallContext& ctx, size_t index)
|
||||
@@ -808,11 +867,14 @@ namespace nasal
|
||||
template<class Arg>
|
||||
static
|
||||
typename boost::enable_if<
|
||||
boost::is_same<Arg, const CallContext&>,
|
||||
internal::reduced_is_same<Arg, CallContext>,
|
||||
typename from_nasal_ptr<Arg>::return_type
|
||||
>::type
|
||||
arg_from_nasal(const CallContext& ctx, size_t)
|
||||
{
|
||||
// Either const CallContext& or CallContext, non-const reference
|
||||
// does not make sense.
|
||||
BOOST_STATIC_ASSERT( (!boost::is_same<Arg, CallContext&>::value) );
|
||||
return ctx;
|
||||
};
|
||||
|
||||
|
||||
@@ -227,13 +227,27 @@ int main(int argc, char* argv[])
|
||||
// Test nasal::CallContext
|
||||
//----------------------------------------------------------------------------
|
||||
|
||||
|
||||
int int_vec[] = {1,2,3};
|
||||
std::map<std::string, std::string> map;
|
||||
naRef args[] = {
|
||||
to_nasal(c, std::string("test-arg"))
|
||||
to_nasal(c, std::string("test-arg")),
|
||||
to_nasal(c, 4),
|
||||
to_nasal(c, int_vec),
|
||||
to_nasal(c, map)
|
||||
};
|
||||
CallContext cc(c, sizeof(args)/sizeof(args[0]), args);
|
||||
VERIFY( cc.requireArg<std::string>(0) == "test-arg" );
|
||||
VERIFY( cc.getArg<std::string>(0) == "test-arg" );
|
||||
VERIFY( cc.getArg<std::string>(1) == "" );
|
||||
VERIFY( cc.getArg<std::string>(10) == "" );
|
||||
VERIFY( cc.isString(0) );
|
||||
VERIFY( !cc.isNumeric(0) );
|
||||
VERIFY( !cc.isVector(0) );
|
||||
VERIFY( !cc.isHash(0) );
|
||||
VERIFY( !cc.isGhost(0) );
|
||||
VERIFY( cc.isNumeric(1) );
|
||||
VERIFY( cc.isVector(2) );
|
||||
VERIFY( cc.isHash(3) );
|
||||
|
||||
naRef args_vec = nasal::to_nasal(c, args);
|
||||
VERIFY( naIsVector(args_vec) );
|
||||
|
||||
Reference in New Issue
Block a user