Use better method names and add comments

This commit is contained in:
Thomas Geymayer 2012-11-29 17:52:52 +01:00
parent 2f0dfc4d74
commit 94b068a40f
2 changed files with 17 additions and 5 deletions

View File

@ -229,8 +229,16 @@ namespace nasal
args(args) args(args)
{} {}
/**
* Get the argument with given index if it exists. Otherwise returns the
* passed default value.
*
* @tparam T Type of argument (converted using ::from_nasal)
* @param index Index of requested argument
* @param def Default value returned if too few arguments available
*/
template<class T> template<class T>
T get(size_t index, const T& def = T()) const T getArg(size_t index, const T& def = T()) const
{ {
if( index >= argc ) if( index >= argc )
return def; return def;
@ -238,8 +246,12 @@ namespace nasal
return from_nasal<T>(c, args[index]); return from_nasal<T>(c, args[index]);
} }
/**
* Get the argument with given index. Raises a Nasal runtime error if there
* are to few arguments available.
*/
template<class T> template<class T>
T require(size_t index) const T requireArg(size_t index) const
{ {
if( index >= argc ) if( index >= argc )
naRuntimeError(c, "Missing required arg #%d", index); naRuntimeError(c, "Missing required arg #%d", index);

View File

@ -157,9 +157,9 @@ int main(int argc, char* argv[])
to_nasal(c, std::string("test-arg")) to_nasal(c, std::string("test-arg"))
}; };
CallContext cc(c, sizeof(args)/sizeof(args[0]), args); CallContext cc(c, sizeof(args)/sizeof(args[0]), args);
VERIFY( cc.require<std::string>(0) == "test-arg" ); VERIFY( cc.requireArg<std::string>(0) == "test-arg" );
VERIFY( cc.get<std::string>(0) == "test-arg" ); VERIFY( cc.getArg<std::string>(0) == "test-arg" );
VERIFY( cc.get<std::string>(1) == "" ); VERIFY( cc.getArg<std::string>(1) == "" );
// TODO actually do something with the ghosts... // TODO actually do something with the ghosts...