00001 //---------------------------------------------------------------------------- 00002 /** @file 00003 */ 00004 //---------------------------------------------------------------------------- 00005 00006 #ifndef SAFEBOOL_HPP 00007 #define SAFEBOOL_HPP 00008 00009 #include "Benzene.hpp" 00010 00011 _BEGIN_BENZENE_NAMESPACE_ 00012 00013 //---------------------------------------------------------------------------- 00014 00015 /** Implements the Save Bool Idiom. Classes deriving off of SafeBool 00016 need to implement a 'bool boolean_test()' method. See: 00017 http://www.artima.com/cppsource/safebool.html. */ 00018 template <typename T> 00019 class SafeBool 00020 { 00021 protected: 00022 typedef void (SafeBool::*bool_type)() const; 00023 void this_type_does_not_support_comparisons() const {} 00024 00025 public: 00026 operator bool_type() const 00027 { 00028 return (static_cast<const T*>(this))->boolean_test() 00029 ? &SafeBool::this_type_does_not_support_comparisons : 0; 00030 } 00031 }; 00032 00033 /** Invalidates operator!= */ 00034 template <typename T, typename U> 00035 bool operator!=(const SafeBool<T>& lhs, const SafeBool<U>& rhs) 00036 { 00037 lhs.this_type_does_not_support_comparisons(); 00038 return false; 00039 } 00040 00041 /** Invalidates operator== */ 00042 template <typename T, typename U> 00043 bool operator==(const SafeBool<T>& lhs, const SafeBool<U>& rhs) 00044 { 00045 lhs.this_type_does_not_support_comparisons(); 00046 return false; 00047 } 00048 00049 //--------------------------------------------------------------------------- 00050 00051 _END_BENZENE_NAMESPACE_ 00052 00053 #endif // SAFEBOOL_HPP