How to "check" for a boolean function argument through the C api

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

How to "check" for a boolean function argument through the C api

RJP Computing
How do you "check" for a boolean argument from the C api? For that matter, I want the argument to have a default value of 'false'. I see that there are a bunch of luaL_checkxxx() functions, but none for boolean. There is also the set of luaL_optxxx() functions and boolean is not there either.

Here is what I would like the Lua script to look like when calling my function:
        obj:Write( "Unlock", "comma, separated, list, of, arguments", true ) -- The last parameter is optional and is using the Lua built-in 'true' not a string.
       
Inside my bindings for this function:
        luaL_checkstring( L, command, 1 );
        luaL_checkstring( L, args, 2 );
        -- How do I make this optional and let the code above be valid?

I hope this makes sense. Thanks.
--
Regards,
Ryan
RJP Computing
Reply | Threaded
Open this post in threaded view
|

Re: How to "check" for a boolean function argument through the C api

Ralph Hempel
RJP Computing wrote:
How do you "check" for a boolean argument from the C api? For that matter, I want the argument to have a default value of 'false'. I see that there are a bunch of luaL_checkxxx() functions, but none for boolean. There is also the set of luaL_optxxx() functions and boolean is not there either.

Ryan, I could not find it in the C API either, so I wrote my own
versions based on how the other versions that check for int, string, etc
work.

Then I figured there must be a good reason why that seemingly obvious
API call is not included and decided to use 0 and non-zero integers
instead.

Can anyone explain why these are not in the API?

Ralph


Reply | Threaded
Open this post in threaded view
|

Re: How to "check" for a boolean function argument through the C api

Etan Reisner
On Fri, Jul 27, 2007 at 04:55:34PM -0400, Ralph Hempel wrote:
> RJP Computing wrote:
> >How do you "check" for a boolean argument from the C api? For that
> >matter, I want the argument to have a default value of 'false'. I see
> >that there are a bunch of luaL_checkxxx() functions, but none for
> >boolean. There is also the set of luaL_optxxx() functions and boolean is
> >not there either.
>
> Ryan, I could not find it in the C API either, so I wrote my own
> versions based on how the other versions that check for int, string, etc
> work.
>
> Then I figured there must be a good reason why that seemingly obvious
> API call is not included and decided to use 0 and non-zero integers
> instead.
>
> Can anyone explain why these are not in the API?
>
> Ralph

Both lua_toboolean and lua_isboolean exist and can be used to do what you
want.

I have no real idea why there is no luaL_checkboolean other than to guess
that perhaps (like table copying) there wasn't a clear ability to decide
what it should do exactly. Since both true/false only and the lua
normative definition of true/false are valid.

	-Etan

Reply | Threaded
Open this post in threaded view
|

Re: How to "check" for a boolean function argument through the C api

Merick
In reply to this post by Ralph Hempel
Ralph Hempel wrote:

Can anyone explain why these are not in the API?

Ralph

Actually, there is:
if lua_type(L,1) = LUA_TBOOLEAN then arg = lua_toboolean(L,1) end

or, you could use the macro:
if lua_isboolean(L,1)  then arg = lua_toboolean(L,1) end





Reply | Threaded
Open this post in threaded view
|

Re: How to "check" for a boolean function argument through the C api

Romulo Bahiense
Merick wrote:
Ralph Hempel wrote:

Can anyone explain why these are not in the API?

Ralph

Actually, there is:
if lua_type(L,1) = LUA_TBOOLEAN then arg = lua_toboolean(L,1) end

or, you could use the macro:
if lua_isboolean(L,1)  then arg = lua_toboolean(L,1) end

Is this Lua or C? Or both? :P

--rb

Reply | Threaded
Open this post in threaded view
|

Re: How to "check" for a boolean function argument through the C api

RJP Computing
In reply to this post by Etan Reisner
On 7/27/07, Etan Reisner <[hidden email]> wrote:
Both lua_toboolean and lua_isboolean exist and can be used to do what you
want.

I have no real idea why there is no luaL_checkboolean other than to guess
that perhaps (like table copying) there wasn't a clear ability to decide
what it should do exactly. Since both true/false only and the lua
normative definition of true/false are valid.

        -Etan

I found a solution to both the optional arguments and the "check" in luaxx. Here is the code for completeness.

bool as( bool default_bool, int index = -1 )
{
    if ( lua_isboolean( L, index ) )
        return lua_toboolean( L, index );
    else
        return default_bool;
}

state& to( bool& boolean, int index = -1 )
{
    if ( lua_isboolean( L, index ) )
        boolean = lua_toboolean( L, index );
    else
        throw bad_conversion( "Cannot convert non 'boolean' value to bool" );

    return *this;
}

I still would like to know why they are missing from the C api.
--
Regards,
Ryan
RJP Computing
Reply | Threaded
Open this post in threaded view
|

Re: How to "check" for a boolean function argument through the C api

Etan Reisner
Like I said you only handled one of the two possible interpretations of
'boolean' in lua, the true/false only case. The other is the way lua
treats things in an if ... then statement where anything other than false
or nil is 'true'. Your code doesn't handle that second interpretation,
because lua_isboolean checks the type of the value. So, for similar
reasons to why there isn't a default table copy I would assume that they
left it up to the person who wanted the check to write it there way.

But I am of course open to being corrected by the people who actually
know.

	-Etan

Reply | Threaded
Open this post in threaded view
|

Re: How to "check" for a boolean function argument through the C api

Roberto Ierusalimschy
> Like I said you only handled one of the two possible interpretations of
> 'boolean' in lua, the true/false only case. The other is the way lua
> treats things in an if ... then statement where anything other than false
> or nil is 'true'. Your code doesn't handle that second interpretation,
> because lua_isboolean checks the type of the value. So, for similar
> reasons to why there isn't a default table copy I would assume that they
> left it up to the person who wanted the check to write it there way.
> 
> But I am of course open to being corrected by the people who actually
> know.

Your explanation is correct. lua.h provides low-level facilities that
try to allow maximum flexibility. lauxlib.h provides higher-level
facilities for some usual tasks; unlike lua.h, it does not try to be
"complete". Most functions that accept booleans behave (or should
behave?) like Lua: any value but nil/false is true, nil/false is
false. With this interpretation, we do not need a checkboolean, because
any value is valid.

-- Roberto

Reply | Threaded
Open this post in threaded view
|

Re: How to "check" for a boolean function argument through the C api

RJP Computing


On 7/27/07, Roberto Ierusalimschy <[hidden email]> wrote:
Your explanation is correct. lua.h provides low-level facilities that
try to allow maximum flexibility. lauxlib.h provides higher-level
facilities for some usual tasks; unlike lua.h, it does not try to be
"complete". Most functions that accept booleans behave (or should
behave?) like Lua: any value but nil/false is true, nil/false is
false. With this interpretation, we do not need a checkboolean, because
any value is valid.

-- Roberto

OK just so I have this strait, I can use the luaL_checkany() function? I guess what I am driving at is the ability to use the parameter as a boolean like in C++. My problem is then I have to do the type conversion. If I use luaL_checkint() I have to cast the value. This doesn't seem as useful as adding the functionality to luaxlib.h. Like you said it provides the higher level and it seems to miss boolean. On the same argument, if you are saying that you don't want to pidgin hole a boolean into a certain type, well the lua_isboolean() has already defined what a boolean should be. I am not trying to argue, just understand the motivation.

--
Regards,
Ryan
RJP Computing
Reply | Threaded
Open this post in threaded view
|

Re: How to "check" for a boolean function argument through the C api

Etan Reisner
On Fri, Jul 27, 2007 at 06:41:11PM -0400, RJP Computing wrote:

<snip>

> OK just so I have this strait, I can use the luaL_checkany() function? I
> guess what I am driving at is the ability to use the parameter as a boolean
> like in C++. My problem is then I have to do the type conversion. If I use
> luaL_checkint() I have to cast the value. This doesn't seem as useful as
> adding the functionality to luaxlib.h. Like you said it provides the higher
> level and it seems to miss boolean. On the same argument, if you are saying
> that you don't want to pidgin hole a boolean into a certain type, well the
> lua_isboolean() has already defined what a boolean should be. I am not
> trying to argue, just understand the motivation.

If all you want is the standard lua 'any value' vs. 'false/nil' testing
then you just use lua_toboolean directly. If you want the boolean values
only bit then you use lua_isboolean first. Which the refman for
lua_toboolean suggests.

> --
> Regards,
> Ryan
> RJP Computing

	-Etan

Reply | Threaded
Open this post in threaded view
|

Re: How to "check" for a boolean function argument through the C api

Merick
In reply to this post by Romulo Bahiense
Romulo Bahiense wrote:
Merick wrote:
Ralph Hempel wrote:

Can anyone explain why these are not in the API?

Ralph

Actually, there is:
if lua_type(L,1) = LUA_TBOOLEAN then arg = lua_toboolean(L,1) end

or, you could use the macro:
if lua_isboolean(L,1)  then arg = lua_toboolean(L,1) end

Is this Lua or C? Or both? :P

--rb


Actually, It's FreeBasic ;)