lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I wasn't at all telling you to ignore errors, I just showed you how to *detect* them without leaking nor using exceptions. All the discussion I read were about problems of different alternatives when you stick to original constraints of your code snippet:
        static int SetSongOptions( T* p, lua_State *L )
        {
                ModsLevel m = Enum::Check<ModsLevel>( L, 1 );

                SongOptions so;

                so.FromString( SArg(2) );
                p->m_SongOptions.Assign( m, so );
                return 0;
        }

I kept the constraints. Here are some tries. Neither uses exceptions nor long jumps. They don't affect the code readability much. They don't leak.

/// 1
// No snippet modification
const char* SArg_impl(lua_State* L, int index)
{
   if (lua_isstring(L, index)
      return lua_tostring(L, index);
   else
   {
       /* Do your own error handling here */
       return "";
   }
}
#define SArg(index) SArg_impl(L, index)

/// 2
// Modify snippet, 1 line overhead
#define SArg(index) lua_tostring(L, index)
#define CheckSArg(index) lua_isstring(L, index)
        static int SetSongOptions( T* p, lua_State *L )
        {
                ModsLevel m = Enum::Check<ModsLevel>( L, 1 );

                SongOptions so;

                if (!CheckSArg(2)) return 1;
                so.FromString( SArg(2) );
                p->m_SongOptions.Assign( m, so );
                return 0;
        }

/// 3
// Modified snippet, 1 line overhead
        static int SetSongOptions( T* p, lua_State *L )
        {
                cont char* SArg2 = SArg(2);
                ModsLevel m = Enum::Check<ModsLevel>( L, 1 );

                SongOptions so;

                so.FromString( SArg2 );
                p->m_SongOptions.Assign( m, so );
                return 0;
        }

-----Message d'origine-----
De : lua-bounces@bazar2.conectiva.com.br [mailto:lua-bounces@bazar2.conectiva.com.br] De la part de Glenn Maynard
Envoyé : 17 octobre 2006 16:34
À : lua@bazar2.conectiva.com.br
Objet : Re: Lua error handling and C++

On Tue, Oct 17, 2006 at 11:05:37AM -0400, Jerome Vuarand wrote:
> I have troubles understanding why so much has been written around a problem that is so easy to solve.

Error checking is indeed easy, if you don't do any.  :)  We're discussing methods of handling errors, not methods of silently ignoring them, though I guess in some contexts that might be a reasonable alternative.

--
Glenn Maynard