wibble  0.1.28
fallback.h
Go to the documentation of this file.
00001 
00006 #include <wibble/exception.h>
00007 
00008 #ifndef WIBBLE_FALLBACK_H
00009 #define WIBBLE_FALLBACK_H
00010 
00011 namespace wibble {
00012 
00013 struct Error {};
00014 
00015 template< typename T >
00016 struct Fallback {
00017     const T *value;
00018     Fallback( const T &v ) : value( &v ) {}
00019     Fallback( Error = Error() ) : value( 0 ) {}
00020 
00021     template< typename E > const T &get( const E &e ) {
00022         if ( !value ) throw e;
00023         return *value;
00024     }
00025 
00026     const T &get() {
00027         if ( !value ) throw exception::Consistency( "tried to use undefined fallback value" );
00028         return *value;
00029     }
00030 };
00031 
00032 }
00033 
00034 #endif