Boost logo

Boost :

From: Rene Rivera (grafik666_at_[hidden])
Date: 2002-08-10 14:09:16


[2002-08-10] John Maddock wrote:

>> How about...
>>
>>
>> namespace boost {
>>
>> struct exception_throw_policy
>> {
>> template <class E> void handle_exception(E const & e)
>> {
>> throw e;
>> }
>> };
[snip]
>> #ifndef BOOST_NO_EXCEPTIONS
>> namespace boost {
>> typedef exception_throw_policy exception_policy;
>> }
>> #endif

>Not bad,
:-)

> but the system can't really tell whether exception_policy has been
>defined by the user or not.
Sure it can if you are willing to...

> This would seem to be one of those sad cases
>where macros actually add more functionality:
>
>#ifndef BOOST_THROW_EXCEPTION
---------^^^^^^^^^^^^^^^^^^^^^- Add more preprocessor symbols as you did.
>#ifdef BOOST_NO_EXCEPTIONS
>#include <cstdio>
>#define BOOST_THROW_EXCEPTION(x) std::fprintf(std::stderr, e.what()),
>abort()
>#else
>#define BOOST_THROW_EXCEPTION(x) throw x
>#endif
>#endif

Here's basically the same code as before, but with an additional
preprocessor symbol that the user defines (or not)...
(Only tested on GCC 2.96 RH7.1)

/*
Copyright (C) 2002, Rene Rivera. Permission to copy, use, modify, sell and
distribute this software is granted provided this copyright notice appears
in
all copies. This software is provided "as is" without express or implied
warranty, and with no claim as to its suitability for any purpose.
*/

#ifndef BOOST_THROW_HPP
#define BOOST_THROW_HPP

#include <cassert>
#include <cstdlib>
#include <cstdio>

namespace boost
{
    #ifndef BOOST_NO_EXCEPTIONS
    struct exception_throw_policy
    {
        template <class E> static inline void handle_exception(E const & e)
        {
            throw e;
        }
    };
    #endif
    
    struct exception_assert_policy
    {
        template <class E> static inline void handle_exception(E const & /*
e */)
        {
            assert(false);
        }
    };
    
    struct exception_ignore_policy
    {
        template <class E> static inline void handle_exception(E const & /*
e */)
        {
        }
    };
    
    struct execption_print_abort_policy
    {
        template <class E> static inline void handle_exception(E const & e)
        {
            std::fputs(e.what(),std::stderr);
            std::abort();
        }
    };
    
    #ifndef BOOST_EXCEPTION_POLICY
        #ifdef BOOST_NO_EXCEPTIONS
            #define BOOST_EXCEPTION_POLICY exception_assert_policy
        #else
            #define BOOST_EXCEPTION_POLICY exception_throw_policy
        #endif
    #endif
    
    template <class E> inline void throw_exception(E const & e)
    {
        BOOST_EXCEPTION_POLICY::handle_exception(e);
    }
}

#endif

-- grafik - Don't Assume Anything
-- rrivera_at_[hidden] - grafik_at_[hidden]
-- 102708583_at_icq - Grafik666_at_AIM - Grafik_at_[hidden]


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk