| 1 | #include <boost/exception/all.hpp>
|
|---|
| 2 | #include <boost/tuple/tuple.hpp>
|
|---|
| 3 |
|
|---|
| 4 | typedef boost::error_info<struct tag_error_string, std::string> error_string_entry;
|
|---|
| 5 | typedef boost::error_info<struct tag_error_int, int> error_int_entry;
|
|---|
| 6 |
|
|---|
| 7 | struct exception : virtual std::exception, virtual boost::exception
|
|---|
| 8 | { };
|
|---|
| 9 |
|
|---|
| 10 | #ifndef WORKAROUND
|
|---|
| 11 |
|
|---|
| 12 | //I don't see why this code as is shouldn't just work
|
|---|
| 13 | #define THROW_EXCEPTION( cnd__, ... ) if( !(cnd__) ) { \
|
|---|
| 14 | BOOST_THROW_EXCEPTION( exception() << boost::make_tuple( __VA_ARGS__ ) ); }
|
|---|
| 15 |
|
|---|
| 16 | #else
|
|---|
| 17 |
|
|---|
| 18 | //Instead I have to pathcup for the empty case, and surprisingly also the 1-length case
|
|---|
| 19 | #define THROW_EXCEPTION( cnd__, ... ) if( !(cnd__) ) { \
|
|---|
| 20 | BOOST_THROW_EXCEPTION( error_with_tags( exception(), boost::make_tuple( __VA_ARGS__ ) ) ); }
|
|---|
| 21 |
|
|---|
| 22 | template<typename E, typename T,int CNT>
|
|---|
| 23 | struct error_with_tags_helper
|
|---|
| 24 | {
|
|---|
| 25 | E const & operator()( E const & x, T const & t )
|
|---|
| 26 | {
|
|---|
| 27 | x << t;
|
|---|
| 28 | return x;
|
|---|
| 29 | }
|
|---|
| 30 | };
|
|---|
| 31 |
|
|---|
| 32 | template<typename E, typename T>
|
|---|
| 33 | struct error_with_tags_helper<E,T,0>
|
|---|
| 34 | {
|
|---|
| 35 | E const & operator()( E const & x, T const & t )
|
|---|
| 36 | {
|
|---|
| 37 | return x;
|
|---|
| 38 | }
|
|---|
| 39 | };
|
|---|
| 40 |
|
|---|
| 41 | template<typename E, typename T>
|
|---|
| 42 | struct error_with_tags_helper<E,T,1>
|
|---|
| 43 | {
|
|---|
| 44 | E const & operator()( E const & x, T const & t )
|
|---|
| 45 | {
|
|---|
| 46 | return x << boost::get<0>(t);
|
|---|
| 47 | }
|
|---|
| 48 | };
|
|---|
| 49 |
|
|---|
| 50 | template<typename E, typename T>
|
|---|
| 51 | inline E const & error_with_tags( E const & x, T const & t )
|
|---|
| 52 | {
|
|---|
| 53 | error_with_tags_helper<E,T,boost::tuples::length<T>::value> et;
|
|---|
| 54 | return et(x,t);
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | #endif
|
|---|
| 58 |
|
|---|
| 59 | int main()
|
|---|
| 60 | {
|
|---|
| 61 | //Okay
|
|---|
| 62 | THROW_EXCEPTION( true, error_string_entry( "hello" ), error_int_entry( 123 ) );
|
|---|
| 63 |
|
|---|
| 64 | //Fail -- zero parameters
|
|---|
| 65 | THROW_EXCEPTION( true );
|
|---|
| 66 |
|
|---|
| 67 | //Fail -- one parameter
|
|---|
| 68 | THROW_EXCEPTION( true, error_string_entry( "hello" ) );
|
|---|
| 69 | }
|
|---|